Hi,
Since this is my first Flash thread here at TS I thought I'd make it worth while.
I have a car, controlled by the arrow keys. I have walls.
Naturally I don't want the car to go through the walls.
I have been researching collision detection and understand the basics, but for some reason I can't achieve the results that I am looking for. With the code I have now, when the car hits the wall it still tries to move forward resulting in a very wrenching collision.
Any help with this would be much appreciated!
Here is some code that ought to work. Just put this on Frame 1 of your main timeline. In the last bit of code, change '_root.myMC...' to the name of your movie clip. You can set the size of your confinment by modifying the _globals.
// Sets global variables for size of bounding box. _global.top = 0; _global.left = 0; _global.bottom = 300; _global.right = 300;
// This code keeps the clip inside the boundaries. function direction () { // If _x is beyond either of the side boundaries... if (this._x > right || this._x < left) { // Change its direction to the opposite direction. this.dirX *= -1; }
// If _y is beyond either of the side boundaries...
if (this._y > bottom || this._y < top) {
// Change its direction to the opposite direction.
this.dirY *= -1;
}
}
// Main function. // This controls the motion of the section movieClips. motion = function () { this._x += this.dirX; this._y += this.dirY;
// Used to check location related to boundaries.
direction.apply (this);
updateAfterEvent ();
}
// Sets base _x/_y directions _root.myMC.dirX = 1; _root.myMC.dirY = 1; // Calls function 'motion' which instansiates the motion of the // section button movieClip. _root.myMC.onEnterFrame = motion;
You do not have to use '_global' and, in fact, you might not want to in case you want to redefine the box size on the fly. This is just code I happened to already have in place and just copied and pasted it in here.
I hope this helps!
I seem to have it working using multiple:
hitTest(x, y, true);
Thanks
I am not sure how many things you have that are needing to detect boundaries, but this last bit of the code I sent:
// Sets base _x/_y directions _root.myMC.dirX = 1; _root.myMC.dirY = 1; // Calls function 'motion' which instansiates the motion of the // section button movieClip. _root.myMC.onEnterFrame = motion;
can be infinitely (well, based on cpu limitations) expanded. That is, all you need to do is swap out 'myMC' with whatever name you assigned to your movie clip(s), regardless of how many you have. Then, if you ever need to change the size of your boundaries, you simply adjust the '_global' values and everyone of your movie clips are automatically updated.
I am not attempting to sell you on this way over any other since actionscript offers many paths to the same destination, but this should afford quite a bit of flexibility to your movie without considerable maintanence if you want to alter your code in the future. I have also read that it is less cpu intensive, although this part I am not positive on.
Anyway, the bottom line is you have it working, which is awesome 
Good luck to you!
quite awhile ago i had a small project doing similar thing to what you are looking to do.
the first thing i can say is that hit detection IS NOT EASY! i spent at least 50% of the time on the project working with the best way to go about it and to this day I am still not satifisfied.
you can view my project here http://www.musicfuel.net/VW
the controls are all with the arrow keys and CTRL will cause the car to skid. if it looks like something close to what you are trying to do, let me know and i'll dig up the code from it.
hope this helps!
Thanks guys. I'm going to be working on the project today so I'll let you know if I hit any brick walls.
I'm pretty confident that I'll be able to make it function how I want it to.
Cheers,