class flixel.addons.weapon.FlxWeapon

Available on all platforms

A Weapon can only fire 1 type of bullet. But it can fire many of them at once (in different directions if needed) via createBulletPattern * A Player could fire multiple Weapons at the same time however, if you need to layer them up * * @version 1.3 - October 9th 2011 * @link http://www.photonstorm.com * @link http://www.haxeflixel.com * @author Richard Davey / Photon Storm * @author Touch added by Impaler / Beeblerox * * TODO: Angled bullets * TODO: Baked Rotation support for angled bullets * TODO: Bullet death styles (particle effects) * TODO: Bullet trails - blur FX style and Missile Command "draw lines" style? (could be another FX plugin) * TODO: Homing Missiles * TODO: Bullet uses random sprite from sprite sheet (for rainbow style bullets), or cycles through them in sequence? * TODO: Some Weapon base classes like shotgun, lazer, etc?

Class Fields

static var BULLET_DOWN:Int

static var BULLET_LEFT:Int

static var BULLET_RIGHT:Int

static var BULLET_UP:Int

Instance Fields

var currentBullet:FlxBullet

A reference to the Bullet that was fired

var group:FlxTypedGroup<FlxBullet>

The FlxGroup into which all the bullets for this weapon are drawn. This should be added to your display and collision checked against it.

var name:String

Internal name for this weapon (i.e. "pulse rifle")

function new(Name:String, ?ParentRef:FlxSprite = null, ?BulletType:Class<FlxBullet> = null, ?BulletID:Int = 0):Void

Creates the FlxWeapon class which will fire your bullets. * You should call one of the makeBullet functions to visually create the bullets. * Then either use setDirection with fire() or one of the fireAt functions to launch them. * *

Name

The name of your weapon (i.e. "lazer" or "shotgun"). For your internal reference really, but could be displayed in-game. *

ParentRef

If this weapon belongs to a parent sprite, specify it here (bullets will fire from the sprites x/y vars as defined below). *

BulletType

Class of the bullet to be associated with this FlxWeapon, must inherit FlxBullet *

BulletID

An optional ID for the bullet. Can be accessed through FlxBullet.ID

function bulletsOverlap(ObjectOrGroup:FlxBasic, ?NotifyCallBack:FlxObject ->FlxObject ->Void = null, ?SkipParent:Bool = true):Void

Checks to see if the bullets are overlapping the specified object or group * *

ObjectOrGroup

The group or object to check if bullets collide *

NotifyCallBack

A function that will get called if a bullet overlaps an object *

SkipParent

Don't trigger colision notifies with the parent of this object

function fire():Bool

Fires a bullet (if one is available). The bullet will be given the velocity defined in setBulletDirection and fired at the rate set in setFireRate. * *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function fireAtMouse():Bool

Fires a bullet (if one is available) at the mouse coordinates, using the speed set in setBulletSpeed and the rate set in setFireRate. * *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function fireAtPosition(X:Int, Y:Int):Bool

Fires a bullet (if one is available) at the given x/y coordinates, using the speed set in setBulletSpeed and the rate set in setFireRate. * *

X

The x coordinate (in game world pixels) to fire at *

Y

The y coordinate (in game world pixels) to fire at *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function fireAtTarget(Target:FlxSprite):Bool

Fires a bullet (if one is available) at the given targets x/y coordinates, using the speed set in setBulletSpeed and the rate set in setFireRate. * *

Target

The FlxSprite you wish to fire the bullet at *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function fireAtTouch(?Touch:FlxTouch = null):Bool

Fires a bullet (if one is available) at the FlxTouch coordinates, using the speed set in setBulletSpeed and the rate set in setFireRate. * *

Touch

The FlxTouch object to fire at, if null use the first available one *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function fireFromAngle(Angle:Int):Bool

Fires a bullet (if one is available) based on the given angle * *

Angle

The angle (in degrees) calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative) *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function fireFromParentAngle():Bool

Fires a bullet (if one is available) based on the angle of the Weapons parent * *

returns

True if a bullet was fired or false if one wasn't available. A reference to the bullet fired is stored in FlxWeapon.currentBullet.

function getBulletSpeed():Int

The speed in pixels/sec (sq) that the bullet travels at when fired via fireAtMouse, fireAtPosition or fireAtTarget. * *

returns

The speed the bullet moves at, in pixels per second (sq)

function makeAnimatedBullet(Quantity:Int, ImageSequence:Dynamic, FrameWidth:Int, FrameHeight:Int, Frames:Array<Int>, FrameRate:Int, Looped:Bool, ?OffsetX:Int = 0, ?OffsetY:Int = 0):Void

Makes an animated bullet from the image and frame data given. * *

Quantity

