this is like a overhead game? such as a maze game?
in which you need to add a code along the lines of :
onClipEvent (load) {
power = 6;
radius = 14;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= power;
}
if (Key.isDown(Key.RIGHT)) {
_x += power;
}
if (Key.isDown(Key.UP)) {
_y -= power;
}
if (Key.isDown(Key.DOWN)) {
_y += power;
}
while (_root.wall.hitTest(_x, _y+radius, true)) {
_y--;
}
while (_root.wall.hitTest(_x, _y-radius, true)) {
_y++;
}
while (_root.wall.hitTest(_x-radius, _y, true)) {
_x++;
}
while (_root.wall.hitTest(_x+radius, _y, true)) {
_x--;
}
}
I'll explain each part for you.
The Power (line 2) it just to make the player move faster than it is said in the Key.down code lines. I put this in to make him run faster. So ignore that.
We skip along a bit blah blah blah.
ok here we are. If you make a movieclip of a square and name it wall, and give it an instance name of wall, then this code will work., here's why:
I check the hit test between the center of the player and the wall before moving him. If I "foresee" the player's center will hit the wall, I don't move him. Then I performed the same hit test as before but not relatively to the center. then I perform the test and move back the player of one pixel in a direction every time the hit test is detected in that direction. Since player's speed is 3, the while loop won't be executed more than 3 times, that is not CPU expensive.
Make sense? ;)