PDA

View Full Version : Collision Tests with Walls, Obstacles, etc.



JapanMan
June 21st, 2008, 06:54 PM
Very simple question (I'm an AS n00b, I know...):

What's the best way to stop a Movie Clip's movement in a specific direction (ie. when you move into a wall or the side of the stage)?


Thanks!


EDIT: this is Flash MX I'm using.

tapioca
June 22nd, 2008, 12:35 AM
i usually test a point in the direction they are trying to move with hitTest(x + x_speed, y + y_speed) first, and then only move if it's clear. some people i think move, then test, then move back if something is hit.

something like this:



if (Key.isDown(Key.RIGHT)) {
x_speed = 5;
y_speed =0;
}

// more of these for directions

if (_root.clipping_map.hitTest(_x + x_speed, _y + y_speed) == false) {
_x += x_speed;
_y += y_speed;
}

JapanMan
June 22nd, 2008, 07:47 PM
so, in the instance of your code, "map" would be the wall or obstacle, and the code would be in the moving Movie Clip's actions, correct?

tapioca
June 23rd, 2008, 02:47 AM
yeah, clipping_map is just a vector drawing with solid where they can't walk, and you can do onClipEvent(load) { _visible = false; } in it to make it invisible. and the code in the example is attached to the moving movie clip.

uh, i messed up a line tho. it should say:

if (_root.clipping_map.hitTest(_x + x_speed, _y + y_speed, true) == false) {