I was trying to make a game with simple spaceships and a bullet that is supposed to hit them and delete them on screen. As all ships are deleted the level gets up by one, increasing number of ships to delete.
I think the collision detection part of my program works fine, but their is some problem in their motion synchronization. I have been trying hard at it but to no advantage… any help will be appreciated. Thanks in advance…!!
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class HitGame extends Sprite
{
private var spaceShips:Array = new Array(); // Array that holds all sapceships
private var bullet:Sprite = new Sprite(); // The Bullet
private var bWidth:Number = 4; // Width of bullet
private var bHeight:Number = 12; // Height of bullet
private var sWidth:Number = 20; // Width of spaceship
private var sHeight:Number = 10; // Height of spaceship
private var bSpeed:Number = 1; // Initial speed of bullet
private var evenSpaceShipSpeed:Number= 3; // Speed of left->right space ship
private var oddSpaceShipSpeed:Number = -3; // Speed of right->left space ship
private var curLevel:Number = 1; // Current level of game (start should be 1)
private var killedList:Array = new Array(); // Recording all dead space ships
private var speedMultiplier:Number = 1.1; // Multiplier of speed each times it gets out of bound
private function makeBullet():void // draws one bullet
{
bullet.graphics.lineStyle(2);
bullet.graphics.beginFill(0x000000);
bullet.graphics.drawRect(-bWidth,-bHeight,bWidth,bHeight);
bullet.graphics.endFill();
}
private function makeSpaceShips():Sprite // Draws a space ship
{
var spaceShip:Sprite = new Sprite();
spaceShip.graphics.lineStyle(2);
spaceShip.graphics.beginFill(0x000000);
spaceShip.graphics.drawRoundRect(-sWidth/2,-sHeight/2,sWidth,sHeight,10);
spaceShip.graphics.endFill();
return spaceShip;
}
private function makeAllSpaceShips():void
{
var i:uint;
var spaceShip:Sprite = new Sprite();
for(i=0; i<curLevel; i++) // No of ship = No of level
{
spaceShip = makeSpaceShips(); // draw one space ship
spaceShips.push(spaceShip); // push drawn space ship into array
}
evenSpaceShipSpeed=3;
oddSpaceShipSpeed= -3;
iniSpaceShip(); // Assign coordinates to space ships
enableHitting(); // Make space ships hit bullets
}
private function onReleaseBullet(event:Event):void
{
// Managing speed of bullet
bullet.y -= bSpeed;
bSpeed++; // bullet gradually accelerates
if(bullet.y<0) // Bullet out of screen
{
bSpeed = 1; // reset speed
removeChild(bullet); // hide bullet
bullet.removeEventListener(Event.ENTER_FRAME, onReleaseBullet); // stop bullet motion
iniBullet(); // make another bullet
}
}
private function releaseBullet(event:MouseEvent):void
{
addChild(bullet); // after click bullet adds to screen
bullet.x = mouseX; // from where the bullet starts
stage.removeEventListener(MouseEvent.CLICK, releaseBullet);
// Only one bullet at a time
bullet.addEventListener(Event.ENTER_FRAME, onReleaseBullet);
// Moving the bullet
}
private function iniBullet():void // initializing bullets
{
bullet.y = stage.stageHeight + bullet.width/2;
stage.addEventListener(MouseEvent.CLICK, releaseBullet); // releasing bullet
}
private function makeBorders():void // making border of the game
{
graphics.lineStyle(2);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
}
private function onMoveEvenSpaceShip(event:Event):void
{
event.currentTarget.x+=evenSpaceShipSpeed;
if((event.currentTarget.x + event.currentTarget.width/2)> stage.stageWidth)
{
evenSpaceShipSpeed*=-speedMultiplier;
event.currentTarget.y+= event.currentTarget.height + 5;
}
if(event.currentTarget.x - event.currentTarget.width/2 < 0)
{
evenSpaceShipSpeed = speedMultiplier*Math.abs(evenSpaceShipSpeed);
event.currentTarget.y+= event.currentTarget.height + 5;
}
}
private function onMoveOddSpaceShip(event:Event):void
{
event.currentTarget.x+=oddSpaceShipSpeed;
if((event.currentTarget.x -event.currentTarget.width/2 ) < 0)
{
oddSpaceShipSpeed = speedMultiplier*Math.abs(oddSpaceShipSpeed)
event.currentTarget.y += event.currentTarget.height + 5;
}
if((event.currentTarget.x + event.currentTarget.width/2)>stage.stageWidth)
{
oddSpaceShipSpeed*=-speedMultiplier;
event.currentTarget.y+= event.currentTarget.height + 5;
}
}
private function iniSpaceShip():void // giving coordinates to newly born ships
{
var i:uint;
var yOffSet:uint = 0;
for (i=0; i<curLevel; i++)
{
addChild(spaceShips*);
if(i%2 == 0) // even ships move from left to right
{
spaceShips*.x = spaceShips*.width/2;
spaceShips*.y = spaceShips*.height/2 + yOffSet;
yOffSet += spaceShips*.height +5; // increasing offset values as ships increase
spaceShips*.addEventListener(Event.ENTER_FRAME, onMoveEvenSpaceShip);
}
else // odd ships move from right to left
{
spaceShips*.x = stage.stageWidth - spaceShips*.width/2;
spaceShips*.y = spaceShips*.height/2 + yOffSet;
yOffSet += spaceShips*.height +5;
spaceShips*.addEventListener(Event.ENTER_FRAME, onMoveOddSpaceShip);
}
}
}
private function checkLeftEnemies():void
{
if(killedList.length == curLevel) // if all ships are dead
{
stage.removeEventListener(Event.ENTER_FRAME, onHit); // disabling hits
var i:uint;
for(i=0; i<curLevel; i++)
{
spaceShips.pop(); // deleting all the previous space ships
killedList.pop(); // refreshing records
}
curLevel++; // increasing level of game
makeAllSpaceShips(); // making new space ships
}
}
private function onHit(event:Event):void
{
var noSpaceShips:uint = spaceShips.length;
var i:uint;
for(i=0; i<noSpaceShips; i++)
{
if(spaceShips*.hitTestObject(bullet))
{
removeChild(spaceShips*); // gets spaceship of the screen
spaceShips*.x = 10000; // settng the gone spaceship to far coordinates
spaceShips*.y = 10000;
killedList.push(i); // recording the ships that died
// removing motion event for dead ship
if(i%2 == 0)
{
spaceShips*.removeEventListener(Event.ENTER_FRAME, onMoveEvenSpaceShip);
}
else
{
spaceShips*.removeEventListener(Event.ENTER_FRAME, onMoveOddSpaceShip);
}
// checking if any ship is left, if not change the level
checkLeftEnemies();
}
}
}
private function enableHitting():void // enables hitting of spaceship and bullet
{
stage.addEventListener(Event.ENTER_FRAME, onHit);
}
public function HitGame()
{
makeBorders(); // make borders of the game
makeBullet(); // make the bullet
makeAllSpaceShips(); // make all the space ships as per the level of the game
iniBullet(); /// initialize the bullet
}
}
}