How many bullets do you need to make? This value should be high enough to cover all bullets you need on-screen at once plus probably a few extra spare! *

ImageSequence

The image used to created the animated bullet from *

FrameWidth

The width of each frame in the animation *

FrameHeight

The height of each frame in the animation *

Frames

An array of numbers indicating what frames to play in what order (e.g. 1, 2, 3) *

FrameRate

The speed in frames per second that the animation should play at (e.g. 40 fps) *

Looped

Whether or not the animation is looped or just plays once *

OffsetX

When the bullet is fired if you need to offset it on the x axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

OffsetY

When the bullet is fired if you need to offset it on the y axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative)

function makeImageBullet(Quantity:Int, Image:Dynamic, ?OffsetX:Int = 0, ?OffsetY:Int = 0, ?AutoRotate:Bool = false, ?Rotations:Int = 16, ?Frame:Int = -1, ?AntiAliasing:Bool = false, ?AutoBuffer:Bool = false):Void

Makes a bullet sprite from the given image. It will use the width/height of the image. * *

Quantity

How many bullets do you need to make? This value should be high enough to cover all bullets you need on-screen at once plus probably a few extra spare! *

Image

The image used to create the bullet from *

OffsetX

When the bullet is fired if you need to offset it on the x axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

OffsetY

When the bullet is fired if you need to offset it on the y axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

AutoRotate

When true the bullet sprite will rotate to match the angle of the parent sprite. Call fireFromParentAngle or fromFromAngle to fire it using an angle as the velocity. *

Frame

If the image has a single row of square animation frames on it, you can specify which of the frames you want to use here. Default is -1, or "use whole graphic" *

Rotations

The number of rotation frames the final sprite should have. For small sprites this can be quite a large number (360 even) without any problems. *

AntiAliasing

Whether to use high quality rotations when creating the graphic. Default is false. *

AutoBuffer

Whether to automatically increase the image size to accomodate rotated corners. Default is false. Will create frames that are 150% larger on each axis than the original frame or graphic.

function makePixelBullet(Quantity:Int, ?Width:Int = 2, ?Height:Int = 2, ?Color:Int = -1, ?OffsetX:Int = 0, ?OffsetY:Int = 0):Void

Makes a pixel bullet sprite (rather than an image). You can set the width/height and color of the bullet. * *

Quantity

How many bullets do you need to make? This value should be high enough to cover all bullets you need on-screen at once plus probably a few extra spare! *

Width

The width (in pixels) of the bullets *

Height

The height (in pixels) of the bullets *

Color

The color of the bullets. Must be given in 0xAARRGGBB format *

OffsetX

When the bullet is fired if you need to offset it on the x axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

OffsetY

When the bullet is fired if you need to offset it on the y axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative)

function onFireCallback():Void

function setBulletAcceleration(AccelerationX:Int, AccelerationY:Int, SpeedMaxX:Int, SpeedMaxY:Int):Void

If you'd like your bullets to accelerate to their top speed rather than be launched already at it, then set the acceleration value here. * If you've previously set the acceleration then setting it to zero will cancel the effect. * This will update ALL bullets, even those currently "in flight", so be careful about when you call this! * *

AccelerationX

Acceleration speed in pixels per second to apply to the sprites horizontal movement, set to zero to cancel. Negative values move left, positive move right. *

AccelerationY

Acceleration speed in pixels per second to apply to the sprites vertical movement, set to zero to cancel. Negative values move up, positive move down. *

SpeedMaxX

The maximum speed in pixels per second in which the sprite can move horizontally *

SpeedMaxY

The maximum speed in pixels per second in which the sprite can move vertically

function setBulletBounds(Bounds:FlxRect):Void

When a bullet goes outside of this bounds it will be automatically killed, freeing it up for firing again. * TODO - Needs testing with a scrolling map (when not using single screen display) * *

Bounds

An FlxRect area. Inside this area the bullet should be considered alive, once outside it will be killed.

function setBulletDirection(Angle:Int, Speed:Int):Void

Set the direction the bullet will travel when fired. * You can use one of the consts such as BULLETUP, BULLETDOWN or BULLETNORTHEAST to set the angle easily. * Speed should be given in pixels/sec (sq) and is the speed at which the bullet travels when fired. * *

angle

The angle of the bullet. In clockwise positive direction: Right = 0, Down = 90, Left = 180, Up = -90. You can use one of the consts such as BULLET_UP, etc *

speed

The speed it will move, in pixels per second (sq)

function setBulletElasticity(Elasticity:Float):Void

The elasticity of the fired bullet controls how much it rebounds off collision surfaces. * *

Elasticity

The elasticity of the bullet between 0 and 1 (0 being no rebound, 1 being 100% force rebound). Set to zero to disable.

