A reference to the Phaser.Scene instance.
The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.
The texture cache key of the image that this Sprite will use.
If the Sprite image contains multiple frames you can specify which one to use here.
Optional Group to add the object to.
Should debug graphics render for physics bodies?
Log level for this weapon. Either warn
, error
or off
. warn
is the default.
If you change this, please do so before setting any other properties.
The scene the Weapon is bound to
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.
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.
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 KILL_WEAPON_BOUNDS or 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.
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 KILL_WEAPON_BOUNDS or 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.
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.
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.
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.
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.
The string based name of the animation that the Bullet will be given on launch. This is set via addBulletAnimation.
The string based name of the animation that the Bullet will be given on launch. This is set via addBulletAnimation.
The Rectangle used to calculate the bullet bounds from.
The Rectangle used to calculate the bullet bounds from.
The Class of the bullets that are launched by this Weapon. Defaults to Bullet, but can be overridden before calling createBullets and set to your own class type.
It should be a class (or constructor function) accepting the same params as Bullet,
i.e. (scene:
Phaser.Scene
, x: number, y: number, key: string, frame: string | number)
.
The Class of the bullets that are launched by this Weapon. Defaults to Bullet, but can be overridden before calling createBullets and set to your own class type.
It should be a class (or constructor function) accepting the same params as Bullet,
i.e. (scene:
Phaser.Scene
, x: number, y: number, key: string, frame: string | number)
.
Should bullets collide with the World bounds or not?
Should bullets collide with the World bounds or not?
The Texture Frame that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.
The Texture Frame that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.
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.
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.
The index of the frame within bulletFrames that is currently being used.
This value is only used if bulletFrameCycle is set to true
.
The index of the frame within bulletFrames that is currently being used.
This value is only used if bulletFrameCycle is set to true
.
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.
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.
This array stores the frames added via setBulletFrames.
This array stores the frames added via setBulletFrames.
This is the acceleration due to gravity added to the Bullets physics body when fired, in pixels per second squared. Total gravity is the sum of this vector and the simulation's gravity.
This is the acceleration due to gravity added to the Bullets physics body when fired, in pixels per second squared. Total gravity is the sum of this vector and the simulation's gravity.
When a Bullet is fired it can optionally inherit the velocity of the trackedSprite if set.
When a Bullet is fired it can optionally inherit the velocity of the trackedSprite if set.
The Texture Key that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.
The Texture Key that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.
If you've set bulletKillType to KILL_DISTANCE this controls the distance the Bullet can travel before it is automatically killed. The distance is given in pixels.
If you've set bulletKillType to KILL_DISTANCE this controls the distance the Bullet can travel before it is automatically killed. The distance is given in pixels.
This controls how the bullets will be killed. The default is KILL_WORLD_BOUNDS.
There are 7 different "kill types" available:
KILL_NEVER
The bullets are never destroyed by the Weapon. It's up to you to destroy them via your own code.
KILL_LIFESPAN
The bullets are automatically killed when their bulletLifespan amount expires.
KILL_DISTANCE
The bullets are automatically killed when they
exceed bulletKillDistance pixels away from their original launch position.
KILL_WEAPON_BOUNDS
The bullets are automatically killed when they no longer intersect with the bounds rectangle.
KILL_CAMERA_BOUNDS
The bullets are automatically killed when they no longer intersect with the Camera.getBounds rectangle.
KILL_WORLD_BOUNDS
The bullets are automatically killed when they no longer intersect with the World.bounds rectangle.
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.
This controls how the bullets will be killed. The default is KILL_WORLD_BOUNDS.
There are 7 different "kill types" available:
KILL_NEVER
The bullets are never destroyed by the Weapon. It's up to you to destroy them via your own code.
KILL_LIFESPAN
The bullets are automatically killed when their bulletLifespan amount expires.
KILL_DISTANCE
The bullets are automatically killed when they
exceed bulletKillDistance pixels away from their original launch position.
KILL_WEAPON_BOUNDS
The bullets are automatically killed when they no longer intersect with the bounds rectangle.
KILL_CAMERA_BOUNDS
The bullets are automatically killed when they no longer intersect with the Camera.getBounds rectangle.
KILL_WORLD_BOUNDS
The bullets are automatically killed when they no longer intersect with the World.bounds rectangle.
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.
If you've set bulletKillType to 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.
If you've set bulletKillType to 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.
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.
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.
The initial velocity of fired bullets, in pixels per second.
The initial velocity of fired bullets, in pixels per second.
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.
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.
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.
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.
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.
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.
The angle at which the bullets are fired. This can be a const such as ANGLE_UP or it can be any number from 0 to 360 inclusive, where 0 degrees is to the right.
The angle at which the bullets are fired. This can be a const such as ANGLE_UP or it can be any number from 0 to 360 inclusive, where 0 degrees is to the right.
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.
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.
The maximum number of shots that this Weapon is allowed to fire before it stops. When the limit is hit the WEAPON_FIRE_LIMIT event is dispatched. You can reset the shot counter via resetShots.
The maximum number of shots that this Weapon is allowed to fire before it stops. When the limit is hit the WEAPON_FIRE_LIMIT event is dispatched. You can reset the shot counter via resetShots.
The minimum interval between shots, in milliseconds.
The minimum interval between shots, in milliseconds.
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 a fireRateVariance of 50 then the actual firing rate of the Weapon will be between 150 and 250.
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 a fireRateVariance of 50 then the actual firing rate of the Weapon will be between 150 and 250.
If you want this Weapon to be able to fire more than 1 bullet in a single
update, then set this property to true
. When true
the Weapon plugin won't
set the shot / firing timers until the postRender phase of the game loop.
This means you can call fire (and similar methods) as often as you like in one
single game update.
If you want this Weapon to be able to fire more than 1 bullet in a single
update, then set this property to true
. When true
the Weapon plugin won't
set the shot / firing timers until the postRender phase of the game loop.
This means you can call fire (and similar methods) as often as you like in one
single game update.
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.
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.
The Track Offset is a Vector2 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.
The Track Offset is a Vector2 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.
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.
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.
The Pointer currently being tracked by the Weapon, if any. This is set via the trackPointer method.
The Pointer currently being tracked by the Weapon, if any. This is set via the trackPointer method.
The Sprite currently being tracked by the Weapon, if any. This is set via the trackSprite method.
The Sprite currently being tracked by the Weapon, if any. This is set via the trackSprite method.
The x coordinate from which bullets are fired. This is the same as Weapon.fireFrom.x, and can be overridden by the fire arguments.
The x coordinate from which bullets are fired. This is the same as Weapon.fireFrom.x, and can be overridden by the fire arguments.
The y coordinate from which bullets are fired. This is the same as Weapon.fireFrom.y, and can be overridden by the fire arguments.
The y coordinate from which bullets are fired. This is the same as Weapon.fireFrom.y, and can be overridden by the fire arguments.
Adds a new animation under the given key. Optionally set the frames, frame rate and loop. The arguments are all the same as for AnimationManager.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).
The unique (within the Weapon instance) name for the animation, i.e. "fire", "blast".
An array of numbers/strings that correspond to the framesto 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.
The speed at which the animation should play. The speed is given in frames per second.
Number of times to repeat the animation. Set to -1 to repeat forever.
This Weapon instance.
Add a listener for a given event.
The event name.
The listener function.
The context to invoke the listener with. Default this.
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 its
children to 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.
The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.
The texture cache key of the image that this Sprite will use.
If the Sprite image contains multiple frames you can specify which one to use here.
Optional Group to add the object to.
This Weapon instance.
Destroys this Weapon. You must release everything in here, all references, all objects, free it all up.
Calls each of the listeners registered for a given event.
The event name.
Additional arguments that will be passed to the event handler.
Return an array listing the events for which the emitter has registered listeners.
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 undefined
. It will also return undefined
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, fireAngle 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 call fire as many times as you like, per loop. Multiple fires in a single update
only counts once towards the shots total, but you will still receive an event for each bullet.
Optionally fires the bullet from the x and y properties of this object.
If set this overrides trackedSprite or trackedPointer. Pass null
to ignore it.
The x coordinate, in world space, to fire the bullet towards.
If left as undefined
, or null
, the bullet direction is based on its angle.
The y coordinate, in world space, to fire the bullet towards.
If left as undefined
, or null
, the bullet direction is based on its angle.
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.
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.
The fired bullet, if a launch was successful, otherwise undefined
.
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.
The Pointer to fire the bullet towards.
The fired bullet if successful, undefined otherwise.
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.
The Sprite to fire the bullet towards.
The fired bullet if successful, undefined otherwise.
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.
The x coordinate, in world space, to fire the bullet towards.
The y coordinate, in world space, to fire the bullet towards.
The fired bullet if successful, undefined otherwise.
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.
Optionally fires the bullets from the x and y properties of this object, instead of any trackedSprite or trackedPointer that is set.
An array containing all of the fired Bullet objects, if a launch was successful, otherwise an empty array.
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 undefined
. It will also return undefined
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.
The horizontal offset from the position of the tracked Sprite or Pointer, as set with trackSprite.
The vertical offset from the position of the tracked Sprite or Pointer, as set with trackSprite.
The fired bullet, if a launch was successful, otherwise undefined
.
Call a function on each in-flight bullet in this Weapon.
See Set.each for more details.
The function that will be called for each applicable child. The child will be passed as the first argument.
The context in which the function should be called (usually 'this').
Additional arguments to pass to the callback function, after the child item.
This Weapon instance.
Calls Bullet.kill on every in-flight bullet in this Weapon. Also re-enables their physics bodies, should they have been disabled via pauseAll.
This Weapon instance.
Return the number of listeners listening to a given event.
The event name.
Return the listeners registered for a given event.
The event name.
Remove the listeners of a given event.
The event name.
Only remove the listeners that match this function.
Only remove the listeners that have this context.
Only remove one-time listeners.
Add a listener for a given event.
The event name.
The listener function.
The context to invoke the listener with. Default this.
Add a one-time listener for a given event.
The event name.
The listener function.
The context to invoke the listener with. Default this.
Sets 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.
This Weapon instance.
Internal postRender method, called by the Weapon Plugin. Used for instance when multiFire is enabled.
Remove all listeners, or those of the specified event.
The event name.
Remove the listeners of a given event.
The event name.
Only remove the listeners that match this function.
Only remove the listeners that have this context.
Only remove one-time listeners.
Sets 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.
This Weapon instance.
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.
The width of the Body.
The height of the Body.
The X offset of the Body from the top-left of the Sprites texture.
The Y offset of the Body from the top-left of the Sprites texture.
This Weapon instance.
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.Utils.Array.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.
The minimum value the frame can be. Usually zero.
The maximum value the frame can be.
Specifies how the frame for the fired bullet will be selected. See consts.FrameType for options.
This Weapon instance.
Removes all listeners.
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 undefined, should it have been set, as you can only track either a Pointer, or a Sprite, at once, but not both.
The Pointer to track the position of. Defaults to InputPlugin.activePointer if not specified.
The horizontal offset from the Pointers position to be applied to the Weapon.
The vertical offset from the Pointers position to be applied to the Weapon.
This Weapon instance.
Sets this Weapon to track the given Sprite, or any Object with x/y coords and optionally rotation 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 undefined, should it have been set, as you can only track either a Sprite, or a Pointer, at once, but not both.
The Sprite to track the position of.
The horizontal offset from the Sprites position to be applied to the Weapon.
The vertical offset from the Sprites position to be applied to the Weapon.
Should the Weapon also track the Sprites rotation?
This Weapon instance.
Internal update method, called by the Weapon Plugin. Used for updating weapon bounds and handling autofire.
Generated using TypeDoc
The Weapon provides the ability to easily create a bullet pool and manager.
Weapons fire Bullet objects, which are essentially Sprites with a few extra properties. The Bullets are enabled for Arcade Physics. They do not currently work with Impact or Matter Physics.
The Bullets are created inside of weapon.bullets, which is a Group instance. Anything you can usually do with a Group, like 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.Scene.create method: