A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Problem with boundaries

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    10

    Problem with boundaries

    I've been struggling with making boundaries with my game. I've managed to make boundaries, but in stead of my game character not moving across that border it just bounces back. Which is not the effect I want.

    Here's my code


    onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
    xspeed -= power;
    }
    if (Key.isDown(Key.RIGHT)) {
    xspeed += power;
    }
    if (Key.isDown(Key.UP)) {
    yspeed -= power;
    }
    if (Key.isDown(Key.DOWN)) {
    yspeed += power;


    }


    xspeed *= friction;
    yspeed *= friction;
    _y += yspeed;
    _x += xspeed;

    //BOUNDARIES

    if(this._x <0){
    this._x += 10;
    }
    if(this._x> 720){
    this._x -= 10;
    }
    if(this._y <50){
    this._y += 10;
    }
    if(this._y> 500){
    this._y -= 10;
    }
    }

    Thanks a lot

  2. #2
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    What kind of boundaries are you looking to have?
    Z¡µµ¥ D££

    Soup In A Box

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    I'm sorry.

    It's boundaries so that the player wont be able to go outside the stage (what you can "see").

    And right now with that code, it doesn't really work. It can easily go outside the bounds (but there is some boundry, but the caracter just seem to bounce into the wall. It doesn't look great. I just want it to not be able to go outside the stage so that it stops there instead of bouncing back and forth)

  4. #4
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    if(this._x <0){
    this._x=0;
    }
    if(this._x> 720){
    this._x =720;
    }
    if(this._y <50){
    this._y = 50;
    }
    if(this._y> 500){
    this._y = 500;
    }
    }


    That's one approach. You may have to tweak some numbers, but that's the general concept.
    Z¡µµ¥ D££

    Soup In A Box

Tags for this Thread

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