function setBulletGravity(ForceX:Int, ForceY:Int):Void

Sets gravity on all currently created bullets * This will update ALL bullets, even those currently "in flight", so be careful about when you call this! * *

ForceX

A positive value applies gravity dragging the bullet to the right. A negative value drags the bullet to the left. Zero disables horizontal gravity. *

ForceY

A positive value applies gravity dragging the bullet down. A negative value drags the bullet up. Zero disables vertical gravity.

function setBulletLifeSpan(Lifespan:Float):Void

If the bullet should have a fixed life span use this function to set it. * The bullet will be killed once it passes this lifespan, if still alive and in bounds. * *

Lifespan

The lifespan of the bullet, given in seconds.

function setBulletOffset(OffsetX:Float, OffsetY:Float):Void

When the bullet is fired from a parent (or fixed position) it will do so from their x/y coordinate. * Often you need to align a bullet with the sprite, i.e. to make it look like it came out of the "nose" of a space ship. * Use this offset x/y value to achieve that effect. * *

OffsetX

The x coordinate offset to add to the launch location (positive or negative) *

OffsetY

The y coordinate offset to add to the launch location (positive or negative)

function setBulletRandomFactor(?RandomAngle:Int = 0, ?RandomSpeed:Int = 0, ?RandomPosition:FlxPoint = null, ?RandomLifeSpan:Float = 0):Void

To make the bullet apply a random factor to either its angle, speed, or both when fired, set these values. Can create a nice "scatter gun" effect. * *

RandomAngle

The +- value applied to the angle when fired. For example 20 means the bullet can fire up to 20 degrees under or over its angle when fired. *

RandomSpeed

The +- value applied to the speed when fired. For example 20 means the bullet can fire up to 20 px/sec slower or faster when fired. *

RandomPosition

The +- value applied to the firing location when fired (fire spread). *

RandomLifeSpan

The +- value applied to the bulletLifeSpan when fired. For example passing 2 when bulletLifeSpan is 3, means the bullet can live up to 5 seconds, minimum of 1.

function setBulletSpeed(Speed:Int):Void

The speed in pixels/sec (sq) that the bullet travels at when fired via fireAtMouse, fireAtPosition or fireAtTarget. * You can update this value in real-time, should you need to speed-up or slow-down your bullets (i.e. collecting a power-up) * *

Speed

The speed it will move, in pixels per second (sq)

function setFireCallback(?Callback:Void ->Void = null, ?Sound:FlxSound = null):Void

Sets a fire callback function and sound. These are played immediately as the bullet is fired. * *

Callback

The function to call *

Sound

A FlxSound to play

function setFireRate(Rate:Int):Void

Sets the firing rate of the Weapon. By default there is no rate, as it can be controlled by FlxControl.setFireButton. * However if you are firing using the mouse you may wish to set a firing rate. * *

Rate

The delay in milliseconds (ms) between which each bullet is fired, set to zero to clear

function setFiringPosition(X:Int, Y:Int, ?OffsetX:Int = 0, ?OffsetY:Int = 0):Void

Causes the Weapon to fire from a fixed x/y position on the screen, like in the game Missile Command. * If set this over-rides a call to setParent (which causes the Weapon to fire from the parents x/y position) * *

X

The x coordinate (in game world pixels) to fire from *

Y

The y coordinate (in game world pixels) to fire from *

OffsetX

When the bullet is fired if you need to offset it on the x axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

OffsetY

When the bullet is fired if you need to offset it on the y axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative)

function setParent(ParentRef:FlxSprite, ?OffsetX:Int = 0, ?OffsetY:Int = 0, ?UseDirection:Bool = false):Void

Causes the Weapon to fire from the parents x/y value, as seen in Space Invaders and most shoot-em-ups. * *

ParentRef

If this weapon belongs to a parent sprite, specify it here (bullets will fire from the sprites x/y vars as defined below). *

OffsetX

When the bullet is fired if you need to offset it on the x axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

OffsetY

When the bullet is fired if you need to offset it on the y axis, for example to line it up with the "nose" of a space ship, set the amount here (positive or negative) *

UseDirection

When fired the bullet direction is based on parent sprites facing value (up/down/left/right)

function setPostFireCallback(?Callback:Void ->Void = null, ?Sound:FlxSound = null):Void

Sets a post-fire callback function and sound. These are played immediately after the bullet is fired. * *

Callback

The function to call *

Sound

An FlxSound to play

function setPreFireCallback(?Callback:Void ->Void = null, ?Sound:FlxSound = null):Void

Sets a pre-fire callback function and sound. These are played immediately before the bullet is fired. * *

Callback

The function to call *

Sound

A FlxSound to play