PDA

View Full Version : HitTest Help


MichaelPot
02-22-2008, 12:19 AM
I am creating a game where the user gets to choose from a variety of cars, planes, and other vehicles. You get to drive around using the keys, but I need walls. The following code works for one of my cars, I need a way to for it to apply to all the vehicles. I also would like the walls to be on one layer, and one extended frame, so I don't have to change the walls on every frame when I decide to change it.

onClipEvent (enterFrame) {
if (this.hitTest(_root.beetleCAR)) {
this._y-- ;
}
}

I tried putting the walls for each vehicle on their own frame, but the second code which was:

onClipEvent (enterFrame) {
if (this.hitTest(_root.phantomJET)) {
this._y-- ;
}
}

Yet I then get the error even though it is basically the same. So basically I am trying to combine these two codes together.

Please can you help me.

madmac66
02-22-2008, 12:46 AM
is this code on the walls or the cars?

might be best to have hit test on cars all checking to see if they are hitting the same object, rather than have the wall check to see if hitting multiple objects...

make sense? Maybe you are already doing that and I misunderstood. Post more detail

MichaelPot
02-22-2008, 01:21 AM
I have the code on the wall, and I shall try putting it on the cars. Thanks!

MichaelPot
02-22-2008, 01:28 AM
It works! thanks for the adive, as I never thought of doing it the way you said. thanks again.

rchrdms@aol.com
02-25-2008, 11:27 PM
Hi,

I totally agree with Madmac66.

You should put the hitTest on the objects(plane, car, etc.) to see if they hit the wall.

Try this:

MovieClip.prototype.checkWall = function(toHit, functionToRun) {
if (this.hitTest(toHit)) {
functionToRun(this);
}
};
hitFunction = function () {
trace("a hit!");
};
object1.onEnterFrame = function() {
this.checkWall(wall, hitFunction);
};
object2.onEnterFrame = function() {
this.checkWall(wall, hitFunction);
};


"wall" is the instance name of the wall MC; "object1", etc. are the names of the vehicles you play.

I tried this code with the "wall" clip testing the other objects and it just didn't work.

PS. I got this code from an old ed. of Computer Arts on gaming.