I will go over basic physics with Actionscript,I am using Flash MX 2004,it might work with Flash MX but probably not with Flash 5.
At the end of this tutorial,you will be able to make a fun pong game using very simple physics.
The first thing to know about physics:
Physics are usally related to speed,velocity,gravity,and wind.When physics is mentioned,there must be a certain movement.
Starting with basic physics
Let's have a quick look at basic physics,open flash,draw a ball or whatever.I won't be explaining everything because I consider you already know a bit actionscript,otherwise you shouldn't be pointed to physics or maths.Let's start by stting varaibles for the speed.
So type the following code in your ball converted to a movie clip:
onClipEvent(load)
{
Gravity = 5;
Velocity = 0;
}
This will tell to the computer that if we use the word "Gravity",it will take that as a 5.Now we are going to use (enterFrame) to begin the action,we will want the ball to fall at the speed of velocity,but velocity is 0.The following code will simply make the ball fall in a rate of 5 pixels every frame,but we don't want that.We want the ball to fall faster every frame,as it is in real gravity in our real world.Take for example,a man standing on a little wall,when the man jumps,he will fall in a little amount of time,while another man in a plane,would start falling slowly from the plane and then as the gravity attracts him,he will fall faster and faster.So we are going to use a code for reallistic gravity
type the following code in your ball
onClipEvent(enterFrame)//checks if something happens/changes
{
this._y+=Velocity;//makes the ball fall at the rate of the velocity(0)
Velocity=Velocity+Gravity//adds the gravity to the velocity every frame
}
Note:When I type
//
do not worry,everything written after it will be ignored,it is just so I can explain the code in the code itself
now check out the movie and you will see the ball falling faster
Making a floor for the ball to bounce on
now you can try to add a floor so the ball can bounce,now THAT would be real physic!Add thise code to the ball:
(the floor will be positionned at 400 _y,therefore your stage siwe should be 550x400)
onClipEvent(enterFrame)
{
if(this._y>=400)
{
Velocity*=.7;//Note:if it doesn't work,try this: Velocity*=-.7;
}
}
Proceeding to the pong game
Now that you know basics physics,we will proceed to more complicated physics used in a pong game.When you get the point of physics,it becomes very easy to make block brekers and pong games.
We will not be using gravity for that,but we will in the third physics tutorial.
Now that we know how to make a ball move,we only need to know how to make it change the direction of the speed.Open a new flash document and draw a ball.Convert the ball to a movie clip and give it an instance name of ball.We will first proceed to the variables.
Type this code in the ball:
onClipEvent(load)
{
Yspeed = 5;//makes the Yspeed 5
Xspeed = 5;//makes the Xspeed 5
}
We have the variables set,we will start the real coding.We are going to use a stage of 550x400 and make imaginary wallssurrounding the stage.We will actually reverse the speed of the ball when it hits the imaginary walls.Type this in the ball's actionscript panel:
onClipEvent(load)
{
Yspeed = 5;//makes the Yspeed 5
Xspeed = 5;//makes the Xspeed 5
}
onClipEvent(enterFrame)//checks if something happens
{
this._y-=Yspeed;//the speed of the ball is equal to Yspeed
this._x-=Xspeed;//the speed of the ball is equal to Xspeed
if(this._x>=550)/*checks if the ball reaches the _x point of 550*/
{
this._x=550;/*doesn't let the ball to go through 550*/Xspeed*=-1;/*reverses the speed of the ball*/}
/*now we are going to make the same thing for the 3 other "imaginary walls"*/
if(this._x<=0){
this._x=0;Xspeed*=-1;}
if(this._y>=400)
{
this._y=400;Yspeed*=-1;}
if(this._y<=0)
{
this._y=0;Yspeed*=-1;}
if(this.hitTest(_root.pad.l))
{
Yspeed*=-1;Xspeed*=-1;}
if(this.hitTest(_root.pad.r))
{
Yspeed*=-1;Xspeed}
}
If you test the game,the ball should travel 'round the screen!
We are going to make a paddle for some more interactivity.So draw a paddle,convert it into a Movie Clip and give it an instance name of paddleWe are going to use variables again.
Type this code into the paddle:
onClipEvent(load)
{
moveleft = 0; //Sets moveleft to 0
moveright = 0; //Sets moveright to 0
}
onClipEvent(enterFrame)
{
this._x-=moveleft;
this._x+=moveright;
if(Key.isDown(Key.LEFT))
{
moveleft = 5;}//Sets moveleft to 5
else
{
movemeft=0;}
if(!Key.isDown(Key.LEFT))
{
moveleft=0;}
if(!Key.isDown(Key.RIGHT))
{
moveright=0;}
if(Key.isDown(Key.RIGHT))
{
moveright = 5;}
else
{moveright=0;}
if(this._x>=550){moveright=0;} /*If the paddle's _x position is bigger than 550,it cant move right*/
if(this._x<=0){moveleft=0;}/*If the paddle's _x position is smaller than 0,it cant move left.*/
}
Now we are going to make our game more fun,although it is a bit off topic because the topic is about physics,not games.Anyway,we will start by making a dynamic text box;give it a var of score.
click the first and only frame you have got in the timeline and type this in the Actionscript panel:
score = 0;
that will set our text box to 0.Now draw a little funny thing which the player will have to hit in order to get a beetter score.convert it into a movie clip and give it an instance name of
circle(Note:it doesn't have to be a circle though).We are going to make that every time that the ball hits it,it gives you one point(score)and then it goes to another place.Type this code in the 'circle's' Actionscript panel.
onClipEvent(enterFrame)
{
if(this.hitTest(_root.ball))
{
this._x=random(430);
this._y=random(380);
Yspeed*=-1;
Xspeed*=-1;
score+=1;
}
}
That will make that every time that the ball hits it,the ball changes its direction,the score is added by 1,and tthe object is positionned someewhere else.
Have fun!
Phew!That was my longest post ever!(lol,only 50 characters left!)
I will make the third physic tutorial(block breaker with gravity)later because I havn't got any characters left!(9!)
