2

我正在创建一个游戏,其中带有简单数学方程式的气泡从屏幕顶部掉落,您必须输入答案才能使特定气泡消失。唯一的问题是我需要一个可以删除与答案相对应的气泡的系统。我正在使用 Actions-Frame 代码和 mathBubble AS 类。我将首先显示框架代码。

stop();
var cooldown:int = 200;
var cooldownMax:int = 200;
inputAnswer.restrict = "^A-Za-z";
addEventListener(Event.ENTER_FRAME, bubbleSpawn);

function bubbleSpawn(e:Event) {

    if (cooldown>cooldownMax) {
        var bubble = new mathBubble();
        addChild(bubble);
        bubble.x = Math.round(Math.random()*480);
        bubble.y = 0;
        cooldown = 0;
    }

    cooldown += 1;

}

package {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.text.*;

    public class mathBubble extends MovieClip {

        var firstInteger:int;
        var equationSymbol:int;
        var secondInteger:int;
        var mathAnswer:int;
        var firstSpace:String;
        var secondSpace:String;
        var mathAnswerString:String;

        function mathBubble() {
            firstInteger = Math.round(Math.random()*9);
            equationSymbol = Math.round(Math.random()*2)+1;
            secondInteger = Math.round(Math.random()*9);

            trace("bubble spawned");

            firstSpace = String(firstInteger);
            secondSpace = String(secondInteger);
            firstNumber.text = firstSpace;
            secondNumber.text = secondSpace;

            if (equationSymbol==1) {
                mathSymbol.text = "+";
                mathAnswer = firstInteger+secondInteger;
                mathAnswerString = String(mathAnswer);
                trace(mathAnswerString);
            }
            if (equationSymbol==2) {
                mathSymbol.text = "-";
                mathAnswer = firstInteger-secondInteger;
                mathAnswerString = String(mathAnswer);
                trace(mathAnswerString);
            }

            if (equationSymbol==3) {
                mathSymbol.text = "x";
                mathAnswer = firstInteger*secondInteger;
                mathAnswerString = String(mathAnswer);
                trace(mathAnswerString);
            }

            addEventListener(Event.ENTER_FRAME, bubbleFall);
            function bubbleFall(e:Event) {
                y += 1;
            }
        }
    }
}

阅读代码并不是完全必要的,我只是想知道是否有人可以解释我如何使用数组来删除每个孩子,其答案与每个孩子通过变量“mathAnswer”和“mathAnswerString”包含的随机答案相同,我认为数组将是这将如何工作,但如果有任何其他方式,那么请说出来。谢谢你读到这里。

4

1 回答 1

1

You could use a CustomEvent with the an answer property. After an answer is given dispatch an event with the answer, each bubble can then check against their own answer.

If the strings are identical, you will need to implement a remove function in the bubble, where you remove all event listeners and finally the parent remove the child.

To dispatch and listen to the event you could pass an event dispatcher as a parameter to each bubble.

For a child to remove itself:

    if(this.parent != null )
       this.parent.removeChild( this );

As for the child removing itself after its y position is greater than 50, I'm not sure having the child removing itself would be the better solution since you would have to implement an enter frame event listener in each bubble... it may be more efficient to have a single event listener in the container checking all bubbles.

于 2011-11-25T02:39:49.283 回答