Prevent movement off screen
Hi,
I curretnyl have this code for moving a character left and right on the stage:
onClipEvent (load) {
speed = 15;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
_xscale = -100;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
_xscale = 100;
}
}
Is there a way I can make it so that when the player reaches a certain point at the ede of the screen it will prevent them from moving further i nthat direction (perhaps something where once it reaches a certain distance to the left then only the right key will work)
Thanks
I Add some code with urs!
onClipEvent (load) {
speed = 15;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
preXcor=_x;
_x -= speed;
_xscale = -100;
if (_x<0){
_x=preXcor
}
}
if (Key.isDown(Key.RIGHT)) {
preXcor=_x;
_x += speed;
_xscale = 100;
tot=_x+this._width
if (tot>Stage.width){
_x=preXcor;
}
}
}