Options
All
  • Public
  • Public/Protected
  • All
Menu

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.

example

A small example, using add.weapon, assumed to be running from within a Phaser.Scene.create method:

var weapon = this.add.weapon(10, 'bullet');
weapon.fireFrom.setPosition(300, 300);
this.input.on(Phaser.Input.Events.POINTER_DOWN, weapon.fire, this);

Hierarchy

Index

Constructors

constructor

  • new Weapon(scene: Scene, bulletLimit?: number, key?: string, frame?: string, group?: Group): Weapon
  • Parameters

    • scene: Scene

      A reference to the Phaser.Scene instance.

    • Optional bulletLimit: number

      The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.

    • key: string = ''

      The texture cache key of the image that this Sprite will use.

    • frame: string = ''

      If the Sprite image contains multiple frames you can specify which one to use here.

    • Optional group: Group

      Optional Group to add the object to.

    Returns Weapon

Properties

debugPhysics

debugPhysics: boolean = false

Should debug graphics render for physics bodies?

logLevel

logLevel: "warn" | "error" | "off" = 'warn'

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.

scene

scene: Scene

The scene the Weapon is bound to

Accessors

autoExpandBulletsGroup

  • get autoExpandBulletsGroup(): boolean
  • set autoExpandBulletsGroup(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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.

    Parameters

    • value: boolean

    Returns void

autofire

  • get autofire(): boolean
  • set autofire(value: boolean): void
  • Will this weapon auto fire? If set to true then a new bullet will be fired based on the fireRate value.

    defaultvalue

    false

    Returns boolean

  • Will this weapon auto fire? If set to true then a new bullet will be fired based on the fireRate value.

    Parameters

    • value: boolean

    Returns void

bounds

  • 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.

    Returns 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 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.

    Parameters

    Returns void

bulletAngleOffset

  • get bulletAngleOffset(): number
  • set bulletAngleOffset(value: number): void
  • 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.

    defaultvalue

    0

    Returns 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.

    Parameters

    • value: number

    Returns void

bulletAngleVariance

  • get bulletAngleVariance(): number
  • set bulletAngleVariance(value: number): void
  • 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.

    defaultvalue

    0

    Returns 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.

    Parameters

    • value: number

    Returns void

bulletAnimation

  • get bulletAnimation(): string
  • set bulletAnimation(value: string): void
  • The string based name of the animation that the Bullet will be given on launch. This is set via addBulletAnimation.

    defaultvalue

    ''

    Returns string

  • The string based name of the animation that the Bullet will be given on launch. This is set via addBulletAnimation.

    Parameters

    • value: string

    Returns void

bulletBounds

bulletClass

  • get bulletClass(): typeof Bullet
  • set bulletClass(classType: typeof Bullet): void
  • 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).

    defaultvalue

    Bullet

    Returns typeof Bullet

  • 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).

    Parameters

    Returns void

bulletCollideWorldBounds

  • get bulletCollideWorldBounds(): boolean
  • set bulletCollideWorldBounds(value: boolean): void
  • Should bullets collide with the World bounds or not?

    defaultvalue

    false

    Returns boolean

  • Should bullets collide with the World bounds or not?

    Parameters

    • value: boolean

    Returns void

bulletFrame

  • get bulletFrame(): string | number
  • set bulletFrame(value: string | number): void
  • The Texture Frame that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.

    defaultvalue

    ''

    Returns string | number

  • The Texture Frame that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.

    Parameters

    • value: string | number

    Returns void

