What all this here about?
This is a code i regularly use when working on games to make perfect boundaries, its a fake tile based system. The concept is that we move a perfect little box in boundaries of perfect little boxes and have the graphics commanded to follow.
Setup
First you'll want to decide what size your tiles will be, for the sake of this tutorial we'll use 20x20. Right-Click on the stage, select Grid>Edit Grid... Set the grid to 20x20 and tap on show grid and snap to grid. Draw some boundaries on screen and make them a movie clip and give them an instance name of "bounds". Now make a single square and make it a movie clip and give it an instance name of "hero". And finally make a circle into a movieclip and give it the instance name "graphics".
Code
//the following code goes inside the hero clip
onClipEvent (load) {
//the following vars are keycodes so if i choose to change the keys later i can
north = 38;
south = 40;
east = 39;
west = 37;
//these vars are for calling bounds and graphics
bounds = _root.bounds;
graphics = _root.graphics;
//these vars are the important ones that do useful stuff yay!
wait=false;
dir = "S";
speed = 20; //speed should be the same amount as your grids
grafspeed = 4; //alter this one to change the speed of walking
//and the following just makes sure the graphics are where we want them to be when we start
graphics._x = _x;
graphics._y = _y;
}
onClipEvent (enterFrame) {
//You may want to skip down to see the key codes first
if (wait) { //if the player has moved the hero and we now need to move the graphics
if (dir == "E") { //if we're goin east
graphics._x += grafspeed; //then the graphics go east
if (graphics._x>=_x) { //if the graphics are past or equal to the hero
graphics._x = _x; //the graphics eual the hero
wait = false; //allow the player to control again
}
//repeat for other directions
} else if (dir == "W") {
graphics._x -= grafspeed;
if (graphics._x<=_x) {
graphics._x = _x;
wait = false;
}
} else if (dir == "S") {
graphics._y += grafspeed;
if (graphics._y>=_y) {
graphics._y = _y;
wait = false;
}
} else if (dir == "N") {
graphics._y -= grafspeed;
if (graphics._y<=_y) {
graphics._y = _y;
wait = false;
}
}
}
if (wait == false) { //if we're all ready for the player to run around
if (Key.isDown(north)) { //yknow what this means fool
dir = "N"; //we set the dir var to N-north
_y -= speed; //we move the hero up in the grid
if (bounds.hitTest(_x, _y, true)) { //if we hit bounds on our way
_y += speed; //counteract our movement
}
wait = true; //wait for te graphics to catch up
//repeat for other directions
} else if (Key.isDown(south)) {
dir = "S";
_y += speed;
if (bounds.hitTest(_x, _y, true)) {
_y -= speed;
}
wait = true;
} else if (Key.isDown(east)) {
dir = "E";
_x += speed;
if (bounds.hitTest(_x, _y, true)) {
_x -= speed;
}
wait = true;
} else if (Key.isDown(west)) {
dir = "W";
_x -= speed;
if (bounds.hitTest(_x, _y, true)) {
_x += speed;
}
wait = true;
}
}
}
Wrap-Up
You can also add more to the graphics by making multiple frames in the graphics movieclip and labelling them things such as "walkN" "standS" etc etc. then adding
graphics.gotoAndStop("walk"+dir);
in the graphic movement codes and an
}else{
graphics.gotoAndStop("stand"+dir);
at the end of the hero movement code.