如何检查用户是否在较大对象之前单击较小对象?

How to check if user clicked on smaller object before larger object?

大家好,我目前正在开发一款游戏,objective 供用户点击舞台上的对象。但是用户必须先点击最大的对象,然后才能点击较小的对象。我想让它如果用户先点击一个较小的对象而不是较大的对象,那么游戏就会结束。我以为我可以为舞台上的每个对象使用布尔值来设置它,但我的 if 语句并没有把它剪下来,这里是我设置它的方式:

这是我使用的对象和布尔值:

//Add box references
    public var box_1:MovieClip;
    public var box_2:MovieClip;
    public var box_3:MovieClip;
    public var box_4:MovieClip;

    //Booleans
    private var b1:Boolean;
    private var b2:Boolean;
    private var b3:Boolean;
    private var b4:Boolean;

现在我将所有布尔值设置为 false,如果用户单击其中一个对象,我将布尔值设置为 true。

这是与我的主要 ENTER_FRAME 侦听器关联的 if 语句:

    private function level_1():void 
    {

        if (b1)
        {
            mainScreen.box_1.gotoAndPlay("B1GONE");
            b1 = false;
        }else
        if (b2)
        {
            mainScreen.box_2.gotoAndPlay("B2GONE");
            b2 = false;
        }else
        if (b3)
        {
            mainScreen.box_3.gotoAndPlay("B3GONE");
            b3 = false;
        }else
        if (b2 && !b1)
        {
            endGameCondition();
        }

    }

关于这个声明:

if (b2 && !b1)
        {
            endGameCondition();
        }

我试图声明,如果 box_2 为真意味着它被点击,而 box_1 还没有被点击,这是更大的对象那么游戏现在由于用户而结束不要先点击最大的对象。我将其设置为 box_1 是最大的对象,其他对象是下一个尺寸。

谁能看出为什么这不能正常工作,或者是否有更好的方法?

**** 更新我的主要 CLASS 现在的设置 ***************

我添加所有影片剪辑和变量的位置:

public class boxTapEngine extends MovieClip 
{

    //Screens
    private var mainScreen:mcMainScreen;



    //Add box references
    public var box_1:MovieClip;
    public var box_2:MovieClip;
    public var box_3:MovieClip;
    public var box_4:MovieClip;


    private var aBoxesArray:Array;

在我的构造函数中:

         aBoxesArray = [box_1, box_2, box_3, box_4];

        //AddMainScreen
        mainScreen = new mcMainScreen();
        mainScreen.x = (stage.stageWidth / 2);
        mainScreen.y = (stage.stageHeight / 2);
        stage.addChild(mainScreen);

        //Initiate numbers
        nLevel = 1;

        for each(var box:MovieClip in aBoxesArray)
        {
            box.addEventListener(MouseEvent.CLICK, boxClick);
        }

终于在 boxClick 函数上:

private function boxClick(e:MouseEvent):void 
    {

         var box:MovieClip = e.currentTarget as MovieClip; //get a reference to one that was just clicked
         box.mouseEnabled = false; //we'll use this as a flag to know if it's been clicked yet
         box.gotoAndPlay("BGONE");


          //check to see if previous boxes have been clicked.
         //this will iterate through all the boxes in order
         for (var i:int = 0; i < aBoxesArray.length; i++)
         {
             if(aBoxesArray[i] == box) return; //if we've reached the currently clicked box, all is good, no need to keep checking so let's exit this loop and function
             if (!aBoxesArray[i].mouseEnabled)
             { //one of the previous boxes hasn't been clicked yet
                 endGameCondition();
             }
         }

    }

可能无法正常工作,因为您的最终 else if 语句将永远不会到达(因为您正在处理 b2 == true 之前将绕过所有其他 else 语句)。加上您在较早处理时将 ​​b2 设置为 false,因此在它收到您的最终声明时它始终为 false。

在检查其他内容之前,您需要移动最后的 else if。见代码注释:

    //Do this first, and not as a else if
    if (b2 && !b1){
        endGameCondition();
        return; //no need to check checking things if it's game over
    }

    //now you can do the rest
    if (b1)
    {
        mainScreen.box_1.gotoAndPlay("B1GONE");
        b1 = false;
    }else
    if (b2)
    {
        mainScreen.box_2.gotoAndPlay("B2GONE");
        b2 = false;
    }else
    if (b3)
    {
        mainScreen.box_3.gotoAndPlay("B3GONE");
        b3 = false;
    }

顺便说一句,您不需要进入框架处理程序,您只需单击即可检查所有内容。像这样的东西会起作用:

var boxes:Array = [box_1,box_2,box_3,box_4]; //make sure this is in order of what needs to be clicked first

//if you wanted to actually sort them dynamically based off total size (regardless of what order you've stuffed them in the array), you could add in something like this, which will order them from biggest to smallest:
boxes.sort(function(a,b){
    //compare which item has a greater total area (width * height)
    if(a.width * a.height > b.width * b.height) return -1; //A is bigger, so put a before b in the array
    if(a.width * a.height < b.width * b.height) return 1; //put b before a in the array
    return 0; //return 0 if they are the same
});




for each(var box:MovieClip in boxes){
    box.addEventListener(MouseEvent.CLICK, boxClick,false,0,true);
}

function boxClick(e:Event):void {
    var box:MovieClip = e.currentTarget as MovieClip; //get a reference to one that was just clicked

    box.mouseEnabled = false; //we'll use this as a flag to know if it's been clicked yet
    box.gotoAndPlay("BGONE");

    //or you could just do this to get rid of the box:
    if(box.parent) box.parent.removeChild(box);

    //check to see if previous boxes have been clicked.
    //this will iterate through all the boxes in order
    for(var i:int=0;i<boxes.length;i++){
        if(boxes[i] == box) return; //if we've reached the currently clicked box, all is good, no need to keep checking so let's exit this loop and function
        if(!boxes[i].mouseEnabled){ //one of the previous boxes hasn't been clicked yet
            endGameCondition();
        }
    }
}
//Store your clips in an array
var myClips:Array = [mc1,mc2,mc3,mc4];

//get the clip sizes and store it to an array.
var sizes:Array = new Array();
for (var i:uint = 0; i < myClips.length; i++) {
    sizes.push(myClips[i].width);
}

//apply Numeric array sort. 
sizes.sort(Array.NUMERIC);
function onClickAction(e:MouseEvent):void { 
    //Check wheather the array is empty or not.
    if (sizes.length != 0) {
        //Check wheather the clicked object bigger or not.
        if (e.target.width == sizes[sizes.length - 1]) {
            trace("Bigger");
            e.target.alpha = .5;
            sizes.splice(sizes.length-1,1);         
        } else {
            trace("Smaller");
        }
    }
}