A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Prevent movement off screen

Hybrid View

  1. #1
    Member
    Join Date
    Jun 2005
    Posts
    45

    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

  2. #2
    Junior Member
    Join Date
    Feb 2007
    Posts
    3

    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;
    }


    }
    }

  3. #3
    Member
    Join Date
    Jun 2005
    Posts
    45
    That works great thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center