我必须写出所有的可能性吗?

Do I have to write all the possibilities?

我做了一个记忆游戏

鱼(它的按钮)显示在屏幕上并停止,然后其他鱼(错误的鱼)显示在 stage.After 一会儿停止所有场景,玩家猜测(点击)正确的鱼。

例如,如果玩家点击了 2 个 false 和 1 个 true 按钮,或者 2 个 true 和 1 个 false,这将导致 player.And 如果点击 true 3 fis 则玩家获胜.

但问题是我必须为所有代码编写代码 possibilities.How 我能做到这么简单吗?

var clicked1:Boolean = false;
var clicked2:Boolean = false;
var clicked3:Boolean = false;
var clicked4:Boolean = false;
var clicked5:Boolean = false;
var clicked6:Boolean = false;
 
 
btn1.addEventListener(MouseEvent.CLICK, fish1);
function fish1(event:MouseEvent):void
{

    clicked1 = true;
    checkButtonsone()
    }
 
btn2.addEventListener(MouseEvent.CLICK, redButton1a);
function redButton1a(event:MouseEvent):void
{

    clicked2 = true;
    checkButtonsone()
    }
 
btn3.addEventListener(MouseEvent.CLICK, redButton12);
function redButton12(event:MouseEvent):void
{

    clicked3 = true;
    checkButtonsone()
    }
 
btn4.addEventListener(MouseEvent.CLICK, redButton22);
function redButton22(event:MouseEvent):void
{

    clicked4 = true;
    checkButtonsone()
    }
 
btn5.addEventListener(MouseEvent.CLICK, redButton32);
function redButton32(event:MouseEvent):void
{

    clicked5 = true;
    checkButtonsone()
    }
 
btn6.addEventListener(MouseEvent.CLICK, redButton42);
function redButton42(event:MouseEvent):void
{

    clicked6 = true;
    checkButtonsone()
    }
 
//Check true and false

function checkButtonsone():void
var correctcombine = false;
var falsecombine1 = false;
 
{
if(clicked1 && clicked2 && clicked3 )
    {
    correctcombine = true;
    }
 
    {
    if(falsetiklandi && falsetiklandi && falsetiklandi){
       falsecombine1 = true;
    }
 
///Go to true or false
 
if(correctcombine == true)
 
            {
            gotoAndStop(3)
        }   

        if(falsecombine1 == true)
        {
            gotoAndStop(2)
        }
}
}
}



 

您可以更改您的代码,这样就更容易了:

您可以调用链接到不同按钮的相同事件,然后您可以评估事件的 currentTarget 属性(作为参数传递)

btn1.addEventListener(MouseEvent.CLICK, changeClicked);
btn2.addEventListener(MouseEvent.CLICK, changeClicked);
btn3.addEventListener(MouseEvent.CLICK, changeClicked);
btn4.addEventListener(MouseEvent.CLICK, changeClicked);
btn5.addEventListener(MouseEvent.CLICK, changeClicked);
btn6.addEventListener(MouseEvent.CLICK, changeClicked);

function changeClicked(event:MouseEvent):void {
   switch(event.currentTarget.id) {
       case "btn1": {
           clicked1 = true;
           break;
       }
       case "btn2": {
           clicked2 = true;
           break;
       }
       case "btn3": {
           clicked3 = true;
           break;
       }           
       case "btn4": {
           clicked4 = true;
           break;
       }
       case "btn5": {
           clicked5 = true;
           break;
       }
       case "btn6": {
           clicked6 = true;
           break;
       }
   }
    checkButtonsone();
}

您必须用我的代码覆盖您的代码:

btn1.addEventListener(MouseEvent.CLICK, fish1);

直到前一行

//Check true and false

可以使用 6 个不同的变量来进一步优化,您可以使用 Array 或 ArrayCollection,或者您可以定义一个对象来封装不同的点击。

更新

您可以定义一个名为(例如)QuizLevelActionScript 对象,如下所示:

[Bindable]
public class QuizLevel {
    private var _levelNo:int;
    private var _value1:Boolean;
    private var _value2:Boolean;
    private var _value3:Boolean;
    private var _value4:Boolean;
    private var _value5:Boolean;
    private var _value6:Boolean;

    // Here you put getter and setter
}

当你开始一个新的测验级别时,你定义了你的矩阵。

如果只有三个为真,则您有一个 QuizLevel 对象实例化如下:

