Sunday, August 16, 2009

Creating a game with APE - APEOut - Part 3

In the third and final part of this tutorial, we'll finish off this game by adding collision detection and scoring.Before we start, make sure you've read the first and the second part of this series. So, lets get to it.

Adding collision detection :

In order to add collision detection, we'll need to add an Event listener to the ball. After the ball defnition, add the following code :

ball.addEventListener(CollisionEvent.COLLIDE, onCollision);

Now, we have to code what happens if a collision occurs. Create the onCollision event handler as follows :

private function onCollision(e:CollisionEvent):void
{
var collidingItem:* = e.collidingItem;

if (collidingItem != topWall && collidingItem != bat &&
collidingItem != leftWall && collidingItem != rightWall)
{
--brickCount;
group.removeParticle(collidingItem);
}
}


Here the collidingItem will refer to the object that comes in contact with the ball. Now, we have to remove the object if it is a brick or do nothing if its the bat or the walls. So, we check if the collidingItem is not one of the walls or the bat, then we remove it from the group and reduce the brickCount.

Finishing up the game :

Now, run the game, our bat is moving with the mouse, the ball is bouncing and destroying the bricks that it comes in contact with. Everything is working as it is supposed to. But every good game needs scores, right, so declare a score variable as a Number and a TextField to display the score.We also need a Boolean variable to see if the game is over or not. Here's the final full code :

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;

import org.cove.ape.APEngine;
import org.cove.ape.CircleParticle;
import org.cove.ape.CollisionEvent;
import org.cove.ape.Group;
import org.cove.ape.RectangleParticle;
import org.cove.ape.VectorForce;

/**
* APEOut - A Breakout clone using the ActionScript Physics Engine (APE)
* @author Pradeek
*/

[SWF(width = 600, height = 400)]
public class APEOut extends Sprite
{
private var group:Group;
private var leftWall:RectangleParticle;
private var rightWall:RectangleParticle;
private var topWall:RectangleParticle;
private var bat:RectangleParticle;
private var ball:CircleParticle;
private var brickCount:Number;

private var score:Number;
private var scoreText:TextField;
private var gameOver:Boolean;

public function APEOut():void
{
initAPE();
initObjects();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function initAPE():void
{
APEngine.init();
APEngine.container = this;
APEngine.addForce(new VectorForce(false, 0, 4));
}

private function initObjects():void
{
gameOver = false;

score = 0;
scoreText = new TextField();
scoreText.x = 400;
scoreText.y = 25;
addChild(scoreText);

group = new Group(true);

leftWall = new RectangleParticle( -10, 0, 10, 1000, 0, true, 1, 0.3, 1);
rightWall = new RectangleParticle(610, 0, 10, 1000, 0, true, 1, 0.3, 1);
topWall = new RectangleParticle(0, -10, 1200, 10, 0, true, 1, 0.3, 1);

bat = new RectangleParticle(250, 325, 125, 10, 0, true, 1, 0.50, 1);
bat.alwaysRepaint = true;
bat.setFill(0x0000FF);

ball = new CircleParticle(220, 225, 15, false, 1, 0.65, 1);
ball.setFill(0xFF0000);
ball.addEventListener(CollisionEvent.COLLIDE, onCollision);

var rows:int = 5;
var cols:int = 8;

brickCount = rows * cols;

var currentX:Number = 100;
var currentY:Number = 100;

var brickWidth:Number = 50;
var brickHeight:Number = 10;

for (var i:uint = 0; i < uint =" 0;" rectangleparticle =" new" currentx =" 100;" px =" mouseX;" text = "Score : "> 400)
{
gameOver = true;
group.removeParticle(ball);
scoreText.text = "Game Over. \nYou lose. \nFinal Score : " + score.toString();
}

if (brickCount == 0)
{
gameOver = true;
group.removeParticle(ball);
scoreText.text = "Game Over. \nYou win. \nFinal Score : " + score.toString();
}
}
}
}


Hope the series was a bit useful to you. If you have any questions or suggestions, please drop a comment. Thanks for reading :D

No comments:

Post a Comment