Options
All
  • Public
  • Public/Protected
  • All
Menu

An area that invokes callbacks when an object enters and/or exits (as determined by collision check).

Example

const door = new Trigger(
new HitBox(0, 0, 10, 10),
() => { console.log('Trigger entered.') },
() => { console.log('Trigger exited.') }
)

// In collision checks.

// Sprite collides, call onEnter if first time.
if (sprite.hit(player, door)) {
if (doorEntered) {
return
}

doorEntered = true
door.onEnter()

return
}

// Sprite does not collide, call onExit if previously entered.
if (doorEntered) {
doorEntered = false
door.onExit()
}

Hierarchy

Index

Constructors

constructor

  • new Trigger(hitBox: HitBox, onEnter: Function, onExit: Function, debug?: boolean): Trigger
  • Initialize Trigger object.

    Parameters

    • hitBox: HitBox

      The hitbox to use for the trigger.

    • onEnter: Function

      Callback used when the trigger area is entered.

    • onExit: Function

      Callback used when the trigger area is exited.

    • Default value debug: boolean = false

      Whether to display a debug rectangle for the trigger.

    Returns Trigger

Properties

alpha

alpha: number

The opacity of the entity (from 0 to 1)

children

children: Partial<Entity>[]

An array of child entities for recursive update/render.

dead

dead: boolean

Whether or not the entity is dead.

hitBox

hitBox: HitBox

The hitbox of the sprite.

pos

pos: Vec

Position of the entity.

rotation

rotation: number

The rotation of the sprite.

scale

scale: Vec

Scale of the entity.

visible

visible: boolean

Whether or not the entity is visible.

Accessors

anchor

  • get anchor(): Vec

debug

  • set debug(val: boolean): void

hasChildren

  • get hasChildren(): boolean

height

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

pivot

  • get pivot(): Vec

texture

width

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

Methods

add

  • add<T>(child: T): T
  • Add and return a child object.

    Type parameters

    • T

    Parameters

    • child: T

      Object to add to the container.

    Returns T

map

  • map(fn: (child: Partial<Entity>, index?: number) => void): any
  • Map a function across all children.

    Parameters

    • fn: (child: Partial<Entity>, index?: number) => void

      Function to call for each child.

        • (child: Partial<Entity>, index?: number): void
        • Parameters

          • child: Partial<Entity>
          • Optional index: number

          Returns void

    Returns any

onEnter

  • onEnter(...args: any): any
  • Call to trigger onEnter callback.

    Parameters

    • Rest ...args: any

    Returns any

onExit

  • onExit(...args: any): any
  • Call to trigger onExit callback.

    Parameters

    • Rest ...args: any

    Returns any

remove

  • remove<T>(child: T): T
  • Remove and return a child object.

    Type parameters

    • T

    Parameters

    • child: T

      Object to remove from the container.

    Returns T

update

  • update(_: number, __: number): void