Search tutorials
This is a REALLY easy hit test script for a wall
First, Create the right wall (this script only deals with the left and right walls). Make it a straight line. Highlight the line and press f8, now select the function Movie Clip (here after refered to as MCs). Now you have you first wall, give it the instance name wallr by going into properties and changing the instance name. Position this line at the right of the stage. Make another wall MC and give it the instance name walll (yes, with the 3 L's). Position this MC at the left of the stage.
NOTE: These MCs do NOT need to have any scripts applied to them.
Now create your player MC (it can be a simple square) and give it the following actions.
onClipEvent (load) {
speed = 10
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
}
This is the script that makes the MC move. Unfortunately, the MC can still wander off the screen. To fix this we add another simple bit of code to the end of what we already have.
onClipEvent (enterFrame) {
if (this.hitTest(_root.Walll)) {
_x = _x+10;
}
if (this.hitTest(_root.Wallr)) {
_x = _x-10;
}
}
Add this code AFTER the movement code. This code basically just says when the character touches the wall, move the character 10px back.
You now have a simple hit test script that can be manipulated to do a whole slew of other things.
The final code should be as follows:
onClipEvent (load) {
speed = 10
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
}
onClipEvent (enterFrame) {
if (this.hitTest(_root.Walll)) {
_x = _x+10;
}
if (this.hitTest(_root.Wallr)) {
_x = _x-10;
}
}
| » Level Basic |
|
Added: 2005-12-21 Rating: 5 Votes: 13 |
| » Author |
| Name: Sean Twist Location: Canada Uses: Flash MX, MX 2004, and 8 |
| » Download |
| Download the files used in this tutorial. |
| Download (0 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


Comments
There are no comments yet. Be the first to comment!