Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何检查用户是否在单击较大对象之前单击较小对象?_Actionscript 3_If Statement_Boolean_Flash Cs6_Boolean Logic - Fatal编程技术网

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

Actionscript 3 如何检查用户是否在单击较大对象之前单击较小对象?,actionscript-3,if-statement,boolean,flash-cs6,boolean-logic,Actionscript 3,If Statement,Boolean,Flash Cs6,Boolean Logic,大家好,我现在正在做一个游戏,目标是让用户点击舞台上的物体。但是用户必须先单击最大的对象,然后才能单击较小的对象。我想让它,如果用户先点击一个较小的对象,而不是较大的对象,那么游戏就结束了。我想我可以为舞台上的每个对象设置布尔值,但我的if语句没有对其进行切割,我就是这样设置的: 以下是我使用的对象和布尔值: //Add box references public var box_1:MovieClip; public var box_2:MovieClip; publi

大家好,我现在正在做一个游戏,目标是让用户点击舞台上的物体。但是用户必须先单击最大的对象,然后才能单击较小的对象。我想让它,如果用户先点击一个较小的对象,而不是较大的对象,那么游戏就结束了。我想我可以为舞台上的每个对象设置布尔值,但我的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

以下是与主输入帧侦听器关联的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还没有被点击,这是一个较大的对象,那么游戏现在结束了,因为用户没有先点击最大的对象。我将其设置为,框_1是最大的对象,而其他的则是下一个大小

有人能理解为什么这样做不正确,或者有没有更好的方法

****现在更新我的主类的设置方式**************

我在其中添加所有电影剪辑和变量:

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);
        }
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();
             }
         }

    }
最后,在
框中单击
功能:

         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);
        }
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();
             }
         }

    }
private函数框单击(e:MouseEvent):无效
{
var box:MovieClip=e.currentTarget as MovieClip;//获取对刚刚单击的对象的引用
box.mouseEnabled=false;//我们将使用它作为标志,以了解是否已单击它
box.gotoAndPlay(“BGONE”);
//检查是否单击了前面的框。
//这将按顺序遍历所有框
for(变量i:int=0;i
可能不起作用,因为您的最终
else if
语句永远无法到达(因为您之前处理的
b2==true
将绕过所有其他语句)。再加上在您之前处理它时将b2设置为false,因此在它得到您的最终声明时,它将始终为false

在检查其他东西之前,你需要移动最后一个。请参见代码注释:

    //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;
    }
另外,您不需要enterframe处理程序,只需单击即可检查所有内容。类似这样的方法会奏效:

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();
        }
    }
}
var-box:Array=[box_1,box_2,box_3,box_4]//确保这是按照需要首先单击的顺序进行的
//如果您想根据总大小对它们进行动态排序(不管您在数组中填充它们的顺序如何),可以添加如下内容,将它们从最大到最小排序:
框。排序(功能(a、b){
//比较哪个项目的总面积更大(宽度*高度)
如果(a.width*a.height>b.width*b.height)返回-1;//a更大,那么在数组中把a放在b之前
如果(a.width*a.height//将剪辑存储在数组中
var myClips:Array=[mc1、mc2、mc3、mc4];
//获取剪辑大小并将其存储到阵列中。
变量大小:数组=新数组();
对于(变量i:uint=0;i
Hey@LDMS感谢您的回复。出于某种原因,仍在调用if语句:/。我本来打算实现您编写的第二个方法,但它不会考虑用户必须在单击任何其他较小的框之前先单击较大的框(即框1)。当然可以。只要您的顶部的数组按从大到小的顺序排列非常感谢您提供的详细答案。我刚刚接受了您的答案。我只收到一个错误:“无法访问空对象引用的属性或方法。”我认为这与盒子不可用有关。我将其设置为盒子已经添加到舞台上的位置。更具体地说,是另一个名为“mainScreen”的电影剪辑,然后从该屏幕上,我只引用盒子,因为我的代码显示“box_1,等等”有什么我应该做不同的事情来避免这个错误吗?再次感谢,等等,也许这根本与此无关。我相信这与我如何设置阵列有关。我正在使用flash develop,所以在它“扩展movieclip”的地方,我添加了:private var aBoxesArray:Array;然后在我的解释中