Question

I am creating a game in which bubbles with simple math equations are falling from the top of the screen, and you have to enter in the answer to make the specific bubble disappear. The only problem is that I need a system that can delete the bubble(s) corresponding to the answer. I'm using an Actions-Frame code and a mathBubble AS class. I will show the frame code first.

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

Reading the code is not completely necessary, I was just wondering if anybody could explain how I could use arrays to remove every child with the same answer as the randomized answer each child contains through variables "mathAnswer" and "mathAnswerString" I figured arrays would be how this would work, but if there's any other way then please say so. Thank you for reading this far.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top