AS MAIN
AS: Bouncing ball
Example
This tutorial shows you how to make a cool bouncy ball you control with the arrow keys, and will teach you how the AS works, alternatively, you can just c/p the code into flash, but you won't learn much by doing that, will you?
Okay, so firstly, create a movieClip, in the movie clip draw a circle. Make sure the little + is in the middle of the ball. Then, go back onto the main timeline and insert this AS into the ball:
onClipEvent(load){
grav = 1;
speed = 0;
_root.score = 0;
/*This sets the variables grav, speed and score. You should understand variables to use this code, if you don't, go find a tutorial on them*/
}
onClipEvent(enterFrame){
if(this._rotation > 0){
this._rotation-=4;
}if(this._rotation < 0){
this._rotation+=4;
}
}
/* This code means that if the ball has a rotation of lower than 0, the ball will rotatation will increase by 4 points every frame, while if the rotation is higher than zero, it will decrease 4 points every frame*/
onClipEvent(enterFrame){
if(speed > 0){
speed--;
}
if(speed < 0){
speed++;
}
if(grav < 0){
grav++;
}
}
/*This does the same as the previous block of code, except it changes the speed and grav variables instead of the rotation*/
onClipEvent(enterFrame){
grav++;
this._y+=grav;
this._x+=speed;
this._rotation+=rotate;
rotate = (speed);
}
/* This code makes it so every frame, the variable grav goes up by one, it also makes it so that the speed the ball moves left of right is determined by the variable speed, and the speed the ball falls is determined by the variable grav */
onClipEvent(enterFrame){
if(grav > 30){
grav =30;
}
if(grav < -30){
grav = -30;
}
if(speed > 30){
speed = 30;
}
if(speed < -30){
speed = -30;
}
/*This code makes it so if the variable grav is above 30, it goes back down to 30, and if it is below -30, it will go back up to -30, it does it with the variable speed too, this is so the ball dosen't move too fast.*/
if(Key.isDown(Key.UP)){
grav-=3;
}
if(Key.isDown(Key.LEFT)){
speed-=2;
}
if(Key.isDown(Key.RIGHT)){
speed+=2;
}
}
/*This code says that if the UP key is down, the variable grav goes down by 3 every frame, if you remember the grav variable determines how fast the ball falls, if its in the minus numbers to ball will go up, it also makes it so the left and right keys make the speed variable go up and down, as the speed variable determines how fast the ball goes left/right*/
onClipEvent(enterFrame){
if(this._x < 0){
this._x = 0;
speed = speed - (speed *2);
}
if(this._x > 550){
this._x = 550;
speed = speed - (speed * 2);
}
if(this._y < 0){
this._y = 0;
grav = grav - (grav * 2);
}
if(this._y > 400){
this._y = 396;
grav = grav - (grav * 2.2);
}
}
/* This final bit of code basically means that when a ball hits the side, it will bounce off it, this code assumes the flash document is 550x400. I will explain a bit more, basically, the first bit says that if the balls _x position of 0, the speed variable = speed - (speed times 2), so if speed was 2, and the balls x position was 0, speed would then be 2 - (2 times 2) which is 2 minus 4, which is -2, so the ball would bounce back off the wall*/
You may be able to tell that this is my first tutorial I am writing, but I hope it helps someone :)
