|
Simple Ping Pong with AS3
Overview:
This ping pong game is simple and straigthfoward.
There are other commands in Actionscript 3.0 that help us do some of the math
that is done in the code below, however,
to give a more clear feel for the logic, this example spells out the math.
- We begin by declaring our package and importing the needed classes.
- The [SWF...] part tells the compiler the frameRate, width, and height of the swf it will make.
In this casg the background color is determined by the wrapper the swf file is in.
- Now the main class is defined.
Before we define the swf files main function we declare all but one of our variables.
In this example only the draglock variable is not global.
- The pongstart function gives shape to our sprites,
adds events to the paddle sprites, starts the compfocus timer that will
act as the computers brain, and adds all sprites to the stage.
- The changeOfErrorRatio function changes the error ratio of the computer paddle.
The higher the error ratio, the more stupid the computer will be.
The error ratio gets greater as the velocity of the ball gets greater, in other words,
the computer has a harder time keeping up with the ball as the ball gets faster.
This function is happening on its own timer (compFocus) every 100 milliseconds.
- The movementGovern function is the main opperational function of this file.
It is happening on enter frame of the main timeline.
Because movementGovern is always happening,
the if thens and statements within are more like declarations:
a. The balls x and y position are equal to the direction values multiplied by velocity then divided by 50.
b. If the balls x or y positions become greater than the set x and y ranges,
which include both paddles and the square bounderies,
then the balls direction should change to its opposite and velocity should increase.
c. If' the balls x and y are greator than any range or hit any boundary, play the ding sound.
d. If the balls y position is past the computer paddle and not touching the computer paddle,
then give the player a point, and the same for the player paddle, give the computer a point.
e. When velocity is greater than zero subtract velocity by one.
f. If velocity becomes 501 bring velocity to 480. g. When the ball is in range,
the x position of the computer paddle is the x position of the ball plus the error ratio diveded by two, minus half the width of the computer paddle minus the error ratio diveded by eight.
- The updatescore function is used within the movegov function to update the score.
- The paddle grab function is the event that happens on mouse down of the player paddle.
In this function if the velocity is zero it is changed to 300, initializing ball movement.
- The paddlerelease function happens when the mouse is released over the player paddle.
To compile this file you will need to create your own ding sound mp3 file and
change the embed folder (../exampimages) to the folder where your mp3 is.
Or you can take out the embed code, including the private var theding,
and take out the phrase theding.play(); from the code.
package
{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.utils.Timer;
import flash.media.Sound;
/* a (t)a.d as3 tutorial
copyright 2008 (t)a.d */
[SWF(frameRate = '20', width = '580', height = '400')]
public class PongGame extends Sprite
{
private var dirX:Number = 2;
private var dirY:Number = 2;
private var velocity:Number = 0;
private var theBall:Sprite;
private var basicBounds:Rectangle = new Rectangle(25, 25, 530, 350);
private var showBounds:Sprite;
private var compScore:Number = 0;
private var userScore:Number = 0;
private var playerPaddle:Sprite;
private var compPaddle:Sprite;
private var showScore:TextField;
private var errorRatio:Number = Math.random()*5;
private var compFocus:Timer;
[Embed(source="../exampimages/poing2.mp3")]
private var Dinger:Class;
private var theDing:Sound = new Dinger();
public function PongGame()
{
compFocus = new Timer(100, 0);
compFocus.addEventListener(TimerEvent.TIMER, changeOfErrorRatio);
showBounds = new Sprite();
showBounds.graphics.lineStyle(3, 0xFF0000);
showBounds.graphics.drawRect(25, 25, 530, 350);
addChild(showBounds);
showScore = new TextField();
showScore.text = "Computer: "+ compScore + "\n" + "You: " + userScore;
showScore.x = 35;
showScore.y = 35;
addChild(showScore);
theBall = new Sprite();
theBall.graphics.lineStyle(1, 0x000000);
theBall.graphics.beginFill(0xFF0000);
theBall.graphics.drawCircle(0, 0, 15);
theBall.graphics.endFill();
theBall.x = 580/2 - theBall.width/2;
theBall.y = 400/2 - theBall.height/2;
addChild(theBall);
playerPaddle = new Sprite();
playerPaddle.graphics.lineStyle(1, 0x000000);
playerPaddle.graphics.beginFill(0xC0C0C0);
playerPaddle.graphics.drawRect(0, 0, 100, 17);
playerPaddle.x = 30;
playerPaddle.y = 360;
playerPaddle.buttonMode = true;
playerPaddle.addEventListener(MouseEvent.MOUSE_DOWN, paddleGrabHandler);
playerPaddle.addEventListener(MouseEvent.MOUSE_UP, paddleReleaseHandler);
addChild(playerPaddle);
compPaddle = new Sprite();
compPaddle.graphics.lineStyle(1, 0x000000);
compPaddle.graphics.beginFill(0xFE0000);
compPaddle.graphics.drawRect(0, 0, 100, 17);
compPaddle.x = 30;
compPaddle.y = 22;
addChild(compPaddle);
stage.addEventListener(Event.ENTER_FRAME, movementGovern);
}
private function changeOfErrorRatio(event:TimerEvent):void
{
errorRatio = Math.random()*127 + velocity/5;
}
private function movementGovern(event:Event):void
{
theBall.x += dirX*velocity/50;
theBall.y += dirY*velocity/50;
if (theBall.x > 530)
{dirX = -2;velocity += 5;theDing.play();}
if (theBall.x < 50)
{dirX = 2;velocity += 5;theDing.play();}
if (theBall.y > 350 && !theBall.hitTestObject(playerPaddle))
{dirY = -2;velocity += 5;compScore += 1;updateScore(compScore,userScore);theDing.play();}
if (theBall.y < 45 && !theBall.hitTestObject(compPaddle))
{dirY = 2;velocity += 5;userScore += 1;updateScore(compScore, userScore);theDing.play();}
if (theBall.hitTestObject(playerPaddle))
{dirY = -2; velocity += 40;theDing.play();}
if (velocity>0)
{velocity -= 1;}
else {velocity = 0;}
if (velocity>500)
{velocity = 450;}
if (theBall.x <= 460 && theBall.x >=25)
{compPaddle.x = theBall.x + errorRatio/2 - compPaddle.width/2-errorRatio/8;}
if (theBall.hitTestObject(compPaddle))
{dirY = 2; velocity += 45;theDing.play();}
}
private function updateScore(computer:Number, user:Number):void
{
showScore.text = "Computer: "+ computer + "\n" + "You: " + user;
}
private function paddleGrabHandler(event:MouseEvent):void
{
if (velocity == 0)
{velocity = 300;}
else {velocity = velocity;}
if (compFocus.running == false)
{compFocus.start();}
var dragLock:Rectangle = new Rectangle(20, 360, 435, 0);
event.target.startDrag(false, dragLock);
}
private function paddleReleaseHandler(event:MouseEvent):void
{
if (velocity !=0)
{velocity = 0;}
event.target.stopDrag();
}
}
}
|