Forums

ForumsProgramming Forum

Hittesting multiple movie clips with the same identifier? (AS2)

Posted Dec 14, '13 at 2:31pm

weirdlike

weirdlike

1,038 posts

Moderator

I think this might be a case of

{ code in here }

and you might have

{ code in here } }

check the full code

 

Posted Dec 14, '13 at 2:36pm

mightybob

mightybob

369 posts

The code has no errors now but doesn't work. WTH IS WRONG WITH IT!?

var object:MovieClip = coins.getChildAt(i);

onEnterFrame = function() {

for(var i:Number = 0; i < coins.numChildren;i++)
{
if(char.hitTest(coins.getChildAt(i))==true)
{
trace(coins.getChildAt(i));
trace("YOU WIN");
object = coins.getChildAt(i);
if(object == coin)
{
coin._x = 100000;
trace("YOU WIN");
}
}
}

}

 

Posted Dec 14, '13 at 2:43pm

weirdlike

weirdlike

1,038 posts

Moderator

//you havent declaired the variable i
var object:MovieClip = coins.getChildAt(i);

onEnterFrame = function() {

//here is where the variable i is declaired
for(var i:Number = 0; i < coins.numChildren;i++)
{
if(char.hitTest(coins.getChildAt(i))==true)
{
trace(coins.getChildAt(i));
trace("YOU WIN");
object = coins.getChildAt(i);
if(object == coin)
{
coin._x = 100000;
trace("YOU WIN");
}
}
}

}

 

Posted Dec 14, '13 at 2:45pm

weirdlike

weirdlike

1,038 posts

Moderator

onEnterFrame = function()
{
for(var i:Number = 0; i < coins.numChildren;i++)
{
trace(coins.getChildAt(i));
}
}

do this first and does it trace the coins?

 

Posted Dec 14, '13 at 2:50pm

mightybob

mightybob

369 posts

what is the point of for? why not use if's? is it possible that for is the problem?

 

Posted Dec 14, '13 at 3:05pm

weirdlike

weirdlike

1,038 posts

Moderator

i just googled "for loops explained" and found this HERE

basically the for loop runs more times than the if pending the structure

for (counter; condition; action){
statements;
}

 

Posted Dec 14, '13 at 11:52pm

arobegamr

arobegamr

130 posts

You said you're using AS2?

You can't use getChildAt(), the function is not present in ActionScript2 except for accordion components.

My advice would be to create an array called coins.
var coins:Array = new Array();

When you create the movie clips, push them into the array
coins.push(newCoin);

In your main loop, create a for loop which checks for each item in the array
for(i=0;i

 

Posted Dec 15, '13 at 12:02am

arobegamr

arobegamr

130 posts

Apologies, I accidentally hit the submit button.

In your main loop, create a for loop which check for each item in the array
[i]for(i=0;i

 

Posted Dec 15, '13 at 12:08am

arobegamr

arobegamr

130 posts

Apologies again, it seems html is getting in my way

In your main loop, create a for loop which checks for each item in the array

for(i=0;i &lt coins.length; i++) {
object = coins[i];
if(char.hitTest(object)) {
object.removeMovieClip();
coins.splice(i,1);
i--;
trace("You win");
}
}


Splice prevents the program from checking for a coin which is no longer there by removing its place in the array. Because all elements thereafter get shifted down, you need to decrease i to avoid skipping the next element.

 

Posted Dec 15, '13 at 9:21am

mightybob

mightybob

369 posts

Thanks, but really, this is the first time I have worked with things like this. I need an explanation because or else I can't get it to work.

Here is the code:

var coins:Array = new Array();

coins.push(newCoin);

onEnterFrame = function() {

for(var i:Number = 0; i < coins.numChildren;i++)
{
for(i=0;i < coins.length; i++) {
object = coins[i];
if(char.hitTest(object)==true) {
object.removeMovieClip();
coins.splice(i,1);
i --;
trace("You win");
}
}
}

}

The coin has an identifier of newCoin, it is in the main timeline also with this code.

It is probs because I am getting the names in your code mixed up.

Thanks for all the help

 
Reply to Hittesting multiple movie clips with the same identifier? (AS2)

You must be logged in to post a reply!