Skip to main content
adriana85
Participant
September 25, 2017
Answered

Help with back and forth bounce in action script in Animate.

  • September 25, 2017
  • 3 replies
  • 650 views

I am new to action script in animate and did a tutorial on bouncing an object (plane) from left to right to left. How do I make it bounce back to the right? I'm stumped. Here is the code I used (shared by tutorial).. I just don't know how to write the code to make it bounce to the right again. Any help is appreciated. Thanks!

import flash.events.Event;

import flash.events.MouseEvent;

var plane:Plane = new Plane();

var i:int = 0; //

plane.x=0;

plane.y=100;

addChild(plane);

stage.addEventListener(MouseEvent.CLICK, changeSpeed);

function changeSpeed(event:MouseEvent){

i = 5;

}

plane.addEventListener(Event.ENTER_FRAME,movePlane);

function movePlane (event:Event){

event.target.x += i;

if (event.target.x > stage.stageWidth){

event.target.scaleX = -1;

i *= -1;

}

}

    This topic has been closed for replies.
    Correct answer RandomlyFish

    Right now the code only have a way to make it bounce on the right wall, which is this part:

    if (event.target.x > stage.stageWidth){

         event.target.scaleX = -1;

         i *= -1;

    }

    That checks if the X position of the plane is higher than the width of the stage. The X position for the plane at the far left is 0, so to make it switch direction when it goes all the way left, you would need to check if the plane's X position is less than 0.

    As Colin mentioned this could be written like this instead:

    if (event.target.x > stage.stageWidth || event.target.x < 0){

         event.target.scaleX = -1;

         i *= -1;

    }

    This will cause the code to trigger when both the plane is far left and right. "i *= -1" will cause the value of i to switch between negative and positive every time that code is triggered. However, "event.target.scaleX = -1" will only make the plane point to the left every time it is triggered. So that part will need to be updated to this: "event.target.scaleX *= -1"

    Another problem with the code is this:

    function changeSpeed(event:MouseEvent){

         i = 5;

    }

    Because i should be negative while the plane is flying left, this will cause the plane to fly backwards, if it is flying to the left. While this could be fixed by changing how i is changed, I would recommend using two different variables, one for direction and one for speed. the direction variable would be switch between 1 and -1 depending on the direction, and speed would always be a positive value, that would be increased to make the plane faster.

    The movePlane function would then look like this:

    function movePlane (event : Event) {

         event.target.x += speed * direction;

         if (event.target.x > stage.stageWidth || event.target.x < 0) {

              direction *= -1;

              event.target.scaleX = direction;

         }

    }

    3 replies

    RandomlyFishCorrect answer
    Inspiring
    September 25, 2017

    Right now the code only have a way to make it bounce on the right wall, which is this part:

    if (event.target.x > stage.stageWidth){

         event.target.scaleX = -1;

         i *= -1;

    }

    That checks if the X position of the plane is higher than the width of the stage. The X position for the plane at the far left is 0, so to make it switch direction when it goes all the way left, you would need to check if the plane's X position is less than 0.

    As Colin mentioned this could be written like this instead:

    if (event.target.x > stage.stageWidth || event.target.x < 0){

         event.target.scaleX = -1;

         i *= -1;

    }

    This will cause the code to trigger when both the plane is far left and right. "i *= -1" will cause the value of i to switch between negative and positive every time that code is triggered. However, "event.target.scaleX = -1" will only make the plane point to the left every time it is triggered. So that part will need to be updated to this: "event.target.scaleX *= -1"

    Another problem with the code is this:

    function changeSpeed(event:MouseEvent){

         i = 5;

    }

    Because i should be negative while the plane is flying left, this will cause the plane to fly backwards, if it is flying to the left. While this could be fixed by changing how i is changed, I would recommend using two different variables, one for direction and one for speed. the direction variable would be switch between 1 and -1 depending on the direction, and speed would always be a positive value, that would be increased to make the plane faster.

    The movePlane function would then look like this:

    function movePlane (event : Event) {

         event.target.x += speed * direction;

         if (event.target.x > stage.stageWidth || event.target.x < 0) {

              direction *= -1;

              event.target.scaleX = direction;

         }

    }

    Legend
    September 25, 2017

    I don't see how doubling the number of variables required to store an object's velocity is an improvement. That would be an unusual, memory-wasting approach.

    The speed-changing function can stay a one-liner, just a slightly more complex one--

    function changeSpeed(event:MouseEvent) {

        i = 5 * (i == Math.abs(i) ? 1 : -1);

    };

    Colin Holgate
    Inspiring
    September 25, 2017

    It does seem to be missing some code. This bit:

    if (event.target.x > stage.stageWidth){

    ought to be:

    if (event.target.x > stage.stageWidth || event.target.x < 0){

    Ussnorway7605025
    Legend
    September 25, 2017

    moved to Animate forum