Archive

Archive for the ‘Games’ Category

Car race game in AS3

August 19, 2008 Leave a comment

Here is the car race game that I have just started in AS3.

/*
*    File        :    Main.as
*    Author        :    Karthik
*    Date        :    12 Aug 2008
*
*
*/

package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.display.Stage;

public class Main extends Sprite {

/*
*    Declerations
*/
var mcRoad            :MovieClip;
var mcCheckPoint    :MovieClip;
var mcLastCheckPoint:MovieClip;
var mcRoadLine        :MovieClip;
var mcLineContainer    :MovieClip;
var mcCar            :MovieClip;
var mcPlayButton    :MovieClip;
var mcSpeedoMeter    :MovieClip;
var mcGameOver        :MovieClip;
var i                :Number;
var trackLength        :Number        =    100;
var speedX             :Number        = 0;
var speedY             :Number        = 0;
var acceleration     :Number        = 0.75;
var friction         :Number        = 0.98;

/*
*    Function    :    Main
*    Param        :    NIL
*    Return        :    NIL
*    Description    :    Constructor method.
*/
public function Main() {
this.init();
}

/*
*    Function    :    EnterFrame
*    Param        :    NIL
*    Return        :    NIL
*    Description    :    Initialization method that adds all the required graphics,events to stage.
*/
public function init() {
//     Add the road graphics to the center of the stage.
mcRoad         =     new Road();
mcRoad.x     =     stage.stageWidth * 0.5;
mcRoad.y     =     stage.stageHeight * 0.5;
addChild(mcRoad);
//    Add the raod lines to the stage by duplicating it into multiple up to its track length
mcLineContainer        =    new MovieClip();
for(i=0;i<trackLength*0.5;i++) {
mcRoadLine        =    new Roadline();
mcRoadLine.x    =    stage.stageWidth * 0.5;
mcRoadLine.y    =    stage.stageHeight – i * 150;
mcLineContainer.addChild(mcRoadLine);
}
mcLastCheckPoint    = new Checkpoint();
mcLastCheckPoint.x    =    stage.stageWidth * 0.5;
mcLastCheckPoint.y    =    stage.stageHeight – i * 150;
mcLineContainer.addChild(mcLastCheckPoint);
addChild(mcLineContainer);
//    Add the checkpoint graphics to the center of the stage
mcCheckPoint    =    new Checkpoint();
mcCheckPoint.x    =    stage.stageWidth * 0.5;
mcCheckPoint.y    =    stage.stageHeight * 0.5;
addChild(mcCheckPoint);
//     Add the car graphic to the center of the stage.
mcCar         =     new Car();
mcCar.x     =     stage.stageWidth * 0.5;
mcCar.y     =     stage.stageHeight * 0.5;
addChild(mcCar);
//    Add the play button to the stage and make it to be clicked to start the game.
mcPlayButton                 =     new Playbutton();
mcPlayButton.buttonMode     =     true;
mcPlayButton.x                 =     stage.stageWidth * 0.80;
mcPlayButton.y                 =     stage.stageHeight * 0.5;
mcPlayButton.addEventListener(MouseEvent.CLICK, PlaybuttonClicked);
addChild(mcPlayButton);
}

/*
*    Function    :    EnterFrame
*    Param        :    Any Event
*    Return        :    NIL
*    Description    :    This EnterFrame function is equalent to onEnterFrame function.
*/
private function EnterFrame(event:Event):void {
/*if (Key.isDown(Keyboard.UP)) {
mcLineContainer.y     += 15;
mcCheckPoint.y        += 15;
}*/
if (Key.isDown(Keyboard.LEFT)) {
//speedX = speedX-acceleration;
mcCar.x -= 4;
if(mcCar.x <= stage.stageWidth * 0.39) {
mcCar.x += 4;
}
}
if (Key.isDown(Keyboard.RIGHT)) {
//speedX = speedX+acceleration;
mcCar.x += 4;
if(mcCar.x >= stage.stageWidth * 0.61) {
mcCar.x -= 4;
}
}
if (Key.isDown(Keyboard.UP)) {
speedY = speedY-acceleration;
}
if (Key.isDown(Keyboard.DOWN)) {
speedY = speedY+acceleration;
}
speedX                 = speedX*friction;
speedY                 = speedY*friction;
mcLineContainer.y     -= speedY;
mcCheckPoint.y         -= speedY;
mcSpeedoMeter.speedHand.rotation -= speedY/60;
if(mcCar.hitTestObject(mcLastCheckPoint)) {
stage.removeEventListener(Event.ENTER_FRAME, EnterFrame);
mcGameOver        =    new Gameover();
mcGameOver.x    =    stage.stageWidth * 0.5;
mcGameOver.y    =    stage.stageHeight * 0.5;
addChild(mcGameOver);
}
}

/*
*    Function    :    StartRace
*    Param        :    NIL
*    Return        :    NIL
*    Description    :    Start race function will activate keyboard event to stage and invokes the EnterFrame function
*                    to the stage Enter_Frame event.
*/
private function StartRace():void {
stage.addEventListener(Event.ENTER_FRAME, EnterFrame);
Key.initialize(stage);
}

/*
*    Function    :    PlaybuttonClicked
*    Param        :    Mouse Event
*    Return        :    NIL
*    Description    :    Function will invoke the start race function up on Play button clicked
*/
private function PlaybuttonClicked(event:MouseEvent):void {
StartRace();
mcPlayButton.play();
mcSpeedoMeter         =     new Speedometer();
mcSpeedoMeter.x     =     stage.stageWidth * 0.15;
mcSpeedoMeter.y     =     stage.stageHeight * 0.9;
mcSpeedoMeter.speedHand.rotation    =    0;
addChild(mcSpeedoMeter);
}
}
}

Categories: AS3, Games Tags: ,
Follow

Get every new post delivered to your Inbox.