3 Replies Latest reply on Nov 1, 2007 8:44 AM by kglad

    Help with code...emergency

    solstickan
      Hi!

      We are making a game in school, it's a boat crossing the ocean.
      We have a problem that the boat doesn't stop when it gets to the end of the screen, it just continues moving away.

      So, If anyone can help us with the code that makes the boat stop inside the border... PLEASE

      This is the size of the border that we want the boat to stay inside:

      W: 4381.4 X: -2190.7
      H: 2352.0 Y: -1176.0


      This is the code we have now:

      on (keyPress "<Left>")
      {
      this._x = this._x + this._parent.boatSpeed;
      }

      on (keyPress "<Right>")
      {
      this._x = this._x - this._parent.boatSpeed;
      }

      on (keyPress "<Up>") {
      this._y = this._y + this._parent.boatSpeed;
      }

      on (keyPress "<Down>") {
      this._y = this._y - this._parent.boatSpeed;
      }




      Thank you in advance!!
        • 1. Re: Help with code...emergency
          kglad Adobe Community Professional & MVP
          use an if-statement so you only update the boat's position when it's within your boundaries.
          • 2. Re: Help with code...emergency
            solstickan Level 1
            Hi!

            Thanks for your answer.... I've tried this code...but I can't get it to work... can you see what is wrong??


            var top:Number=0;
            var left:Number=0;
            var right:Number=500;
            var bottom:Number=500;

            on (keyPress "<Left>")
            {
            if(this._x<left)
            {
            this._x=left;
            }else
            {
            this._x = this._x + this._parent.boatSpeed;
            }
            }


            on (keyPress "<Right>")
            {
            if(this._x>right)
            {
            this._x=right;
            }else
            {
            this._x = this._x - this._parent.boatSpeed;
            }
            }

            on (keyPress "<Up>") {
            if(this._y<top)
            {
            this._y=top;
            }else
            {
            this._y = this._y - this._parent.boatSpeed;
            }
            }

            on (keyPress "<Down>") {
            if(this._y>bottom)
            {
            this._y=bottom;
            }else
            {
            this._y = this._y + this._parent.boatSpeed;
            }
            }

            • 3. Re: Help with code...emergency
              kglad Adobe Community Professional & MVP
              don't you have your left and right keys switched? and are your buttons movieclip buttons? if so, use:

              this._parent.left; // etc instead of left within your button handlers.