bulletFrameCycle

  • get bulletFrameCycle(): boolean
  • set bulletFrameCycle(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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.

    Parameters

    • value: boolean

    Returns void

bulletFrameIndex

  • get bulletFrameIndex(): number
  • set bulletFrameIndex(value: number): void

bulletFrameRandom

  • get bulletFrameRandom(): boolean
  • set bulletFrameRandom(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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.

    Parameters

    • value: boolean

    Returns void

bulletFrames

  • get bulletFrames(): number[]
  • set bulletFrames(value: number[]): void

bulletGravity

  • 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.

    Returns Vector2

  • 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.

    Parameters

    Returns void

bulletInheritSpriteSpeed

  • get bulletInheritSpriteSpeed(): boolean
  • set bulletInheritSpriteSpeed(value: boolean): void
  • When a Bullet is fired it can optionally inherit the velocity of the trackedSprite if set.

    defaultvalue

    false

    Returns boolean

  • When a Bullet is fired it can optionally inherit the velocity of the trackedSprite if set.

    Parameters

    • value: boolean

    Returns void

bulletKey

  • get bulletKey(): string
  • set bulletKey(value: string): void
  • The Texture Key that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.

    defaultvalue

    ''

    Returns string

  • The Texture Key that the Bullets use when rendering. Changing this has no effect on bullets in-flight, only on newly spawned bullets.

    Parameters

    • value: string

    Returns void

bulletKillDistance

  • get bulletKillDistance(): number
  • set bulletKillDistance(value: number): void
  • 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.

    defaultvalue

    0

    Returns number

  • 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.

    Parameters

    • value: number

    Returns void

bulletKillType

bulletLifespan

  • get bulletLifespan(): number
  • set bulletLifespan(value: number): void
  • 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.

    defaultvalue

    0

    Returns number

  • 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.

    Parameters

    • value: number

    Returns void

bulletRotateToVelocity

  • get bulletRotateToVelocity(): boolean
  • set bulletRotateToVelocity(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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.

    Parameters

    • value: boolean

    Returns void

bulletSpeed

  • get bulletSpeed(): number
  • set bulletSpeed(value: number): void
  • The initial velocity of fired bullets, in pixels per second.

    defaultvalue

    200

    Returns number

  • The initial velocity of fired bullets, in pixels per second.

    Parameters

    • value: number

    Returns void

bulletSpeedVariance

  • get bulletSpeedVariance(): number
  • set bulletSpeedVariance(value: number): void
  • 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.

    defaultvalue

    0

    Returns 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.

    Parameters

    • value: number

    Returns void

bulletWorldWrap

  • get bulletWorldWrap(): boolean
  • set bulletWorldWrap(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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.

    Parameters

    • value: boolean

    Returns void

bulletWorldWrapPadding

  • get bulletWorldWrapPadding(): number
  • set bulletWorldWrapPadding(value: number): void
  • 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.

    defaultvalue

    0

    Returns number

  • 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.

    Parameters

    • value: number

    Returns void

bullets

  • get bullets(): Group
  • set bullets(value: Group): void

fireAngle

  • get fireAngle(): number
  • set fireAngle(value: number): void
  • 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.

    defaultvalue

    ANGLE_UP

    Returns number

  • 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.

    Parameters

    • value: number

    Returns void

fireFrom

  • 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.

    Returns 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.

    Parameters

    Returns void

fireLimit

  • get fireLimit(): number
  • set fireLimit(value: number): void
  • 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.

    defaultvalue

    0

    Returns number

  • 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.

    Parameters

    • value: number

    Returns void

fireRate

  • get fireRate(): number
  • set fireRate(value: number): void
  • The minimum interval between shots, in milliseconds.

    defaultvalue

    100

    Returns number

  • The minimum interval between shots, in milliseconds.

    Parameters

    • value: number

    Returns void

fireRateVariance

  • get fireRateVariance(): number
  • set fireRateVariance(value: number): void
  • 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.

    defaultvalue

    0

    Returns 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 a fireRateVariance of 50 then the actual firing rate of the Weapon will be between 150 and 250.

    Parameters

    • value: number

    Returns void

multiFire

  • get multiFire(): boolean
  • set multiFire(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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. 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.

    Parameters

    • value: boolean

    Returns void

shots

  • get shots(): number
  • set shots(value: number): void
  • 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.

    defaultvalue

    0

    Returns 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.

    Parameters

    • value: number

    Returns void

trackOffset

  • 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.

    Returns Vector2

  • 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.

    Parameters

    Returns void

trackRotation

  • get trackRotation(): boolean
  • set trackRotation(value: boolean): void
  • 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.

    defaultvalue

    false

    Returns 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.

    Parameters

    • value: boolean

    Returns void

trackedPointer

  • get trackedPointer(): undefined | Pointer
  • set trackedPointer(value: undefined | Pointer): void

trackedSprite

x

  • get x(): number
  • set x(value: number): void
  • The x coordinate from which bullets are fired. This is the same as Weapon.fireFrom.x, and can be overridden by the fire arguments.

    Returns 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.

    Parameters

    • value: number

    Returns void

y

  • get y(): number
  • set y(value: number): void
  • The y coordinate from which bullets are fired. This is the same as Weapon.fireFrom.y, and can be overridden by the fire arguments.

    Returns 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.

    Parameters

    • value: number

    Returns void

Methods

addBulletAnimation

  • addBulletAnimation(name: string, frames?: AnimationFrame[], frameRate?: number, repeat?: number): Weapon
  • 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).

    Parameters

    • name: string

      The unique (within the Weapon instance) name for the animation, i.e. "fire", "blast".

    • Optional frames: AnimationFrame[]

      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.

    • frameRate: number = 60

      The speed at which the animation should play. The speed is given in frames per second.

    • repeat: number = 0

      Number of times to repeat the animation. Set to -1 to repeat forever.

    Returns Weapon

    This Weapon instance.

addListener

  • addListener(event: string | symbol, fn: Function, context?: any): Weapon
  • Add a listener for a given event.

    Parameters

    • event: string | symbol

      The event name.

    • fn: Function

      The listener function.

    • Optional context: any

      The context to invoke the listener with. Default this.

    Returns Weapon

createBullets

  • createBullets(quantity?: number, key?: string, frame?: string | number, group?: Group, bulletClass?: typeof Bullet): Weapon
  • 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.

    Parameters

    • quantity: number = 1

      The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.

    • Optional key: string

      The texture cache key of the image that this Sprite will use.

    • Optional frame: string | number

      If the Sprite image contains multiple frames you can specify which one to use here.

    • Optional group: Group

      Optional Group to add the object to.

    • Optional bulletClass: typeof Bullet

    Returns Weapon

    This Weapon instance.

destroy

  • destroy(): void
  • Destroys this Weapon. You must release everything in here, all references, all objects, free it all up.

    Returns void

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Calls each of the listeners registered for a given event.

    Parameters

    • event: string | symbol

      The event name.

    • Rest ...args: any[]

      Additional arguments that will be passed to the event handler.

    Returns boolean

eventNames

  • eventNames(): (string | symbol)[]
  • Return an array listing the events for which the emitter has registered listeners.

    Returns (string | symbol)[]

fire

  • 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.

    emits

    WEAPON_FIRE

    emits

    WEAPON_FIRE_LIMIT

    Parameters

    • Optional from: null | Sprite | Vector2 | ObjectWithTransform

      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.

    • Optional x: null | number

      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.

    • Optional y: null | number

      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.

    • offsetX: number = 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 = 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 undefined | Bullet

    The fired bullet, if a launch was successful, otherwise undefined.

fireAtPointer

  • 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

    • Optional pointer: Pointer

      The Pointer to fire the bullet towards.

    Returns undefined | Bullet

    The fired bullet if successful, undefined otherwise.

fireAtSprite

  • 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

    • Optional sprite: Sprite

      The Sprite to fire the bullet towards.

    Returns undefined | Bullet

    The fired bullet if successful, undefined otherwise.

fireAtXY

  • fireAtXY(x?: number, y?: number): undefined | Bullet
  • 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

    • Optional x: number

      The x coordinate, in world space, to fire the bullet towards.

    • Optional y: number

      The y coordinate, in world space, to fire the bullet towards.

    Returns undefined | Bullet

    The fired bullet if successful, undefined otherwise.

fireMany

  • 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

    Returns Bullet[]

    An array containing all of the fired Bullet objects, if a launch was successful, otherwise an empty array.

fireOffset

  • fireOffset(offsetX?: number, offsetY?: number): undefined | Bullet
  • 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.

    Parameters

    • offsetX: number = 0

      The horizontal offset from the position of the tracked Sprite or Pointer, as set with trackSprite.

    • offsetY: number = 0

      The vertical offset from the position of the tracked Sprite or Pointer, as set with trackSprite.

    Returns undefined | Bullet

    The fired bullet, if a launch was successful, otherwise undefined.

forEach

  • forEach(callback: (child: GameObject, ...args: unknown[]) => void, callbackContext: unknown, ...args: unknown[]): Weapon
  • Call a function on each in-flight bullet in this Weapon.

    See Set.each for more details.

    Parameters

    • callback: (child: GameObject, ...args: unknown[]) => void

      The function that will be called for each applicable child. The child will be passed as the first argument.

        • Parameters

          Returns void

    • callbackContext: unknown

      The context in which the function should be called (usually 'this').

    • Rest ...args: unknown[]

      Additional arguments to pass to the callback function, after the child item.

    Returns Weapon

    This Weapon instance.

killAll

listenerCount

  • listenerCount(event: string | symbol): number
  • Return the number of listeners listening to a given event.

    Parameters

    • event: string | symbol

      The event name.

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Return the listeners registered for a given event.

    Parameters

    • event: string | symbol

      The event name.

    Returns Function[]

off

  • off(event: string | symbol, fn?: Function, context?: any, once?: boolean): Weapon
  • Remove the listeners of a given event.

    Parameters

    • event: string | symbol

      The event name.

    • Optional fn: Function

      Only remove the listeners that match this function.

    • Optional context: any

      Only remove the listeners that have this context.

    • Optional once: boolean

      Only remove one-time listeners.

    Returns Weapon

on

  • on(event: string | symbol, fn: Function, context?: any): Weapon
  • Add a listener for a given event.

    Parameters

    • event: string | symbol

      The event name.

    • fn: Function

      The listener function.

    • Optional context: any

      The context to invoke the listener with. Default this.

    Returns Weapon

once

  • once(event: string | symbol, fn: Function, context?: any): Weapon
  • Add a one-time listener for a given event.

    Parameters

    • event: string | symbol

      The event name.

    • fn: Function

      The listener function.

    • Optional context: any

      The context to invoke the listener with. Default this.

    Returns Weapon

pauseAll

  • 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.

    Returns Weapon

    This Weapon instance.

postRender

  • postRender(): void
  • Internal postRender method, called by the Weapon Plugin. Used for instance when multiFire is enabled.

    internal

    Returns void

removeAllListeners

  • removeAllListeners(event?: string | symbol): Weapon
  • Remove all listeners, or those of the specified event.

    Parameters

    • Optional event: string | symbol

      The event name.

    Returns Weapon

removeListener

  • removeListener(event: string | symbol, fn?: Function, context?: any, once?: boolean): Weapon
  • Remove the listeners of a given event.

    Parameters

    • event: string | symbol

      The event name.

    • Optional fn: Function

      Only remove the listeners that match this function.

    • Optional context: any

      Only remove the listeners that have this context.

    • Optional once: boolean

      Only remove one-time listeners.

    Returns Weapon

resetShots

  • resetShots(newLimit?: number): Weapon
  • 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

    • Optional newLimit: number

      Optionally set a new fireLimit.

    Returns Weapon

    This Weapon instance.

resumeAll

  • 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.

    Returns Weapon

    This Weapon instance.

setBulletBodyOffset

  • setBulletBodyOffset(width: number, height: number, offsetX?: number, offsetY?: number): Weapon
  • 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

    • width: number

      The width of the Body.

    • height: number

      The height of the Body.

    • offsetX: number = 0

      The X offset of the Body from the top-left of the Sprites texture.

    • offsetY: number = 0

      The Y offset of the Body from the top-left of the Sprites texture.

    Returns Weapon

    This Weapon instance.

setBulletFrames

  • setBulletFrames(min: number, max: number, selectionMethod?: FrameType): Weapon
  • 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.

    Parameters

    • min: number

      The minimum value the frame can be. Usually zero.

    • max: number

      The maximum value the frame can be.

    • selectionMethod: FrameType = ...

      Specifies how the frame for the fired bullet will be selected. See consts.FrameType for options.

    Returns Weapon

    This Weapon instance.

shutdown

  • shutdown(): void
  • Removes all listeners.

    Returns void

trackPointer

  • trackPointer(pointer?: Pointer, offsetX?: number, offsetY?: number): Weapon
  • 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.

    Parameters

    • Optional pointer: Pointer

      The Pointer to track the position of. Defaults to InputPlugin.activePointer if not specified.

    • offsetX: number = 0

      The horizontal offset from the Pointers position to be applied to the Weapon.

    • offsetY: number = 0

      The vertical offset from the Pointers position to be applied to the Weapon.

    Returns Weapon

    This Weapon instance.

trackSprite

  • 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.

    Parameters

    • sprite: Sprite | ObjectWithTransform

      The Sprite to track the position of.

    • offsetX: number = 0

      The horizontal offset from the Sprites position to be applied to the Weapon.

    • offsetY: number = 0

      The vertical offset from the Sprites position to be applied to the Weapon.

    • trackRotation: boolean = false

      Should the Weapon also track the Sprites rotation?

    Returns Weapon

    This Weapon instance.

update

  • update(): void
  • Internal update method, called by the Weapon Plugin. Used for updating weapon bounds and handling autofire.

    internal

    Returns void

Generated using TypeDoc