0

I'll just start by saying that I am a bit new to programming, and I apologize if this is a stupid question.

I have a timer running in my application that at every interval, creates an a new instance of a MovieClip called blueBall.Here is my code:

var randomX:Number = Math.random() * 350;
var newBlue:mc_BlueBall = new mc_BlueBall  ;
newBlue.x = randomX;
newBlue.y = -20;

for(var i:Number = 0;i < blueArray.length; i++)
{
newBlue.name = "newBlue" + i;
}

blueArray.push(newBlue);
addChild(newBlue);

}
var randomX:Number = Math.random() * 350;

var newBlue:mc_BlueBall = new mc_BlueBall  ;
newBlue.x = randomX;
newBlue.y = -20;

for(var i:Number = 0;i < blueArray.length; i++)
{
newBlue.name = "newBlue" + i;
}

blueArray.push(newBlue);
addChild(newBlue);

}

My question is: How do I make it so that each newly created object in the array has it's own hitTestObject Event? I want to make it so that if the if the user's icon touches one of the newBlue objects, that newBlue object with be removed, and the score will go up a point.

Thanks!

1
  • 1
    You should change the name of the question, it does not reflect the real question. Commented Nov 6, 2013 at 23:01

1 Answer 1

1

this is my first time answering a question here but I hope I can help! Assuming you have a timer for your main game loop, you should try something like this once per frame:

//For each blue ball in the array
for(var i:int = 0; i < blueArray.length; i++) {
    //If it touches the player
    if(blueArray[i].hitTestObject(thePlayerMC)) {
        //Increment the score
        score++;
        //Remove from stage and array
        removeChild(blueArray[i]);
        blueArray.splice(i, 1); //<-At index i, remove 1 element
        //Decrement i since we just pulled it out of the array and don't want to skip the next array item
        i--;
    }
}

This is sort of the quick and dirty solution, but highly effective and commonly used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.