new Weapon(game, parent)
The Weapon Plugin provides the ability to easily create a bullet pool and manager.
Weapons fire Phaser.Bullet objects, which are essentially Sprites with a few extra properties. The Bullets are enabled for Arcade Physics. They do not currently work with P2 Physics.
The Bullets are created inside of weapon.bullets, which is a Phaser.Group instance. Anything you can usually do with a Group, such as move it around the display list, iterate it, etc can be done to the bullets Group too.
Bullets can have textures and even animations. You can control the speed at which they are fired, the firing rate, the firing angle, and even set things like gravity for them.
A small example, using add.weapon, assumed to be running from within a Phaser.State#create method:
var weapon = this.add.weapon(10, 'bullet');
weapon.fireFrom.set(300, 300);
this.input.onDown.add(weapon.fire, this);
If you want to (re)create the bullet pool separately, you can use:
var weapon = this.game.plugins.add(Phaser.Weapon);
// …
weapon.createBullets(10, 'bullet');
Parameters:
Name | Type | Description |
---|---|---|
game |
Phaser.Game | A reference to the current Phaser.Game instance. |
parent |
Phaser.PluginManager | The Phaser Plugin Manager which looks after this plugin. |
Members
-
<static, constant> KILL_CAMERA_BOUNDS : integer
-
A bulletKillType constant that automatically kills the bullets when they leave the Phaser.Camera#bounds rectangle.
Type:
- integer
-
<static, constant> KILL_DISTANCE : integer
-
A bulletKillType constant that automatically kills the bullets after they exceed the bulletKillDistance from their original firing position.
Type:
- integer
-
<static, constant> KILL_LIFESPAN : integer
-
A bulletKillType constant that automatically kills the bullets when their bulletLifespan expires.
Type:
- integer
-
<static, constant> KILL_NEVER : integer
-
A bulletKillType constant that stops the bullets from ever being destroyed automatically.
Type:
- integer
-
<static, constant> KILL_STATIC_BOUNDS : integer
-
A bulletKillType constant that automatically kills the bullets when they leave the bounds rectangle.
Type:
- integer
-
<static, constant> KILL_WEAPON_BOUNDS : integer
-
A bulletKillType constant that automatically kills the bullets when they leave the bounds rectangle.
Type:
- integer
-
<static, constant> KILL_WORLD_BOUNDS : integer
-
A bulletKillType constant that automatically kills the bullets when they leave the Phaser.World#bounds rectangle.
Type:
- integer
-
_hasFired : boolean
-
Internal multiFire test flag.
Type:
- boolean
-
autoExpandBulletsGroup : boolean
-
Should the bullet pool run out of bullets (i.e. they are all in flight) then this boolean controls if the Group will create a brand new bullet object or not.
Type:
- boolean
-
autofire : boolean
-
Will this weapon auto fire? If set to true then a new bullet will be fired based on the fireRate value.
Type:
- boolean
-
bounds : Phaser.Rectangle
-
This Rectangle defines the bounds that are used when determining if a Bullet should be killed or not. It's used in combination with bulletKillType when that is set to either
Phaser.Weapon.KILL_WEAPON_BOUNDS
orPhaser.Weapon.KILL_STATIC_BOUNDS
. If you are not using either of these kill types then the bounds are ignored. If you are tracking a Sprite or Point then the bounds are centered on that object every frame.Type:
-
bulletAngleOffset : number
-
An optional angle offset applied to the Bullets when they are launched. This is useful if for example your bullet sprites have been drawn facing up, instead of to the right, and you want to fire them at an angle. In which case you can set the angle offset to be 90 and they'll be properly rotated when fired.
Type:
- number
-
bulletAngleVariance : number
-
This is a variance added to the angle of Bullets when they are fired. If you fire from an angle of 90 and have a
bulletAngleVariance
of 20 then the actual angle of the Bullets will be between 70 and 110 degrees. This is a quick way to add a great 'spread' effect to a Weapon.Type:
- number
-
bulletAnimation : string
-
The string based name of the animation that the Bullet will be given on launch. This is set via addBulletAnimation.
Type:
- string
-
bulletClass : function
-
The Class of the bullets that are launched by this Weapon. Defaults to Phaser.Bullet, but can be overridden before calling
createBullets
and set to your own class type.It should be a constructor function accepting
(game, x, y, key, frame)
.Type:
- function
-
bulletCollideWorldBounds : boolean
-
Should bullets collide with the World bounds or not?
Type:
- boolean
-
bulletFrame : string | integer
-
The Texture Frame that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.
Type:
- string | integer
-
bulletFrameCycle : boolean
-
If you've added a set of frames via setBulletFrames then you can optionally chose for each Bullet fired to use the next frame in the set. The frame index is then advanced one frame until it reaches the end of the set, then it starts from the start again. Cycling frames like this allows you to create varied bullet effects via sprite sheets.
Type:
- boolean
-
bulletFrameRandom : boolean
-
If you've added a set of frames via setBulletFrames then you can optionally chose for each Bullet fired to pick a random frame from the set.
Type:
- boolean
-
<protected> bulletFrames : Array
-
This array stores the frames added via @link #setBulletFrames.
Type:
- Array
-
bulletGravity : Phaser.Point
-
This is the amount of Phaser.Physics.Arcade.Body#gravity added to the Bullets physics body when fired. Gravity is expressed in pixels / second / second.
Type:
-
bulletInheritSpriteSpeed : boolean
-
When a Bullet is fired it can optionally inherit the velocity of the
trackedSprite
if set.Type:
- boolean
-
bulletKey : string
-
The Texture Key that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.
Type:
- string
-
bulletKillDistance : number
-
If you've set bulletKillType to
Phaser.Weapon.KILL_DISTANCE
this controls the distance the Bullet can travel before it is automatically killed. The distance is given in pixels.Type:
- number
-
bulletKillType : integer
-
This controls how the bullets will be killed. The default is
Phaser.Weapon.KILL_WORLD_BOUNDS
.There are 7 different "kill types" available:
-
Phaser.Weapon.KILL_NEVER
The bullets are never destroyed by the Weapon. It's up to you to destroy them via your own code. -
Phaser.Weapon.KILL_LIFESPAN
The bullets are automatically killed when their bulletLifespan amount expires. -
Phaser.Weapon.KILL_DISTANCE
The bullets are automatically killed when they exceed bulletKillDistance pixels away from their original launch position. -
Phaser.Weapon.KILL_WEAPON_BOUNDS
The bullets are automatically killed when they no longer intersect with the bounds rectangle. -
Phaser.Weapon.KILL_CAMERA_BOUNDS
The bullets are automatically killed when they no longer intersect with the Phaser.Camera#bounds rectangle. -
Phaser.Weapon.KILL_WORLD_BOUNDS
The bullets are automatically killed when they no longer intersect with the Phaser.World#bounds rectangle. -
Phaser.Weapon.KILL_STATIC_BOUNDS
The bullets are automatically killed when they no longer intersect with the bounds rectangle. The difference between static bounds and weapon bounds, is that a static bounds will never be adjusted to match the position of a tracked sprite or pointer.
Type:
- integer
-
-
bulletLifespan : number
-
If you've set bulletKillType to
Phaser.Weapon.KILL_LIFESPAN
this controls the amount of lifespan the Bullets have set on launch. The value is given in milliseconds. When a Bullet hits its lifespan limit it will be automatically killed.Type:
- number
-
bulletRotateToVelocity : boolean
-
Bullets can optionally adjust their rotation in-flight to match their velocity. This can create the effect of a bullet 'pointing' to the path it is following, for example an arrow being fired from a bow, and works especially well when added to bulletGravity.
Type:
- boolean
-
bullets : Phaser.Group
-
This is the Phaser.Group that contains all of the bullets managed by this plugin.
Type:
-
bulletSpeed : number
-
The initial velocity of fired bullets, in pixels per second.
Type:
- number
- Default Value:
-
- 200
- Source:
- src/plugins/weapon/WeaponPlugin.js line 195
-
bulletSpeedVariance : number
-
This is a variance added to the speed of Bullets when they are fired. If bullets have a bulletSpeed value of 200, and a
bulletSpeedVariance
of 50 then the actual speed of the Bullets will be between 150 and 250 pixels per second.Type:
- number
-
bulletWorldWrap : boolean
-
Should the Bullets wrap around the world bounds? This automatically calls
World.wrap
on the Bullet each frame. See the docs for that method for details.Type:
- boolean
-
bulletWorldWrapPadding : integer
-
If
bulletWorldWrap
is true then you can provide an optional padding value with this property. It's added to the calculations determining when the Bullet should wrap around the world or not. The value is given in pixels.Type:
- integer
-
fireAngle : integer
-
The angle at which the bullets are fired. This can be a const such as Phaser.ANGLE_UP or it can be any number from 0 to 360 inclusive, where 0 degrees is to the right.
Type:
- integer
-
fireFrom : Phaser.Rectangle
-
This is a Rectangle from within which the bullets are fired. By default it's a 1x1 rectangle, the equivalent of a Point. But you can change the width and height, and if larger than 1x1 it'll pick a random point within the rectangle to launch the bullet from.
Type:
-
fireLimit : number
-
The maximum number of shots that this Weapon is allowed to fire before it stops. When the limit is his the onFireLimit Signal is dispatched. You can reset the shot counter via resetShots.
Type:
- number
-
fireRate : number
-
The minimum interval between shots, in milliseconds.
Type:
- number
- Default Value:
-
- 100
- Source:
- src/plugins/weapon/WeaponPlugin.js line 91
-
fireRateVariance : number
-
This is a modifier that is added to the fireRate each update to add variety to the firing rate of the Weapon. The value is given in milliseconds. If you've a
fireRate
of 200 and afireRateVariance
of 50 then the actual firing rate of the Weapon will be between 150 and 250.Type:
- number
-
multiFire : boolean
-
If you want this Weapon to be able to fire more than 1 bullet in a single update, then set this property to
true
. Whentrue
the Weapon plugin won't set the shot / firing timers until thepostRender
phase of the game loop. This means you can callfire
(and similar methods) as often as you like in one single game update.Type:
- boolean
-
onFire : Phaser.Signal
-
The onFire Signal is dispatched each time fire is called, and a Bullet is successfully launched. The callback is set two arguments: a reference to the bullet sprite itself, and a reference to the Weapon that fired the bullet.
Type:
-
onFireLimit : Phaser.Signal
-
The onFireLimit Signal is dispatched if fireLimit is > 0, and a bullet launch takes the number of shots fired to equal the fire limit. The callback is sent two arguments: A reference to this Weapon, and the value of fireLimit.
Type:
-
onKill : Phaser.Signal
-
The onKill Signal is dispatched each time a Bullet that is in-flight is killed. This can be the result of leaving the Weapon bounds, an expiring lifespan, or exceeding a specified distance. The callback is sent one argument: A reference to the bullet sprite itself.
Type:
-
shots : number
-
The total number of bullets this Weapon has fired so far. You can limit the number of shots allowed (via fireLimit), and reset this total via resetShots.
Type:
- number
-
trackedPointer : Phaser.Pointer
-
The Pointer currently being tracked by the Weapon, if any. This is set via the trackPointer method.
Type:
-
trackedSprite : Phaser.Sprite | Object
-
The Sprite currently being tracked by the Weapon, if any. This is set via the trackSprite method.
Type:
- Phaser.Sprite | Object
-
trackOffset : Phaser.Point
-
The Track Offset is a Point object that allows you to specify a pixel offset that bullets use when launching from a tracked Sprite or Pointer. For example if you've got a bullet that is 2x2 pixels in size, but you're tracking a Sprite that is 32x32, then you can set
trackOffset.x = 16
to have the bullet launched from the center of the Sprite.Type:
-
trackRotation : boolean
-
If the Weapon is tracking a Sprite, should it also track the Sprites rotation? This is useful for a game such as Asteroids, where you want the weapon to fire based on the sprites rotation.
Type:
- boolean
-
x : number
-
The x coordinate from which bullets are fired. This is the same as
Weapon.fireFrom.x
, and can be overridden by the fire arguments.Type:
- number
-
y : number
-
The y coordinate from which bullets are fired. This is the same as
Weapon.fireFrom.y
, and can be overridden by the fire arguments.Type:
- number
Methods
-
addBulletAnimation(name [, frames] [, frameRate] [, loop] [, useNumericIndex])
-
Adds a new animation under the given key. Optionally set the frames, frame rate and loop. The arguments are all the same as for
Animation.add
, and work in the same way.bulletAnimation will be set to this animation after it's created. From that point on, all bullets fired will play using this animation. You can swap between animations by calling this method several times, and then just changing the bulletAnimation property to the name of the animation you wish to play for the next launched bullet.
If you wish to stop using animations at all, set bulletAnimation to '' (an empty string).
Parameters:
Name Type Argument Default Description name
string The unique (within the Weapon instance) name for the animation, i.e. "fire", "blast".
frames
Array <optional>
null An array of numbers/strings that correspond to the frames to add to this animation and in which order. e.g. [1, 2, 3] or ['run0', 'run1', run2]). If null then all frames will be used.
frameRate
number <optional>
60 The speed at which the animation should play. The speed is given in frames per second.
loop
boolean <optional>
false Whether or not the animation is looped or just plays once.
useNumericIndex
boolean <optional>
true Are the given frames using numeric indexes (default) or strings?
Returns:
The Weapon Plugin.
- Type
- Phaser.Weapon
-
createBullets( [quantity] [, key] [, frame] [, group])
-
This method performs two actions: First it will check to see if the bullets Group exists or not, and if not it creates it, adding it the
group
given as the 4th argument.Then it will seed the bullet pool with the
quantity
number of Bullets, using the texture key and frame provided (if any).If for example you set the quantity to be 10, then this Weapon will only ever be able to have 10 bullets in-flight simultaneously. If you try to fire an 11th bullet then nothing will happen until one, or more, of the in-flight bullets have been killed, freeing them up for use by the Weapon again.
If you do not wish to have a limit set, then pass in -1 as the quantity. In this instance the Weapon will keep increasing the size of the bullet pool as needed. It will never reduce the size of the pool however, so be careful it doesn't grow too large.
You can either set the texture key and frame here, or via the bulletKey and bulletFrame properties. You can also animate bullets, or set them to use random frames. All Bullets belonging to a single Weapon instance must share the same texture key however.
Parameters:
Name Type Argument Default Description quantity
integer <optional>
1 The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.
key
string <optional>
The Game.cache key of the image that this Sprite will use.
frame
integer | string <optional>
If the Sprite image contains multiple frames you can specify which one to use here.
group
Phaser.Group <optional>
Optional Group to add the object to. If not specified it will be added to the World group.
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
debug( [x] [, y] [, debugBodies])
-
Uses
Game.Debug
to draw some useful information about this Weapon, including the number of bullets both in-flight, and available. And optionally the physics debug bodies of the bullets.Parameters:
Name Type Argument Default Description x
integer <optional>
16 The coordinate, in screen space, at which to draw the Weapon debug data.
y
integer <optional>
32 The coordinate, in screen space, at which to draw the Weapon debug data.
debugBodies
boolean <optional>
false Optionally draw the physics body of every bullet in-flight.
-
destroy()
-
Destroys this Weapon. It removes itself from the PluginManager, destroys the bullets Group, and nulls internal references.
-
fire( [from] [, x] [, y] [, offsetX] [, offsetY])
-
Attempts to fire a single Bullet. If there are no more bullets available in the pool, and the pool cannot be extended, then this method returns
null
. It will also returnnull
if not enough time has expired since the last time the Weapon was fired, as defined in the fireRate property.Otherwise the first available bullet is selected, launched, and returned.
The arguments are all optional, but allow you to control both where the bullet is launched from, and aimed at.
If you don't provide any of the arguments then it uses those set via properties such as trackedSprite, bulletAngle and so on.
When the bullet is launched it has its texture and frame updated, as required. The velocity of the bullet is calculated based on Weapon properties like
bulletSpeed
.If you wish to fire multiple bullets in a single game update, then set
Weapon.multiFire = true
and you can callfire
as many times as you like, per loop. Multiple fires in a single update only counts once towards theshots
total, but you will still receive a Signal for each bullet.Parameters:
Name Type Argument Default Description from
Phaser.Sprite | Phaser.Point | Object | string <optional>
Optionally fires the bullet from the
x
andy
properties of this object. If set this overrides #trackedSprite ortrackedPointer
. Passnull
to ignore it.x
number <optional>
The x coordinate, in world space, to fire the bullet towards. If left as
undefined
, ornull
, the bullet direction is based on its angle.y
number <optional>
The y coordinate, in world space, to fire the bullet towards. If left as
undefined
, ornull
, the bullet direction is based on its angle.offsetX
number <optional>
0 If the bullet is fired from a tracked Sprite or Pointer, or the
from
argument is set, this applies a horizontal offset from the launch position.offsetY
number <optional>
0 If the bullet is fired from a tracked Sprite or Pointer, or the
from
argument is set, this applies a vertical offset from the launch position.Returns:
The fired bullet, if a launch was successful, otherwise
null
.- Type
- Phaser.Bullet
-
fireAtPointer( [pointer])
-
Fires a bullet at the given Pointer. The bullet will be launched from the fireFrom position, or from a Tracked Sprite or Pointer, if you have one set.
Parameters:
Name Type Argument Description pointer
Phaser.Pointer <optional>
The Pointer to fire the bullet towards.
Returns:
The fired bullet if successful, null otherwise.
- Type
- Phaser.Bullet
-
fireAtSprite( [sprite])
-
Fires a bullet at the given Sprite. The bullet will be launched from the fireFrom position, or from a Tracked Sprite or Pointer, if you have one set.
Parameters:
Name Type Argument Description sprite
Phaser.Sprite <optional>
The Sprite to fire the bullet towards.
Returns:
The fired bullet if successful, null otherwise.
- Type
- Phaser.Bullet
-
fireAtXY( [x] [, y])
-
Fires a bullet at the given coordinates. The bullet will be launched from the fireFrom position, or from a Tracked Sprite or Pointer, if you have one set.
Parameters:
Name Type Argument Description x
number <optional>
The x coordinate, in world space, to fire the bullet towards.
y
number <optional>
The y coordinate, in world space, to fire the bullet towards.
Returns:
The fired bullet if successful, null otherwise.
- Type
- Phaser.Bullet
-
fireMany(positions [, from])
-
Attempts to fire multiple bullets from the positions defined in the given array.
If you provide a
from
argument, or if there is a tracked Sprite or Pointer, then the positions are treated as offsets from the given objects position.If
from
is undefined, and there is no tracked object, then the bullets are fired from the given positions, as they exist in the world.Calling this method sets multiFire to
true
.If there are not enough bullets available in the pool, and the pool cannot be extended, then this method may not fire from all of the given positions.
When the bullets are launched they have their texture and frame updated, as required. The velocity of the bullets are calculated based on Weapon properties like bulletSpeed.
Parameters:
Name Type Argument Description positions
array An array of positions. Each position can be any Object, as long as it has public
x
andy
properties, such as Phaser.Point, { x: 0, y: 0 }, Phaser.Sprite, etc.from
Phaser.Sprite | Phaser.Point | Object | string <optional>
Optionally fires the bullets from the
x
andy
properties of this object, instead of any #trackedSprite ortrackedPointer
that is set.Returns:
An array containing all of the fired Phaser.Bullet objects, if a launch was successful, otherwise an empty array.
- Type
- array
-
fireOffset( [offsetX] [, offsetY])
-
Attempts to fire a single Bullet from a tracked Sprite or Pointer, but applies an offset to the position first. This is the same as calling fire and passing in the offset arguments.
If there are no more bullets available in the pool, and the pool cannot be extended, then this method returns
null
. It will also returnnull
if not enough time has expired since the last time the Weapon was fired, as defined in the fireRate property.Otherwise the first available bullet is selected, launched, and returned.
When the bullet is launched it has its texture and frame updated, as required. The velocity of the bullet is calculated based on Weapon properties like bulletSpeed.
If you wish to fire multiple bullets in a single game update, then set multiFire to
true
and you can call this method as many times as you like, per loop. See also fireMany.Parameters:
Name Type Argument Default Description offsetX
number <optional>
0 The horizontal offset from the position of the tracked Sprite or Pointer, as set with #trackSprite.
offsetY
number <optional>
0 The vertical offset from the position of the tracked Sprite or Pointer, as set with #trackSprite.
Returns:
The fired bullet, if a launch was successful, otherwise
null
.- Type
- Phaser.Bullet
-
forEach(callback, callbackContext [, args])
-
Call a function on each in-flight bullet in this Weapon.
See forEachExists for more details.
Parameters:
Name Type Argument Default Description callback
function The function that will be called for each applicable child. The child will be passed as the first argument.
callbackContext
object The context in which the function should be called (usually 'this').
args
any <optional>
<repeatable>
(none) Additional arguments to pass to the callback function, after the child item.
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
killAll()
-
Calls Phaser.Bullet#kill on every in-flight bullet in this Weapon. Also re-enables their physics bodies, should they have been disabled via pauseAll.
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
pauseAll()
-
Sets Phaser.Physics.Arcade.Body#enable to
false
on each bullet in this Weapon. This has the effect of stopping them in-flight should they be moving. It also stops them being able to be checked for collision.Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
<protected> postRender()
-
Internal update method, called by the PluginManager.
-
resetShots( [newLimit])
-
Resets the shots counter back to zero. This is used when you've set fireLimit and have hit (or just wish to reset) your limit.
Parameters:
Name Type Argument Description newLimit
integer <optional>
Optionally set a new #fireLimit.
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
resumeAll()
-
Sets Phaser.Physics.Arcade.Body#enable to
true
on each bullet in this Weapon. This has the effect of resuming their motion should they be in-flight. It also enables them for collision checks again.Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
setBulletBodyOffset(width, height [, offsetX] [, offsetY])
-
You can modify the size of the physics Body the Bullets use to be any dimension you need. This allows you to make it smaller, or larger, than the parent Sprite. You can also control the x and y offset of the Body. This is the position of the Body relative to the top-left of the Sprite texture.
For example: If you have a Sprite with a texture that is 80x100 in size, and you want the physics body to be 32x32 pixels in the middle of the texture, you would do:
setSize(32 / Math.abs(this.scale.x), 32 / Math.abs(this.scale.y), 24, 34)
Where the first two parameters are the new Body size (32x32 pixels) relative to the Sprite's scale. 24 is the horizontal offset of the Body from the top-left of the Sprites texture, and 34 is the vertical offset.
Parameters:
Name Type Argument Description width
number The width of the Body.
height
number The height of the Body.
offsetX
number <optional>
The X offset of the Body from the top-left of the Sprites texture.
offsetY
number <optional>
The Y offset of the Body from the top-left of the Sprites texture.
Returns:
The Weapon Plugin.
- Type
- Phaser.Weapon
-
setBulletFrames(min, max [, cycle] [, random])
-
Sets the texture frames that the bullets can use when being launched.
This is intended for use when you've got numeric based frames, such as those loaded via a Sprite Sheet.
It works by calling
Phaser.ArrayUtils.numberArray
internally, using the min and max values provided. Then it sets the frame index to be zero.You can optionally set the cycle and random booleans, to allow bullets to cycle through the frames when they're fired, or pick one at random.
Parameters:
Name Type Argument Default Description min
integer The minimum value the frame can be. Usually zero.
max
integer The maximum value the frame can be.
cycle
boolean <optional>
true Should the bullet frames cycle as they are fired?
random
boolean <optional>
false Should the bullet frames be picked at random as they are fired?
Returns:
The Weapon Plugin.
- Type
- Phaser.Weapon
-
trackPointer( [pointer] [, offsetX] [, offsetY])
-
Sets this Weapon to track the given Pointer. When a Weapon tracks a Pointer it will automatically update its fireFrom value to match the Pointer's position within the Game World, adjusting the coordinates based on the offset arguments.
This allows you to lock a Weapon to a Pointer, so that bullets are always launched from its location.
Calling
trackPointer
will reset trackedSprite to null, should it have been set, as you can only track either a Pointer, or a Sprite, at once, but not both.Parameters:
Name Type Argument Default Description pointer
Phaser.Pointer <optional>
The Pointer to track the position of. Defaults to
Input.activePointer
if not specified.offsetX
integer <optional>
0 The horizontal offset from the Pointers position to be applied to the Weapon.
offsetY
integer <optional>
0 The vertical offset from the Pointers position to be applied to the Weapon.
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
trackSprite(sprite [, offsetX] [, offsetY] [, trackRotation])
-
Sets this Weapon to track the given Sprite, or any Object with a public world Point object. When a Weapon tracks a Sprite it will automatically update its fireFrom value to match the Sprite's position within the Game World, adjusting the coordinates based on the offset arguments.
This allows you to lock a Weapon to a Sprite, so that bullets are always launched from its location.
Calling
trackSprite
will reset trackedPointer to null, should it have been set, as you can only track either a Sprite, or a Pointer, at once, but not both.Parameters:
Name Type Argument Default Description sprite
Phaser.Sprite | Object The Sprite to track the position of.
offsetX
integer <optional>
0 The horizontal offset from the Sprites position to be applied to the Weapon.
offsetY
integer <optional>
0 The vertical offset from the Sprites position to be applied to the Weapon.
trackRotation
boolean <optional>
false Should the Weapon also track the Sprites rotation?
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
<protected> update()
-
Internal update method, called by the PluginManager.