Click anywhere close (CocosCreator)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/shirln/article/details/94551278

Recommended reading:

      Today, a new feature to the contact, when a pop-up box shells need to click anywhere but the remaining playing box, to close the box shells, for example: when the red content display box, clicking anywhere outside the red box red box may be closed . Here Insert Picture Description
      Just start doing this feature, first thought:
      judge click UI UI frame is not the bomb, You only need to change if a statement can be achieved, this time I found two difficult points, 1: How to determine whether click UI it is the target missile frame; 2: using this method, you need to add an event listener for the whole UI interface in addition to bomb box, due to start to build UI when there is no consideration of this case, so to add a listener for the whole UI interface in addition to playing the box the event is particularly complicated.

      For this reason, I am determined to give this method, I find another way: Can I add a Button transparent screen size when playing box pops up to display the Button, add an event listener for the Button: Close popup box . But I also found problems with U3D previous events, click on the event with a penetrating effect, when a bomb box pops up, Button also show that this will result in a bomb box click on the UI, also triggers monitor events the Button.

      Of course, this is just speculation based on their experience in the development, since being not think of any other method, I tried it and found that there is no penetration cocos events. So, when you are a blind alley, the need for try, sometimes you succeed and only one step: hands-on practice.

Next. I talk about the implementation of it
1. above a level playing box, create a transparent Button, named: anyPos
2. Add a listener for the event anyPos

		// 查找btn
        var findBtn = function (str, prt, call, bAudio = true) {
            var obj = cc.find(str, prt);
            obj.on("click", function () {
                if (bAudio) { gm.HallAudio.playClick(); }
                call();
            }, this);
            return obj;
        };

		
		//任意位置按钮
        uiRoot.anyPos = findBtn("anyPos", obj3, function () {
            self.uiRoot.myBox.active = false;//隐藏弹框
            self.uiRoot.anyPos.active = false;//隐藏anyPos
        })
        //默认anyPos为隐藏的
        self.uiRoot.anyPos.active = false;

3. When the playing box

//打开弹框
    openMyBox() {
        var self = this;
        
    self.uiRoot.myBox.active = true;
    self.uiRoot.anyPos.active = true;
    }

In fact, this feature very simple, a few lines of code is achieved, not difficult to understand.

Guess you like

Origin blog.csdn.net/shirln/article/details/94551278