• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Sep 24, 2017 Sep 24, 2017

Copy link to clipboard

Copied

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;

}

}

Views

438

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Sep 25, 2017 Sep 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 (eve

...

Votes

Translate

Translate
LEGEND ,
Sep 24, 2017 Sep 24, 2017

Copy link to clipboard

Copied

moved to Animate forum

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 24, 2017 Sep 24, 2017

Copy link to clipboard

Copied

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){

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

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;

     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

LATEST

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

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines