我是否必须写出所有可能性?

时间:2017-01-17 09:30:41

标签: actionscript-3 flash flash-cs6

我做了记忆游戏

Fishes(它的按钮)显示在屏幕上并停止,然后其他鱼(错误的鱼)显示在舞台上。一段时间后停止所有场景,玩家猜猜(点击)右鱼。

例如,如果玩家点击了2个假和1个真实按钮,或者2个真实和1个假,则会导致玩家失去。如果点击为真3 fis则会导致玩家获胜。

但问题是我必须为所有可能性编写代码。我怎么能这么简单。

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



 

2 个答案:

答案 0 :(得分:1)

您可以更改代码,因此以这种方式变得更加容易:

您可以调用链接到不同按钮的相同事件,然后您可以评估事件的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,也可以定义一个可以封装不同点击的对象。

<强>更新

您可以定义名为(例如)ActionScript的{​​{1}}对象,如下所示:

QuizLevel

当您开始新的测验级别时,您可以定义矩阵。

如果只有三个是真的,那么实例化的[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

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

我创建了一个levelNo = 1 value1 = true value2 = true value3 = true value4 = false value5 = false value6 = false (通过AIR,但对于Flash Player和其他人来说是相同的),我在s:WindowedApplication

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

        

s:WindowedApplication

答案 1 :(得分:1)

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.
    }
}