Skip to main content
Inspiring
February 28, 2016
Answered

flash as3 physics if statement issue :

  • February 28, 2016
  • 1 reply
  • 338 views

When velocity is greater than 4 , the ball should stop . i also tried the code   if(xx>4) { ball.stop(); } inside score . and outside scope . . but does not  work

// set gravity amount

var gravity:Number = .000098;

// set starting velcity

var xx:Number = 0.2;

// mark start time and add listener

var lastTime:int = getTimer();

addEventListener(Event.ENTER_FRAME, animateBall);

// step animation

function animateBall(event:Event) {

  // get time difference

  var timeDiff:int = getTimer()-lastTime;

  lastTime += timeDiff;

  // adjust vertical speed for gravity

  xx+= gravity*timeDiff;

  // move ball

  ball.rotation += timeDiff*xx;

}

  if(xx>4) {

  ball.stop();

  }

This topic has been closed for replies.
Correct answer kglad

that code will stop the timeline of ball, but it won't stop ball from rotating.

if you want to stop ball from rotating when xx>4, use:

// set gravity amount

var gravity:Number = .000098;

// set starting velcity

var xx:Number = 0.2;

// mark start time and add listener

var lastTime:int = getTimer();

addEventListener(Event.ENTER_FRAME, animateBall);

// step animation

function animateBall(event:Event) {

  // get time difference

  var timeDiff:int = getTimer()-lastTime;

  lastTime += timeDiff;

  // adjust vertical speed for gravity

  xx+= gravity*timeDiff;

  // move ball

if(xx<=4){

  ball.rotation += timeDiff*xx;

}

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 28, 2016

that code will stop the timeline of ball, but it won't stop ball from rotating.

if you want to stop ball from rotating when xx>4, use:

// set gravity amount

var gravity:Number = .000098;

// set starting velcity

var xx:Number = 0.2;

// mark start time and add listener

var lastTime:int = getTimer();

addEventListener(Event.ENTER_FRAME, animateBall);

// step animation

function animateBall(event:Event) {

  // get time difference

  var timeDiff:int = getTimer()-lastTime;

  lastTime += timeDiff;

  // adjust vertical speed for gravity

  xx+= gravity*timeDiff;

  // move ball

if(xx<=4){

  ball.rotation += timeDiff*xx;

}

}