ForumsProgramming ForumHelp, hittest in AS3 driving me nuts

4 5381
Raudius
offline
Raudius
12 posts
Nomad

I have a shooting game. And I obviously want the bullet to hit things and make damage.

So, all I need is a Hittest code for AS3.

I thought about putting this code inside the bullet

if (this.hitTestObject(enemy)) {
...........;
}

but it gives me the Implicit Coertion error.

Can someone help me?

  • 4 Replies
Midmenj
offline
Midmenj
216 posts
Nomad

look at this site [url=http://www.flashgametuts.com/] and look for the shooter section. it should help you.

driejen
offline
driejen
486 posts
Nomad

have you tried;

if (this.hitTestObject(_root.enemy)) {
...........;
}

It might be trying to test for collission with a movieclip named enemy within the bullet movieclip, when in fact you want to test for collission with a movieclip that is placed on the main timeline. Im not sure why it gives you an Implicit Coertion error though, perhaps there is an error within the code placed inside your hitTest.

driejen
offline
driejen
486 posts
Nomad

sorry didnt realise your working on AS3, I think its better if you test for collisions in the document class or a class that commands both the enemy and the avatar. Make an array to hold the enemies and another array to hold the bullets.

On the constructor function, you would create the arrays to hold them, and within the function that checks their collosion you would use 'for each()' to test all the movieClips within your arrays. I wish I could elaborate but I havent studied AS3 classes much. You could read this as it explains collision testing for multiple MovieClips.

PrideRage
offline
PrideRage
148 posts
Nomad

Because you're coding in AS3, you can't use _root or this.
You need to take the instance names.
Example:
if(bullet.hitTestObject(enemy)){
....;
}

Showing 1-4 of 4