Flash Game Development with Flex and Actionscript - Adding Weapons (Page 3 of 3)

Article by Matthew Casperson (4,883 pts ) , published Nov 6, 2009

Player.as

package

{

import flash.events.*;

import flash.geom.*;

import mx.core.*;

public class Player extends GameObject

{

protected static const TimeBetweenShots:Number = 0.25;

protected var shooting:Boolean = false;

protected var timeToNextShot:Number = 0;

public function Player()

{

}

public function startupPlayer():void

{

startupGameObject(ResourceManager.BrownPlaneGraphics, new Point(Application.application.width / 2, Application.application.height / 2), ZOrders.PlayerZOrder);

shooting = false;

timeToNextShot = 0;

}

override public function shutdown():void

{

super.shutdown();

}

override public function enterFrame(dt:Number):void

{

super.enterFrame(dt);

timeToNextShot -= dt;

if (timeToNextShot <= 0 && shooting)

{

timeToNextShot = TimeBetweenShots;

var weapon:Weapon = Weapon.pool.ItemFromPool as Weapon;

weapon.startupBasicWeapon(

ResourceManager.TwoBulletsGraphics,

new Point(

position.x + graphics.bitmap.width / 2 - ResourceManager.TwoBulletsGraphics.bitmap.width / 2,

position.y - graphics.bitmap.height + ResourceManager.TwoBulletsGraphics.bitmap.height * 2),

150);

}

}

override public function mouseMove(event:MouseEvent):void

{

// move player to mouse position

position.x = event.stageX;

position.y = event.stageY;

// keep player on the screen

if (position.x < 0)

position.x = 0;

if (position.x > Application.application.width - graphics.bitmap.width)

position.x = Application.application.width - graphics.bitmap.width;

if (position.y < 0)

position.y = 0;

if (position.y > Application.application.height - graphics.bitmap.height )

position.y = Application.application.height - graphics.bitmap.height ;

}

override public function mouseDown(event:MouseEvent):void

{

shooting = true;

}

override public function mouseUp(event:MouseEvent):void

{

shooting = false;

}

}

}

We have added the shooting property. When set to true (in the mouseDown function, which is called when the left mouse button is pressed down) the Player will periodically add create new instances of the Weapon class. The mouseUp (which is called when the left mouse button is released) sets shooting to false, and the Player stops creating new Weapons. The timeToNextShot / TimeBetweenShots pair of properties are used in the timing of the creation of the new Weapon objects.

By creating two new classes (Weapon and Enemy), and adding some slight changes to the Level and Player classes we are almost at the point where we have a playable game. You will notice that you can’t actually shoot the enemies though. That requires something that we will add in part 6: collision detection.

Back to Flash Game Development with Flex and ActionScript

Related Files

Images

The end result
Showing page 3 of 3

Comments

Oct 21, 2009 12:20 PM
Aryadi
just wanna say....
this tutorial really great! thanks for writing it

and also, I have a little question:

I've been trying to understand the code and it seems like the enterFrame logic resolves around the delta time. Like, reducing the timeToNextEnemy with dt and when the timeToNextEnemy value is or below 0 then do something, while the timeToNextEnemy is already 0 I don't see the need to reducing the timeToNextEnemy (it's already 0, the if conditional is true already)

and I wanna know, does Math.randon() return values between 0 and 1? that would be the logical reason how the enemy player came out randomly on the stage

I think I have some more questions, but I guess that's it for now. I'll ask again when I remember what to ask about :)
Aug 18, 2009 7:29 AM
easyDaMan
dt is...
the delta (d) of the time (t) that has passed since the last frame... i guess! in other words: the difference between now and then!

var now:Number - var then:Number = dt:Number! and dt becomes the new now!

i love u!

peace
Aug 3, 2009 12:38 AM
paul
brillianrt
excellent tutorials.

I stuck on what (dt) is though?
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Subscribe
Browse Web Development