Flash & Swish  Home Flash & Swish Swish Tutorials Collision Detection
rss

Collision Detection

Author: Craig Lowe More by this author


Example:

Click on the example above.

And use the arrow keys to control the blue box into the red rectangle.

Ok lets start by opening swishmax make the movie whatever width and height you wish.

Collision detection is used in almost every game created with Swish.

But lets first get used to using the code which makes it possible for this to happen.

First create a square and name this obj this will be our character that we are going to control.

Now create a rectangle and name this wall, place wherever you like on the Scene.

Wall and obj need to be set as targets, so make sure the check box is marked that's says target.

Secondly add the following code to obj.

// this tells the object how to react when a key is pressed.
onEnterFrame(includingFirstFrame) {
if (key.isdown(key.LEFT)) {
_x-= +5;
}
if (key.isdown(key.RIGHT)) {
_x+= +5;
}
if (key.isdown(key.DOWN)) {
_y+= +5;
}
if (key.isdown(key.UP)) {
_y-= +5;
//this is for the width of the movie
//12 is the left hand side of the movie and 188 is the right hand side
//this makes the object stop when it reaches these points
}
if (this._x<12) {
this._x=12;
}
if (this._x>188) {
this._x=188;
}
//this is for the height of the movie
//12 is the top of the movie and 188 is the bottom of the movie
//this makes the object stop when it reaches these points
if (this._y<12) {
this._y=12;
}
if (this._y>138) {
this._y=138;
}
//this tells the movie what to do when it colides with something
//in this case it tells the dynamic text box called info to display the text
//Collision Detected, and if their is no collision the text is set to nothing " "
//if you want it to say something when their is no collision put text between " "
if (_root.wall.isNearThis()) {
_root.info="Collision Dectected";
} else {
_root.info=" ";
}
}

Finally Amend the height and width inside the code to fit your movie.

This cane easily be found in the code as it is marked out for you.

Example if (this._y<12) and if (this._y>138)

Add a dynamic text box name info and place in the top right corner.

That's it, now press Ctrl+T to test your movie.




Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Collision Detection"