Replace the move functions with the following script:
function moveLeft():Void {
var leftWall:Number = 0;
if(target._x > leftWall) {
target._x -= SPEED;
}else {
target._x = leftWall;
}
}
function moveRight():Void {
var rightWall:Number = Stage.width - target._width;
if(target._x < rightWall) {
target._x += SPEED;
}else {
target._x = rightWall;
}
}
function moveUp():Void {
var topWall:Number = 0
if(target._y > topWall) {
target._y -= SPEED;
}else {
target._y = topWall;
}
}
function moveDown():Void {
var bottomWall:Number = Stage.height - target._height;
if(target._y < bottomWall) {
target._y += SPEED;
}else {
target._y = bottomWall;
}
}
In each function, we are calculating the edge of our stage and making sure that the target clip hasn't gone too far. If it has, then we position it up against the edge. The edge values are represented by the
topWall,
bottomWall,
leftWall, and
rightWall properties. This is a simple example of an
if/else statement.