Be a Supporter!
Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
AS:Pong - Basics Apr. 17th, 2007 @ 01:48 AM Reply

AS:Main

Well,I know that Mr. Toast covered Pong Physics and Gravity,but I don't think that covers a basic pong game,so I decided to make this.Mind you,it is BASIC,and won't cover different angles and such depending on where the ball hits the paddle/wall.So,here we go!

This code was tested at 50 FPS

First,I will cover setting up the ball.Here is the code you would put into your pong ball.

onClipEvent (load) {
var speed:Number = 1.5;
dir1 = "left";
dir2 = "down";
}
onClipEvent (enterFrame) {
with (this) {
_x<0 ? dir1="right" : nothing;
_x>550 ? dir1="left" : nothing;
_y>400 ? dir2="up" : nothing;
_y<0 ? dir2="down" : nothing;
dir1 == "left" ? _x -= speed : nothing;
dir1 == "right" ? _x += speed : nothing;
dir2 == "up" ? _y -= speed : nothing;
dir2 == "down" ? _y += speed : nothing;
}
}

So,let's break it down to make it easier to understand.

onClipEvent (load) { - This is the ClipEvent for when the frame 'loads'.People usually use this to just set variables,Booleans,and etc because the events will only happen when the frame loads.

var speed:Number = 1.5; - Sets the variable 'speed' to 1.5

dir1 = "left"; - Sets the variable 'dir1' to 'left'
dir2 = "down"; - Sets the variable 'dir2' to 'down'
} - Closes the ClipEvent

onClipEvent (enterFrame) { - A new ClipEvent.The actions within this happen every frame.

with (this) { - This will just make any actions within 'this' be set to work for 'this'(this ball MC)

_x<0 ? dir1="right" : nothing; - This is like an 'if' and 'else' condition in a smaller and less space consuming form. _x<0 is the 'if' condition.The ? will stand as 'then',which will set dir1 to equal "right".The : stands for 'else',but we don't have an 'else' action for this,so I just put nothing.(I get syntax errors whenever I leave out : )More on ?

_x>550 ? dir1="left" : nothing; - Ditto,sets dir1(x) to equal 'left'
_y>400 ? dir2="up" : nothing; - Ditto,sets dir2(y) to equal 'up'
_y<0 ? dir2="down" : nothing; - Ditto,sets dir2 to equal 'down;

dir1 == "left" ? _x -= speed : nothing; - This part is using the same if/else form,but it checks to see if dir is equal to 'left',and if it is,then the ball will start the action _x-=speed,which will make the ball head to the left.

dir1 == "right" ? _x += speed : nothing; - Ditto,checks if dir1(x) is 'right',moves ball to the right

dir2 == "up" ? _y -= speed : nothing; - Ditto,checks if dir2(y) is 'up',and moves ball up

dir2 == "down" ? _y += speed : nothing; - Ditto,checks if dir2 is 'down',and moves ball down

} - Closes the 'with' statement from before

} - Closes the enterFrame ClipEvent

Ok,so if the code is all good and stuff,then the ball should just start to move around the screen at the speed of 1.5(change speed,or test at 50FPS)

So basically,the code above checks to see where the ball is on the stage(_x 0,_x 550,_y 0,_y 400),and depending on where it is at,the variables dir1 and dir2 will be changed to either left,right,up,or down.If dir1 is left,the ball will go left.Same with right.If dir2 is up,the ball will move up.Same with down.

Now make 1 rectangle MovieClip,and convert it to a symbol.Copy+Paste it and move the copied MC to the right side of the screen.Now add this code to your 'Player' paddle.


onClipEvent (enterFrame) {
with (this) {
Key.isDown(Key.UP) ? _y -= 4 : nothing;
Key.isDown(Key.DOWN) ? _y += 4 : nothing;
hitTest(_parent.ball) ? _parent.ball.dir1="right" : nothing;
}
}

This code basically works the same as the other code,as in with the if/else conditions,but this code includes a new code which will check if a certain keycode/key is down,then will set an action depending on if the key is down.It also checks to see if the ball hits the paddle,and then sets the ball's dir1 variable to right so the ball will start to head right.

Key.isDown(Key.UP) ? _y-=4 : nothing; - Checks if the 'up' key is pressed,then starts the actions '_y-=4',which will make the MC(player) start to move up.

Key.isDown(Key.DOWN) ? _y+=4 : nothing; - Same,but checks to see if the key is down,'down',then moves the paddle down.

hitTest(_parent.ball) ? _parent.ball.dir1="right" : nothing; - Checks for a hitTest between the paddle and ball.The _parent code is part of Hierarchy.We have to use this because since we set the statement 'with' is for this,the hitTest thinks we are talking about a hitTest inside of the paddle.Without _parent,the default code is hitTest(this.ball).It's all explained better in the Hierarchy thread.

So,on the enemy paddle,you will add same basic A.I.(Artificial Intelligence)It's not that hard to understand this basic A.I.,it just checks if the _y of the ball is greater or less than the ball of the enemy MC.I will make the speed a bit slower than the ball just so it is possible to win.This code also includes a hitTest like the one above,using _parent and such,but it makes the ball go left instead of right.

So here's the code.(By the way,you need to make the balls instance name 'ball',otherwise this won't work because the paddle won't know what it's _y needs to be greater than)


onClipEvent (enterFrame) {
with (this) {
_parent.ball._y>_y ? _y += 1.25 : nothing;
_parent.ball._y<_y ? _y -= 1.25 : nothing;
hitTest(_parent.ball) ? _parent.ball.dir1="left" : nothing;
}
}

Again,this is all stuff we have seen above,except for the AI part.EXPLANATION!

_parent.ball._y>_y ? _y += 1.25 : nothing; - This checks if the balls _y is greater than the paddles _y,and if it is,it makes the paddle move down(_y+) at the speed of 1.25,half of 1.5(the ball's speed).It doesn't really find the _y of the ball,it just makes the paddle move down at the rate of 1.25 if the balls _y is greater.

_parent.ball._y<_y ? _y -= 1.25 : nothing; - Same as above,but checks if the ball's _y is less than the paddles _y,then makes it move up at 1.25(explained above)

So,this is a basic pong game.

EXAMPLE

Hope this tutorial helped :)

Scoring is not be explained in this.

wat
a compessor wil raise the volume while lowering the db - chronamut

EvanHayes
EvanHayes
  • Member since: Jan. 13, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS:Pong - Basics Apr. 17th, 2007 @ 07:21 AM Reply

its kinda,\ inefficient,ts
condition?code:0
not :nothing
that makes it go twice as slow
which is 4 times as slow as it would be with an if(){}
so from now on in AS:main topics for basic users,use if(){}
but ok tutorial for the BASICS


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.