Flash Tutorial - Bouncing Ball Physics (AS 2.0) Part 2

1 Views
howto
howto
06 Dec 2019

(Part 2) In this Flash ActionScript Tutorial you will learn how to make a bouncing ball with simulated gravity using ActionScript.

Here is the revised code. Make sure your drawing is in the center of your MovieClip and make sure to name your instance of your MovieClip to "ball"

(Note: YouTube doesn't allow 'less than' or 'greater than' symbols in comments, so make sure you change the '>' and the '<' to the proper symbols.)

ball.yVelocity = -15;
ball.xVelocity = 5;
gravity = 1;

ball.onEnterFrame = function() {
this.yVelocity += gravity;
this._x += this.xVelocity;
this._y += this.yVelocity;

if(this._y >= Stage.height - (this._height/2)) {
this._y = Stage.height - (this._height/2);
this.yVelocity *= -1; //multiply by negative 1 to reverse velocity
}

if(this._x >= Stage.width - (this._width/2)) { //stage width minus radius of the ball
this._x = Stage.width - (this._width/2); //set position to right side of stage minus radius
this.xVelocity *= -1; //multiply by negative 1 to reverse velocity
}
else if(this._x <= 0 + (this._width/2)) { //zero plus radius of the ball
this._x = 0 + (this._width/2); //set position to zero plus radius
this.xVelocity *= -1; //multiply by negative 1 to reverse velocity
}
}

Show more


0 Comments Sort By

No comments found