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, 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);
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
bulletKillTypeconstant that automatically kills the bullets when they leave theCamera.boundsrectangle.Type:
- integer
-
<static, constant> KILL_DISTANCE : integer
-
A
bulletKillTypeconstant that automatically kills the bullets after they exceed thebulletDistancefrom their original firing position.Type:
- integer
-
<static, constant> KILL_LIFESPAN : integer
-
A
bulletKillTypeconstant that automatically kills the bullets when theirbulletLifespanexpires.Type:
- integer
-
<static, constant> KILL_NEVER : integer
-
A
bulletKillTypeconstant that stops the bullets from ever being destroyed automatically.Type:
- integer
-
<static, constant> KILL_STATIC_BOUNDS : integer
-
A
bulletKillTypeconstant that automatically kills the bullets when they leave theWeapon.boundsrectangle.Type:
- integer
-
<static, constant> KILL_WEAPON_BOUNDS : integer
-
A
bulletKillTypeconstant that automatically kills the bullets when they leave theWeapon.boundsrectangle.Type:
- integer
-
<static, constant> KILL_WORLD_BOUNDS : integer
-
A
bulletKillTypeconstant that automatically kills the bullets when they leave theWorld.boundsrectangle.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
fireRatevalue.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
Weapon.bulletKillTypewhen that is set to eitherPhaser.Weapon.KILL_WEAPON_BOUNDSorPhaser.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
bulletAngleVarianceof 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
Weapon.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
createBulletsand 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
Weapon.setBulletFramesthen 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
Weapon.setBulletFramesthen 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
Weapon.setBulletFrames.Type:
- Array
-
bulletGravity : Phaser.Point
-
This is the amount of 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
trackedSpriteif 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
bulletKillTypetoPhaser.Weapon.KILL_DISTANCEthis 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_NEVERThe bullets are never destroyed by the Weapon. It's up to you to destroy them via your own code.Phaser.Weapon.KILL_LIFESPANThe bullets are automatically killed when theirbulletLifespanamount expires.Phaser.Weapon.KILL_DISTANCEThe bullets are automatically killed when they exceedbulletDistancepixels away from their original launch position.Phaser.Weapon.KILL_WEAPON_BOUNDSThe bullets are automatically killed when they no longer intersect with theWeapon.boundsrectangle.Phaser.Weapon.KILL_CAMERA_BOUNDSThe bullets are automatically killed when they no longer intersect with theCamera.boundsrectangle.Phaser.Weapon.KILL_WORLD_BOUNDSThe bullets are automatically killed when they no longer intersect with theWorld.boundsrectangle.Phaser.Weapon.KILL_STATIC_BOUNDSThe bullets are automatically killed when they no longer intersect with theWeapon.boundsrectangle. 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
bulletKillTypetoPhaser.Weapon.KILL_LIFESPANthis 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 speed at which the bullets are fired. This value is given in pixels per second, and is used to set the starting velocity of the bullets.
Type:
- number
-
bulletSpeedVariance : number
-
This is a variance added to the speed of Bullets when they are fired. If bullets have a
bulletSpeedvalue of 200, and abulletSpeedVarianceof 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.wrapon the Bullet each frame. See the docs for that method for details.Type:
- boolean
-
bulletWorldWrapPadding : integer
-
If
bulletWorldWrapis 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
Weapon.onFireLimitSignal is dispatched. You can reset the shot counter viaWeapon.resetShots.Type:
- number
-
fireRate : number
-
The rate at which this Weapon can fire. The value is given in milliseconds.
Type:
- number
-
fireRateVariance : number
-
This is a modifier that is added to the
fireRateeach update to add variety to the firing rate of the Weapon. The value is given in milliseconds. If you've afireRateof 200 and afireRateVarianceof 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. Whentruethe Weapon plugin won't set the shot / firing timers until thepostRenderphase 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
Weapon.fireis 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
Weapon.fireLimitis > 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 the Weapon that hit the limit, and the value ofWeapon.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 viaWeapon.resetShots.Type:
- number
-
trackedPointer : Phaser.Pointer
-
The Pointer currently being tracked by the Weapon, if any. This is set via the
Weapon.trackPointermethod.Type:
-
trackedSprite : Phaser.Sprite | Object
-
The Sprite currently being tracked by the Weapon, if any. This is set via the
Weapon.trackSpritemethod.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 = 16to 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 theWeapon.firearguments.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 theWeapon.firearguments.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.Weapon.bulletAnimationwill 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 theWeapon.bulletAnimationproperty 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
Weapon.bulletAnimationto '' (an empty string).Parameters:
Name Type Argument Default Description namestring The unique (within the Weapon instance) name for the animation, i.e. "fire", "blast".
framesArray <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.
frameRatenumber <optional>
60 The speed at which the animation should play. The speed is given in frames per second.
loopboolean <optional>
false Whether or not the animation is looped or just plays once.
useNumericIndexboolean <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
Weapon.bulletsGroup exists or not, and if not it creates it, adding it thegroupgiven as the 4th argument.Then it will seed the bullet pool with the
quantitynumber 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
Weapon.bulletKeyandWeapon.bulletFrameproperties. 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 quantityinteger <optional>
1 The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.
keystring <optional>
The Game.cache key of the image that this Sprite will use.
frameinteger | string <optional>
If the Sprite image contains multiple frames you can specify which one to use here.
groupPhaser.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.Debugto 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 xinteger <optional>
16 The coordinate, in screen space, at which to draw the Weapon debug data.
yinteger <optional>
32 The coordinate, in screen space, at which to draw the Weapon debug data.
debugBodiesboolean <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 returnnullif not enough time has expired since the last time the Weapon was fired, as defined in theWeapon.fireRateproperty.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
Weapon.trackedSprite,Weapon.bulletAngleand 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 = trueand you can callfireas many times as you like, per loop. Multiple fires in a single update only counts once towards theshotstotal, but you will still receive a Signal for each bullet.Parameters:
Name Type Argument Default Description fromPhaser.Sprite | Phaser.Point | Object | string <optional>
Optionally fires the bullet from the
xandyproperties of this object. If set this overridesWeapon.trackedSpriteortrackedPointer. Passnullto ignore it.xnumber <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.ynumber <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.offsetXnumber <optional>
0 If the bullet is fired from a tracked Sprite or Pointer, or the
fromargument is set, this applies a horizontal offset from the launch position.offsetYnumber <optional>
0 If the bullet is fired from a tracked Sprite or Pointer, or the
fromargument 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
Weapon.fireFromposition, or from a Tracked Sprite or Pointer, if you have one set.Parameters:
Name Type Argument Description pointerPhaser.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
Weapon.fireFromposition, or from a Tracked Sprite or Pointer, if you have one set.Parameters:
Name Type Argument Description spritePhaser.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
Weapon.fireFromposition, or from a Tracked Sprite or Pointer, if you have one set.Parameters:
Name Type Argument Description xnumber <optional>
The x coordinate, in world space, to fire the bullet towards.
ynumber <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
fromargument, or if there is a tracked Sprite or Pointer, then the positions are treated as offsets from the given objects position.If
fromis 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
Weapon.multiFire = 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 positionsarray An array of positions. Each position can be any Object, as long as it has public
xandyproperties, such as Phaser.Point, { x: 0, y: 0 }, Phaser.Sprite, etc.fromPhaser.Sprite | Phaser.Point | Object | string <optional>
Optionally fires the bullets from the
xandyproperties of this object, instead of anyWeapon.trackedSpriteortrackedPointerthat 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
Weapon.fireand 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 returnnullif not enough time has expired since the last time the Weapon was fired, as defined in theWeapon.fireRateproperty.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
Weapon.multiFire = trueand you can call this method as many times as you like, per loop. See alsoWeapon.fireMany.Parameters:
Name Type Argument Default Description offsetXnumber <optional>
0 The horizontal offset from the position of the tracked Sprite or Pointer, as set with
Weapon.trackSprite.offsetYnumber <optional>
0 The vertical offset from the position of the tracked Sprite or Pointer, as set with
Weapon.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 callbackfunction The function that will be called for each applicable child. The child will be passed as the first argument.
callbackContextobject The context in which the function should be called (usually 'this').
argsany <optional>
<repeatable>
(none) Additional arguments to pass to the callback function, after the child item.
Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
killAll()
-
Calls
Bullet.killon every in-flight bullet in this Weapon. Also re-enables their physics bodies, should they have been disabled viapauseAll.Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
pauseAll()
-
Sets
Body.enabletofalseon 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
Weapon.shotscounter back to zero. This is used when you've setWeapon.fireLimit, and have hit (or just wish to reset) your limit.Parameters:
Name Type Argument Description newLimitinteger <optional>
Optionally set a new
Weapon.fireLimit.Returns:
This Weapon instance.
- Type
- Phaser.Weapon
-
resumeAll()
-
Sets
Body.enabletotrueon 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 widthnumber The width of the Body.
heightnumber The height of the Body.
offsetXnumber <optional>
The X offset of the Body from the top-left of the Sprites texture.
offsetYnumber <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.numberArrayinternally, 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 mininteger The minimum value the frame can be. Usually zero.
maxinteger The maximum value the frame can be.
cycleboolean <optional>
true Should the bullet frames cycle as they are fired?
randomboolean <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
fireFromvalue to match the Pointers 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
trackPointerwill resetWeapon.trackedSpriteto 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 pointerPhaser.Pointer <optional>
The Pointer to track the position of. Defaults to
Input.activePointerif not specified.offsetXinteger <optional>
0 The horizontal offset from the Pointers position to be applied to the Weapon.
offsetYinteger <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
worldPoint object. When a Weapon tracks a Sprite it will automatically update itsfireFromvalue to match the Sprites 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
trackSpritewill resetWeapon.trackedPointerto 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 spritePhaser.Sprite | Object The Sprite to track the position of.
offsetXinteger <optional>
0 The horizontal offset from the Sprites position to be applied to the Weapon.
offsetYinteger <optional>
0 The vertical offset from the Sprites position to be applied to the Weapon.
trackRotationboolean <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.