ForumsProgramming ForumHittesting multiple movie clips with the same identifier? (AS2)

43 24007
mightybob
offline
mightybob
360 posts
Peasant

Well somewhere I read that the more enterFrame's I use the more lag there will be.

So I am trying to make a lot of things be in 1 enterFrame, like coins.

So I need it to be so that when you hit a coin, it goes away, and you 1 point, just like it already does but so that I only need 1 enterFrame. Kind of like target objects in As3. But I don't know how to do this in as2â¦

Help?

  • 43 Replies
weirdlike
offline
weirdlike
1,299 posts
Prince

Yes there should only be one loop(unless you need a "mini" loop that gets removed shortly)

how are you adding your movie clips? If everything is all added on the main timeline I think this might actually work for you

for(var i:int = 0; i < root.numChildren;i++)
{
if(HERO_MC.hitTestObject(root.getChildAt(i)))
{
trace(root.getChildAt(i));
}
}

without looking at your code of course. You will need to determine what movie clip is holding all the children. Maybe you have a hero that you add and a level_MC which contains all the enemies ground/walls whatever. "root" would be changed to level_MC

mightybob
offline
mightybob
360 posts
Peasant

I'll try that out, thanks

mightybob
offline
mightybob
360 posts
Peasant

Nope, get the error "Could not load the class or interface 'int""⦠Is int an as3 thing?

mightybob
offline
mightybob
360 posts
Peasant

Wait, changed int to number no errors but how do I access the properties of the object I hit? Like X Y Alpha etc

weirdlike
offline
weirdlike
1,299 posts
Prince

look at the trace that shows up when there is collision, it might just say "coin"

at that point you can get use name and make the function to suit

for(var i:int = 0; i < root.numChildren;i++)
{
if(HERO_MC.hitTestObject(root.getChildAt(i)))
{
var object = root.getChildAt(i);
if(object is coin)
{
//coin function
}
if(object is ground)
{
//ground function
}

}
}

mightybob
offline
mightybob
360 posts
Peasant

Oh, ok, in As2 wouldn't I need to do this though?

var object:MovieClip = getchildstuffblahblahâ¦

Anyways I will try both ways thanks for your help

weirdlike
offline
weirdlike
1,299 posts
Prince

well kinda, it in itself doesn't need to be declared as movieclip as it already is one. Replace the term "object" with anything you like

var interactiveObject = root.getChildAt(i);
if(interactiveObject is coin)

mightybob
offline
mightybob
360 posts
Peasant

ah, ok

mightybob
offline
mightybob
360 posts
Peasant

What the heck am I doing wrong here!?

Here is my code:

onEnterFrame = function() {

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

}

And I get 2 errors:

Unexpected } encountered.

Expected ).

I have triple checked both and am not finding anything wrong

weirdlike
offline
weirdlike
1,299 posts
Prince

I think this might be a case of

{ code in here }

and you might have

{ code in here } }

check the full code

mightybob
offline
mightybob
360 posts
Peasant

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&quot;
object = coins.getChildAt(i);
if(object == coin)
{
coin._x = 100000;
trace("YOU WIN&quot;
}
}
}

}

weirdlike
offline
weirdlike
1,299 posts
Prince

//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&quot;
object = coins.getChildAt(i);
if(object == coin)
{
coin._x = 100000;
trace("YOU WIN&quot;
}
}
}

}

weirdlike
offline
weirdlike
1,299 posts
Prince

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

do this first and does it trace the coins?

mightybob
offline
mightybob
360 posts
Peasant

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

weirdlike
offline
weirdlike
1,299 posts
Prince

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

Showing 1-15 of 43