levelNo = 1
value1 = true
value2 = true
value3 = true
value4 = false
value5 = false
value6 = false

您可以在您的 MXML 定义中编写(代替按钮,使用复选框和一个唯一的按钮来提交您的选择)

我已经创建了一个 s:WindowedApplication(通过 AIR,但对于 Flash Player 和其他人来说是相同的)并且我已经在 s:WindowedApplication

中定义了一个预初始化事件

        import mx.controls.Alert;
        import mx.events.FlexEvent;
        [Bindable]
        private var quizLevel:QuizLevel = new QuizLevel();

        private var check1:Boolean = false;
        private var check2:Boolean = false;
        private var check3:Boolean = false;
        private var check4:Boolean = false;
        private var check5:Boolean = false;
        private var check6:Boolean = false;

        protected function windowedapplication1_preinitializeHandler(event:FlexEvent):void
        {
            quizLevel = new QuizLevel();
            quizLevel.levelNo = 1;
            quizLevel.value1 = true;
            quizLevel.value2 = true;
            quizLevel.value3 = true;
            quizLevel.value4 = false;
            quizLevel.value5 = false;
            quizLevel.value6 = false;
        }

        protected function changeClicked(event:MouseEvent):void
        {
            var checkBox:CheckBox = event.currentTarget as CheckBox;
            switch(event.currentTarget.id) {
                case "chk1":{
                    check1 = checkBox.selected;
                    break;
                }
                case "chk2":{
                    check2 = checkBox.selected;
                    break;
                }
                case "chk3":{
                    check3 = checkBox.selected;
                    break;
                }
                case "chk4":{
                    check4 = checkBox.selected;
                    break;
                }
                case "chk5":{
                    check5 = checkBox.selected;
                    break;
                }
                case "chk6":{
                    check6 = checkBox.selected;
                    break;
                }
            }

        }

        protected function btnSubmit_clickHandler(event:MouseEvent):void
        {
            var message:String = "";
            if (quizLevel.value1 == check1 &&
                quizLevel.value2 == check2 &&
                quizLevel.value3 == check3 &&
                quizLevel.value4 == check4 &&
                quizLevel.value5 == check5 &&
                quizLevel.value6 == check6) {
                message = "It'OK";
            } else {
                message = "You're wrong";
            }

            Alert.show(message);
        }

    ]]>
</fx:Script>
<s:VGroup width="100%" height="100%">
    <s:CheckBox id="chk1" label="Choose #1" click="changeClicked(event)" />
    <s:CheckBox id="chk2" label="Choose #2" click="changeClicked(event)" />
    <s:CheckBox id="chk3" label="Choose #3" click="changeClicked(event)" />
    <s:CheckBox id="chk4" label="Choose #4" click="changeClicked(event)" />
    <s:CheckBox id="chk5" label="Choose #5" click="changeClicked(event)" />
    <s:CheckBox id="chk6" label="Choose #6" click="changeClicked(event)" />
    <s:Button id="btnSubmit" label="Submit" click="btnSubmit_clickHandler(event)" />
</s:VGroup>
var buttons:Array = [fish1,fish2,fish3,fish4,fish5,fish6];
var correct:Array = [true,true,true,false,false,false];
var answers:Array = [false,false,false,false,false,false];
var clicksLeft:int = 3;

for each (var aButton:InteractiveObject in buttons)
{
    aButton.addEventListener(MouseEvemt.CLICK, onButton);
}

function onButton(e:Event):void
{
    var aButton:InteractiveObject = e.currentTarget as InteractiveObject;
    var anIndex:int = buttons.indexOf(aButton);

    // Remove click handler to avoid unnecessary handling.
    aButton.removeEventListener(MouseEvemt.CLICK, onButton);

    // Check answers.
    answers[anIndex] = true;

    clicksLeft--;

    if (clicksLeft > 0)
    {
        return;
    }

    var aWrong:Boolean = false;
    var aComplete:Boolean = true;

    for (var i:int = 0; i < correct.length; i++)
    {
        if (answers[i] != correct[i])
        {
            if (answers[i])
            {
                // answers[i] == true and correct[i] == false
                aWrong = true;
            }
            else
            {
                // answers[i] == false and correct[i] == true
                // That means all correct buttons are not pressed yet.
                aComplete = false;
            }
        }
    }

    if (aWrong)
    {
        // User failed case.
    }
    else if (aComplete)
    {
        // User win case.
    }
}