q5play
    Preparing search index...

    Class Sprite

    A Sprite has a Box2D physics body with a collider (by default), which can interact with other sprites in the physics simulation.

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new sprite.

      Input parameters are optional. A sprite's default position is in the center of the canvas [0,0] and default box collider is 50x50 pixels.

      Parameters

      • Optionalx: number

        horizontal position

      • Optionaly: number

        vertical position

      • Optionalw: number

        width of the collider

      • Optionalh: number

        height of the collider

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite.

      Parameters

      • Optionalx: number

        horizontal position

      • Optionaly: number

        vertical position

      • Optionalw: number

        width of the collider

      • Optionalh: number

        height of the collider

      • OptionalroundedRadius: number

        corner radius for a rounded box collider

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a circle collider.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • d: number

        diameter of the circle collider

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a capsule collider.

      Parameters

      • points: [number, number][]

        array of two [x, y] points defining the capsule's endpoints

      • roundedRadius: number

        the radius of the capsule's rounded ends

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a chain or polygon collider defined by absolute vertex positions. If the first and last vertex are the same and the shape is convex, it becomes a polygon.

      Parameters

      • vertices: [number, number][]

        array of [x, y] vertex positions

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a chain collider defined by relative vertex offsets from the sprite's position.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • vertices: [number, number][]

        array of relative [x, y] vertex offsets

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a chain collider defined by alternating segment lengths and angles. Each angle is relative to the previous segment's angle. If the last value is 5, the chain is closed into a loop.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • distAngles: number[]

        alternating segment lengths and relative angles; append 5 to close the chain

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a rounded chain collider defined by alternating segment lengths and angles. Each angle is relative to the previous segment's angle. If the last value is 5, the chain is closed into a loop.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • distAngles: number[]

        alternating segment lengths and relative angles; append 5 to close the chain

      • roundedRadius: number

        the rounded radius of the chain's segments

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a regular polygon collider.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • sideLength: number

        the length of each side of the polygon

      • polygonName: string

        'triangle', 'square', 'pentagon', 'hexagon', 'septagon', or 'octagon'

      • OptionalroundedRadius: number

        corner radius for a rounded polygon

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • Optionalx: number
      • Optionaly: number
      • Optionalw: number
      • Optionalh: number
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • Optionalx: number
      • Optionaly: number
      • Optionalw: number
      • Optionalh: number
      • OptionalroundedRadius: number
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • x: number
      • y: number
      • d: number
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • points: [number, number][]
      • roundedRadius: number
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • vertices: [number, number][]
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • x: number
      • y: number
      • vertices: [number, number][]
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • x: number
      • y: number
      • distAngles: number[]
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • x: number
      • y: number
      • distAngles: number[]
      • roundedRadius: number
      • OptionalphysicsType: string

      Returns Sprite

    • Parameters

      • ani: string | Image | Ani
      • x: number
      • y: number
      • sideLength: number
      • polygonName: string
      • OptionalroundedRadius: number
      • OptionalphysicsType: string

      Returns Sprite

    Properties

    animations: Anis

    Keys are the animation label, values are Ani objects.

    bdID: b2BodyId

    The Box2D body id for the sprite's physics body. Don't change it!

    colliders: Collider[]

    Array of colliders that are part of the sprite's physics body.

    groups: Group[]

    Groups the sprite belongs to.

    [allSprites]
    
    idNum: number

    Each sprite has a unique id number. Don't change it! They are useful for debugging.

    joints: Joint[]

    Joints that the sprite is attached to.

    []
    
    mod: {}

    Modification tracking object.

    It has sprite property number codes as keys, these correspond to the index of the property in the Sprite.props array, and boolean values, that indicate which properties were changed since the last frame.

    Useful for limiting the amount of sprite data sent in netcode to only the sprite properties that have been modified.

    prevRotation: number

    The sprite's rotation on the previous frame.

    prevX: number

    The sprite's x position on the previous frame.

    prevY: number

    The sprite's y position on the previous frame.

    sensors: Sensor[]

    Array of sensors that are part of the sprite's physics body. Sensors are used to detect overlaps without causing physical collisions.

    text: string

    Text displayed at the center of the sprite.

    undefined
    
    vx: number

    Horizontal velocity of the visual.

    vy: number

    Vertical velocity of the visual.

    watch: boolean

    If set to true, q5play will record all changes to the sprite's properties in its mod array. Intended to be used to enable online multiplayer.

    undefined
    

    Accessors

    • get allowSleeping(): boolean

      Controls the ability for a sprite to "sleep".

      "Sleeping" sprites are not included in the physics simulation, a sprite starts "sleeping" when it stops moving and doesn't collide with anything that it wasn't already colliding with.

      Returns boolean

      true
      
    • set allowSleeping(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get anis(): Anis

      Stores animations. Keys are the animation label, values are Ani objects

      Returns Anis

    • get autoDraw(): boolean

      If true, a sprite is drawn by q5play after each physics update.

      Returns boolean

      true
      
    • set autoDraw(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get autoUpdate(): boolean

      If true, a sprite is updated by q5play before each physics update.

      Returns boolean

      true
      
    • set autoUpdate(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get bearing(): number

      A bearing indicates the direction that needs to be followed to reach a destination.

      Setting a sprite's bearing doesn't do anything by itself. You can apply a force to the sprite at its bearing angle using the applyForce function.

      Returns number

    • set bearing(val: number): void

      Parameters

      • val: number

      Returns void

    • get bounciness(): number

      The bounciness of the sprite's physics body.

      Returns number

      0.2
      
    • set bounciness(val: number): void

      Parameters

      • val: number

      Returns void

    • get canvasPos(): any

      The sprite's absolute position on the canvas.

      Returns any

    • get centerOfMass(): { x: number; y: number }

      The center of mass of the sprite's physics body, the point at which the physics body is balanced and rotates around. By default it's the same as the sprite's position, but it can be changed with this setter.

      Returns { x: number; y: number }

    • set centerOfMass(val: { x: number; y: number }): void

      Parameters

      • val: { x: number; y: number }

      Returns void

    • get d(): number

      The diameter of a circular sprite.

      Returns number

    • set d(val: number): void

      Parameters

      • val: number

      Returns void

    • get debug(): boolean

      If true, outlines of the sprite's colliders and sensors will be drawn.

      Returns boolean

      false
      
    • set debug(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get deleted(): boolean

      If the sprite has been deleted from the world.

      Returns boolean

      false
      
    • set deleted(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get density(): number

      The density of the sprite's physics body.

      Returns number

      1
      
    • set density(val: number): void

      Parameters

      • val: number

      Returns void

    • get diameter(): number

      The diameter of a circular sprite.

      Returns number

    • set diameter(val: number): void

      Parameters

      • val: number

      Returns void

    • get direction(): number

      The angle of the sprite's movement.

      Can be set with directional strings like "up", "down", "left", "right", "upRight", "upLeft", "downRight", and "downLeft". The setter's input parser ignores capitalization, spaces, underscores, dashes, and cardinal direction word order.

      Returns number

      0 ("right")
      
    • set direction(val: string | number): void

      Parameters

      • val: string | number

      Returns void

    • get drag(): number

      The amount of resistance a sprite has to being moved.

      Returns number

      0
      
    • set drag(val: number): void

      Parameters

      • val: number

      Returns void

    • get draw(): Function

      Displays the sprite.

      This function is called automatically at the end of each sketch draw function call but it can also be run by users to customize the order sprites are drawn in relation to other stuff drawn on the canvas. Also see the sprite.layer property.

      A sprite's draw function can be overridden with a custom draw function, inside this function (0, 0) is the center of the sprite.

      Returns Function

    • set draw(val: Function): void

      Draws the visual on the canvas.

      Parameters

      • val: Function

      Returns void

    • get fixedCenterOfMass(): boolean

      If true, the center of mass of the sprite's physics body is fixed to the sprite's [x, y] position.

      It prevents the center of mass from being recalculated (moved) when adding or removing colliders or sensors. Set it to false to allow dynamic center of mass recalculation.

      Returns boolean

      true
      
    • set fixedCenterOfMass(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get friction(): number

      The amount the sprite's colliders resist moving when rubbing against other colliders.

      Returns number

      0.5
      
    • set friction(val: number): void

      Parameters

      • val: number

      Returns void

    • get grabbable(): boolean

      Whether the sprite can be grabbed by a pointer.

      Returns boolean

    • set grabbable(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get gravityScale(): number

      A ratio that defines how much the sprite is affected by gravity.

      Returns number

      1
      
    • set gravityScale(val: number): void

      Parameters

      • val: number

      Returns void

    • get h(): number

      The height of the sprite.

      Returns number

    • set h(val: number): void

      Parameters

      • val: number

      Returns void

    • get halfHeight(): number

      Half the height of the sprite.

      Returns number

    • set halfHeight(val: number): void

      Parameters

      • val: number

      Returns void

    • get halfWidth(): number

      Half the width of the sprite.

      Returns number

    • set halfWidth(val: number): void

      Parameters

      • val: number

      Returns void

    • get heading(): string

      The sprite's heading. This is a string that can be set to "up", "down", "left", "right", "upRight", "upLeft", "downRight"

      The setter's input parser ignores capitalization, spaces, underscores, dashes, and cardinal direction word order.

      Returns string

      undefined
      
    • set heading(val: string): void

      Parameters

      • val: string

      Returns void

    • get height(): number

      The height of the sprite.

      Returns number

    • set height(val: number): void

      Parameters

      • val: number

      Returns void

    • get hh(): number

      Half the height of the sprite.

      Returns number

    • set hh(val: number): void

      Parameters

      • val: number

      Returns void

    • get hw(): number

      Half the width of the sprite.

      Returns number

    • set hw(val: number): void

      Parameters

      • val: number

      Returns void

    • get img(): Image

      Current image or frame of animation being displayed.

      Returns Image

    • set img(image: string | Image): void

      Parameters

      Returns void

    • get isSuperFast(): boolean

      Set this to true if the sprite goes really fast to prevent inaccurate physics simulation.

      Returns boolean

      false
      
    • set isSuperFast(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get layer(): number

      Sprites with the highest layer value get drawn first.

      By default sprites are drawn in the order they were created in.

      Returns number

    • set layer(val: number): void

      Parameters

      • val: number

      Returns void

    • get life(): number

      When the physics simulation is progressed in world.physicsUpdate, each sprite's life is decreased by world.timeScale.

      If life becomes less than or equal to 0, the sprite will be removed.

      Returns number

      Infinity
      
    • set life(val: number): void

      Parameters

      • val: number

      Returns void

    • get mass(): number

      The mass of the sprite's physics body.

      Returns number

    • set mass(val: number): void

      Parameters

      • val: number

      Returns void

    • get opacity(): number

      The sprite's opacity. 0 is transparent, 1 is opaque.

      Returns number

      1
      
    • set opacity(val: number): void

      Parameters

      • val: number

      Returns void

    • get physics(): string

      The physics type of the sprite, which determines how it interacts with other sprites in the physics simulation.

      It can be set to DYNAMIC/DYN, STATIC/STA, or KINEMATIC/KIN.

      Returns string

      "dynamic"
      
    • set physics(val: string): void

      Parameters

      • val: string

      Returns void

    • get physicsEnabled(): boolean

      If true, the sprite's physics body is included in the physics simulation.

      Returns boolean

      true
      
    • set physicsEnabled(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get physicsType(): string

      The physics type of the sprite, which determines how it interacts with other sprites in the physics simulation.

      It can be set to DYNAMIC/DYN, STATIC/STA, or KINEMATIC/KIN.

      Returns string

      "dynamic"
      
    • set physicsType(val: string): void

      Parameters

      • val: string

      Returns void

    • get pixelPerfect(): boolean

      If true, q5play will draw sprites at integer pixel precision.

      This is useful for making retro games.

      By default q5play draws sprites with subpixel rendering.

      Returns boolean

      false
      
    • set pixelPerfect(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get pos(): { x: number; y: number }

      Gets the sprite's position as a readonly object {x, y} which won't be updated if the sprite moves. Useful for saving the sprite's position at a specific moment in time.

      Returns { x: number; y: number }

    • set pos(val: number[] | { x: number; y: number }): void

      The sprite's position vector.

      Parameters

      • val: number[] | { x: number; y: number }

      Returns void

    • get position(): Vector

      The sprite's position vector.

      Returns Vector

    • set position(val: number[] | { x: number; y: number }): void

      Parameters

      • val: number[] | { x: number; y: number }

      Returns void

    • get previousRotation(): number

      The sprite's rotation on the previous frame. Alias for sprite.prevRotation.

      Returns number

    • get previousX(): number

      The sprite's x position on the previous frame. Alias for sprite.prevX.

      Returns number

    • get previousY(): number

      The sprite's y position on the previous frame. Alias for sprite.prevY.

      Returns number

    • get r(): number

      The radius of a circular sprite.

      Returns number

    • set r(val: number): void

      Parameters

      • val: number

      Returns void

    • get radius(): number

      The radius of a circular sprite.

      Returns number

    • set radius(val: number): void

      Parameters

      • val: number

      Returns void

    • get rollingResistance(): number

      Simulates friction that slows down a sprite rolling on another sprite, like a soccer ball rolling to a stop on high grass.

      Returns number

      0
      
    • set rollingResistance(val: number): void

      Parameters

      • val: number

      Returns void

    • get rotation(): number

      The angle of the sprite's rotation, not the direction it's moving.

      If angleMode is set to "degrees", the value will be returned in a range of -180 to 180.

      Returns number

      0
      
    • set rotation(val: number): void

      Parameters

      • val: number

      Returns void

    • get rotationDrag(): number

      The amount the sprite resists rotating.

      Returns number

      0
      
    • set rotationDrag(val: number): void

      Parameters

      • val: number

      Returns void

    • get rotationLock(): boolean

      If true, the sprite can not rotate.

      Returns boolean

      false
      
    • set rotationLock(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get rotationSpeed(): number

      The speed of the sprite's rotation in angles per frame.

      Returns number

      0
      
    • set rotationSpeed(val: number): void

      Parameters

      • val: number

      Returns void

    • get scale(): number | { x: number; y: number }

      Horizontal and vertical scale of the sprite.

      Components can be negative to flip/mirror the sprite on an axis.

      The valueOf function for sprite.scale returns the scale as a number. This enables users to do things like sprite.scale *= 2 to double the sprite's scale.

      Returns number | { x: number; y: number }

      {x: 1, y: 1}
      
    • set scale(val: number | [] | { x: number; y: number }): void

      Parameters

      • val: number | [] | { x: number; y: number }

      Returns void

    • get sleeping(): boolean

      Wake a sprite up or put it to sleep.

      "Sleeping" sprites are not included in the physics simulation, a sprite starts "sleeping" when it stops moving and doesn't collide with anything that it wasn't already colliding with.

      Returns boolean

      true
      
    • set sleeping(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get sleepThreshold(): number

      The minimum speed (in m/s) at which the sprite must be moving before it is considered awake.

      Returns number

    • set sleepThreshold(val: number): void

      Parameters

      • val: number

      Returns void

    • get speed(): number

      The sprite's speed.

      Setting speed to a negative value will make the sprite move 180 degrees opposite of its current direction angle.

      Returns number

      0
      
    • set speed(val: number): void

      Parameters

      • val: number

      Returns void

    • get strokeWeight(): number

      The sprite's stroke weight, the thickness of its outline.

      Returns number

    • set strokeWeight(val: number): void

      Parameters

      • val: number

      Returns void

    • get surfaceSpeed(): number

      The sprite's speed along the surface of its collider(s), like a conveyor belt. Requires friction to be greater than 1 to have an effect.

      Returns number

      0
      
    • set surfaceSpeed(val: number): void

      Parameters

      • val: number

      Returns void

    • get textSize(): number

      The sprite's text size, the sketch's current textSize by default.

      Returns number

    • set textSize(val: number): void

      Parameters

      • val: number

      Returns void

    • get textStroke(): Color

      The sprite's text stroke color. No stroke by default, does not inherit from the sketch's stroke color.

      Returns Color

      undefined
      
    • set textStroke(val: Color): void

      Parameters

      Returns void

    • get textStrokeWeight(): number

      The sprite's text stroke weight, the thickness of its outline. No stroke by default, does not inherit from the sketch's stroke weight.

      Returns number

      undefined
      
    • set textStrokeWeight(val: number): void

      Parameters

      • val: number

      Returns void

    • get tile(): string

      The tile character that represents the sprite in a tile map.

      Returns string

    • set tile(val: string): void

      Parameters

      • val: string

      Returns void

    • get tint(): Color

      Tint color applied to the sprite when drawn.

      Note that this is not good for performance, you should probably pre-render the effect if you want to use it a lot.

      Returns Color

      undefined
      
    • set tint(val: Color): void

      Parameters

      Returns void

    • get update(): Function

      Runs before each physics update by default.

      Set this to a custom function that handles input, directs sprite movement, and performs other tasks that should run before the physics update.

      Optionally, users can run this function manually in q5play's update function.

      Returns Function

    • set update(val: Function): void

      Parameters

      • val: Function

      Returns void

    • get visible(): boolean

      If true the sprite is shown, if set to false the sprite is hidden.

      Becomes null when the sprite is off screen but will be drawn and set to true again if it goes back on screen.

      Returns boolean

      true
      
    • set visible(val: boolean): void

      Parameters

      • val: boolean

      Returns void

    • get w(): number

      The width of the sprite.

      Returns number

    • set w(val: number): void

      Parameters

      • val: number

      Returns void

    • get width(): number

      The width of the sprite.

      Returns number

    • set width(val: number): void

      Parameters

      • val: number

      Returns void

    • get x(): number

      The horizontal position of the sprite.

      Returns number

    • set x(val: number): void

      Horizontal position of the visual.

      Parameters

      • val: number

      Returns void

    • get y(): number

      The vertical position of the sprite.

      Returns number

    • set y(val: number): void

      Vertical position of the visual.

      Parameters

      • val: number

      Returns void

    Methods

    • Adds an animation to the Sprite or Visual.

      Parameters

      • spriteSheetURL: string

        the URL of the sprite sheet image

      • frameCount: number

        the number of frames in the sprite sheet

      Returns Promise<void>

      A promise that fulfills when the animation is loaded

    • Add multiple animations to the Sprite or Visual.

      Parameters

      • atlases: {}

        an object with animation names as keys and an animation or animation atlas as values

      Returns Promise<void>

      A promise that fulfills when the animations are loaded

    • Add multiple animations to the Sprite or Visual.

      Parameters

      • spriteSheetURL: string

        the URL of the sprite sheet image

      • atlases: {}

        an object with animation names as keys and an animation or animation atlas as values

      Returns Promise<void>

      A promise that fulfills when the animations are loaded

    • Add multiple animations to the Sprite or Visual.

      Parameters

      • spriteSheetURL: string

        the URL of the sprite sheet image

      • frameSize: string

        the size of each frame in the sprite sheet in the format "WIDTHxHEIGHT" (example: "32x32")

      • atlases: {}

        an object with animation names as keys and an animation or animation atlas as values

      Returns Promise<void>

      A promise that fulfills when the animations are loaded

    • Adds a collider to the sprite's physics body.

      It accepts parameters in a similar format to the Sprite constructor except the first two parameters are x and y offsets, the distance new collider should be from the center of the sprite.

      This function also recalculates the sprite's mass based on the size of the new collider added to it. However, it does not move the sprite's center of mass, which makes adding multiple colliders to a sprite easier.

      Parameters

      • offsetX: number

        distance from the center of the sprite

      • offsetY: number

        distance from the center of the sprite

      • Optionalw: number

        width of the collider

      • Optionalh: number

        height of the collider

      • OptionalroundedRadius: number

        corner radius for a rounded box collider

      Returns void

    • Creates overlap sensors that are the same size as the sprite's colliders. If you'd like to add more sensors to a sprite, use the addSensor function.

      Used internally if a sprite overlap detection function is called but the sprite has no overlap sensors.

      Returns void

    • Adds an overlap sensor to the sprite's physics body.

      Sensors can't displace or be displaced by colliders. Sensors don't have any mass or other physical properties. Sensors simply detect overlaps.

      This function accepts parameters in a similar format to the Sprite constructor except the first two parameters are x and y offsets, the relative distance the new sensor should be from the center of the sprite.

      Parameters

      • offsetX: number

        distance from the center of the sprite

      • offsetY: number

        distance from the center of the sprite

      • Optionalw: number

        width of the collider

      • Optionalh: number

        height of the collider

      • OptionalroundedRadius: number

        corner radius for a rounded box sensor

      Returns void

    • Finds the minimum angular distance the sprite would need to rotate to face a position, taking into account the sprite's current rotation.

      Useful for rotateTowards-style logic where you need the signed delta rather than an absolute target angle.

      Parameters

      • x: number

        x coordinate

      • y: number

        y coordinate

      • Optionalfacing: number

        offset angle, default is 0

      Returns number

      the minimum angular distance to face the position

    • Finds the minimum angular distance the sprite would need to rotate to face a position, taking into account the sprite's current rotation.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionalfacing: number

        offset angle, default is 0

      Returns number

      the minimum angular distance to face the position

    • Finds the angle from this sprite to the given position.

      Equivalent to atan2(y - sprite.y, x - sprite.x) + facing. Returns the sprite's current rotation if the position is within 0.01 pixels.

      Can be used to set the direction of a sprite so it moves toward a position.

      Parameters

      • x: number

        x coordinate

      • y: number

        y coordinate

      • Optionalfacing: number

        offset angle added to the result, default is 0

      Returns number

      angle to the position

    • Finds the angle from this sprite to the given position.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionalfacing: number

        offset angle added to the result, default is 0

      Returns number

      angle to the position

    • Applies a force magnitude at the sprite's current bearing.

      Parameters

      • amount: number

        force magnitude

      • Optionalorigin: { x: number; y: number }

        point (relative to the sprite) where the force is applied. Accepts an object with x and y properties.

      Returns void

    • Applies a force vector to the sprite.

      Parameters

      • force: number[] | { x: number; y: number }

        force vector as an object with x and y properties or array of [x, y]

      • Optionalorigin: { x: number; y: number }

        point (relative to the sprite) where the force is applied. Accepts an object with x and y properties.

      Returns void

    • Applies a force scaled to the sprite's mass using a magnitude.

      Parameters

      • amount: number

        force magnitude

      • Optionalorigin: { x: number; y: number }

        point (relative to the sprite) where the force is applied. Accepts an object with x and y properties.

      Returns void

    • Applies a force scaled to the sprite's mass using a vector.

      Parameters

      • force: { x: number; y: number }

        force vector as an object with x and y properties

      • Optionalorigin: { x: number; y: number }

        point (relative to the sprite) where the force is applied. Accepts an object with x and y properties.

      Returns void

    • Apply a torque on the sprite's physics body. Torque is the force that causes rotation. A positive torque will rotate the sprite clockwise. A negative torque will rotate the sprite counter-clockwise.

      This function is the rotational equivalent of applyForce(). It will not imperatively set the sprite's rotation.

      Parameters

      • torque: any

      Returns void

    • Applies wind force to the sprite.

      Parameters

      • strength: number

        the strength of the wind

      • angle: number

        the angle the wind is blowing at

      • Optionaldrag: number

        the force that opposes the relative velocity

      • Optionallift: number

        the force that is perpendicular to the relative velocity

      Returns void

    • Applies a force to the sprite's center of mass attracting it to the given position.

      Parameters

      • x: number

        x coordinate

      • y: number

        y coordinate

      • Optionalforce: number

      Returns void

    • Applies a force to the sprite's center of mass attracting it to the given position.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionalforce: number

      Returns void

    • Changes the sprite's animation. Use addAni to define the animation(s) first.

      Parameters

      • name: string

        the name of the animation to switch to

      Returns void

    • Returns true on the first frame that the sprite no longer overlaps with the target sprite or group.

      Parameters

      Returns boolean

    • Returns true on the first frame that the sprite collides with the target sprite or group.

      Custom collision event handling can be done by using this function in an if statement or adding a callback as the second parameter.

      Parameters

      Returns boolean

    • Returns a truthy value while the sprite is colliding with the target sprite or group. The value is the number of frames that the sprite has been colliding with the target.

      Parameters

      Returns number

      frames

    • Deletes the Sprite from the sketch and all the groups it belongs to.

      When a sprite is deleted it will not be drawn or updated anymore. If it has a physics body, it will be deleted from the physics simulation.

      There's no way to undo this operation. If you want to hide a sprite use sprite.visible = false instead.

      Returns void

    • Removes colliders from the sprite's physics body.

      Returns void

    • Removes overlap sensors from the sprite's physics body.

      Returns void

    • Returns the distance to another sprite, the mouse, a touch pointer, or any other object with x and y properties. Uses q5's dist function.

      Parameters

      • x: number
      • y: number

      Returns number

      distance

    • Returns the distance to another sprite, the mouse, a touch pointer, or any other object with x and y properties. Uses q5's dist function.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      Returns number

      distance

    • Attempts to move the sprite to a destination at a constant speed and stops the sprite if it reaches the destination.

      The destination check is deferred until the sprite is estimated to be at the target position, based on world.physicsTime.

      Parameters

      • x: number | null

        destination x, or null to only move on the y-axis

      • y: number | null

        destination y, or null to only move on the x-axis

      • Optionalspeed: number

        movement speed in pixels per frame, defaults to the sprite's current speed or 1

      Returns PromiseLike<boolean>

      a lazy thenable that resolves true if the sprite reached the destination, or false if it didn't.

    • Attempts to move the sprite to a destination at a constant speed and stops the sprite if it reaches the destination.

      Parameters

      • pos: { x: number | null; y: number | null }

        destination object with x and y properties

      • Optionalspeed: number

        movement speed in pixels per frame, defaults to the sprite's current speed or 1

      Returns PromiseLike<boolean>

      a lazy thenable that resolves true if the sprite reached the destination, or false if it didn't.

    • Moves the sprite towards a position at a percentage of the distance between itself and the destination.

      Parameters

      • x: number | null

        destination x, or null to only move on the y-axis

      • y: number | null

        destination y, or null to only move on the x-axis

      • Optionaltracking: number

        percent of the distance to move towards the destination as a 0-1 value, default is 0.1 (10% tracking)

      Returns void

    • Moves the sprite towards a position at a percentage of the distance between itself and the destination.

      Parameters

      • pos: { x: number | null; y: number | null }

        destination object with x and y properties

      • Optionaltracking: number

        percent of the distance to move towards the destination as a 0-1 value, default is 0.1 (10% tracking)

      Returns void

    • Returns true on the first frame that the sprite no longer overlaps with the target sprite or group.

      Parameters

      Returns boolean

    • Returns a truthy value while the sprite is overlapping with the target sprite or group. The value returned is the number of frames the sprite has been overlapping with the target.

      Parameters

      Returns number

      frames

    • Returns true on the first frame that the sprite overlaps with the target sprite or group.

      Custom overlap event handling can be done by using this function in an if statement or adding a callback as the second parameter.

      Parameters

      Returns boolean

    • Sets a pass through contact relationship between the sprite and a target sprite or group.

      Parameters

      Returns void

    • Sets a pass through contact relationship between the sprite and a target sprite or group.

      Parameters

      Returns void

    • Plays an animation.

      You can put special modifier characters before the name:

      • "!" plays it backwards
      • ">" or "<" horizontally flips it
      • "^" vertically flips it

      Parameters

      • name: string

        the name of the animation to play

      Returns Promise<void>

      A promise that fulfills when the animation completes

    • Plays a sequence of animations.

      You can put special modifier characters before each ani name:

      • "!" plays it backwards
      • ">" or "<" horizontally flips it
      • "^" vertically flips it

      You can put sequence modifiers at the end of the sequence:

      • "**" loops it indefinitely
      • ";;" stops it on the last ani's last frame

      Parameters

      • ...sequence: string[]

        the names of animations

      Returns Promise<void>

      A promise that fulfills when the sequence completes

    • Applies a force to the sprite's center of mass repelling it from the given position.

      Parameters

      • x: number

        x coordinate

      • y: number

        y coordinate

      • Optionalforce: number

      Returns void

    • Applies a force to the sprite's center of mass repelling it from the given position.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionalforce: number

      Returns void

    • Recalculates the sprite's mass based on its current density and size.

      Returns void

    • Rotates the sprite by the smallest angular distance to a target angle at a constant speed, stopping when it arrives.

      Parameters

      • angle: number

        target rotation angle

      • Optionalspeed: number

        absolute rotation per frame, defaults to the sprite's current rotationSpeed or 1

      Returns PromiseLike<boolean>

      a lazy thenable that resolves true if the sprite reached the target angle, or false if it didn't.

    • Rotates the sprite by the smallest angular distance to face a position at a constant speed, stopping when it arrives.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionalspeed: number

        absolute rotation per frame, defaults to the sprite's current rotationSpeed or 1

      • Optionalfacing: number

        rotation angle the sprite should be at when "facing" the position, default is 0

      Returns PromiseLike<boolean>

      a lazy thenable that resolves true if the sprite reached the target angle, or false if it didn't.

    • Rotates the sprite to a target angle at a constant speed, stopping if it arrives.

      The destination check is deferred until the sprite is estimated to be at the target angle, based on world.physicsTime.

      Parameters

      • angle: number

        target rotation angle

      • Optionalspeed: number

        rotation speed in degrees (or radians) per frame, defaults to the sprite's current rotationSpeed or 1

      Returns PromiseLike<boolean>

      a lazy thenable that resolves true if the sprite reached the target angle, or false if it didn't.

    • Rotates the sprite to face a position at a constant speed, stopping if it arrives.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionalspeed: number

        rotation speed in degrees (or radians) per frame, defaults to the sprite's current rotationSpeed or 1

      • Optionalfacing: number

        rotation angle the sprite should be at when "facing" the position, default is 0

      Returns PromiseLike<boolean>

      a lazy thenable that resolves true if the sprite reached the target angle, or false if it didn't.

    • Rotates the sprite towards an angle.

      Parameters

      • angle: number

        angle in degrees

      • Optionaltracking: number

        percent of the distance to rotate on each frame towards the target angle, default is 0.1 (10%)

      Returns void

    • Rotates the sprite towards a position.

      Parameters

      • pos: { x: number; y: number }

        object with x and y properties

      • Optionaltracking: number

        percent of the distance to rotate on each frame towards the target position, default is 0.1 (10%)

      • Optionalfacing: number

        rotation angle the sprite should be at when "facing" the position, default is 0

      Returns void

    • Scales the the sprite.

      Components can be negative to flip/mirror the sprite on an axis.

      Parameters

      • x: number

        horizontal scale factor or uniform scale factor for both axes

      • Optionaly: number

        vertical scale factor

      Returns void

    • Efficiently sets the sprite's speed and direction at the same time.

      Parameters

      • speed: number
      • direction: number

      Returns void

    • Returns the sprite's unique identifier sprite.idNum.

      Returns string

      the sprite's id

    • Moves and rotates a sprite's physics body towards a target transform at a percentage of the distance on each frame.

      Uses Box2D's b2Body_SetTargetTransform for maximum efficiency compared to using moveTowards and rotateTowards separately.

      Parameters

      • x: number

        destination x

      • y: number

        destination y

      • Optionalrotation: number

        target rotation angle

      • Optionaltracking: number

        percent of the distance to move towards the target as a 0-1 value, default is 0.1 (10% tracking)

      Returns void

    • Moves and rotates a sprite's physics body towards a target transform at a percentage of the distance on each frame.

      Parameters

      • pos: { x: number; y: number }

        destination object with x and y properties

      • Optionalrotation: number

        target rotation angle

      • Optionaltracking: number

        percent of the distance to move towards the target as a 0-1 value, default is 0.1 (10% tracking)

      Returns void

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • Optionalx: number

        horizontal position

      • Optionaly: number

        vertical position

      • Optionalw: number

        width of the sensor

      • Optionalh: number

        height of the sensor

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • Optionalx: number

        horizontal position

      • Optionaly: number

        vertical position

      • Optionalw: number

        width of the sensor

      • Optionalh: number

        height of the sensor

      • OptionalroundedRadius: number

        corner radius for a rounded box sensor

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a circle overlap sensor.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • d: number

        diameter of the circle sensor

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a capsule overlap sensor.

      Parameters

      • points: [number, number][]

        array of two [x, y] points defining the capsule's endpoints

      • roundedRadius: number

        the radius of the capsule's rounded ends

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a chain overlap sensor defined by absolute vertex positions. If the first and last vertex are the same and the shape is convex, it becomes a polygon sensor.

      Parameters

      • vertices: [number, number][]

        array of [x, y] vertex positions

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a chain overlap sensor defined by relative vertex offsets from the sprite's position.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • vertices: [number, number][]

        array of relative [x, y] vertex offsets

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a chain overlap sensor defined by alternating segment lengths and angles. Each angle is relative to the previous segment's angle. If the last value is 5, the chain is closed into a loop.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • distAngles: number[]

        alternating segment lengths and relative angles; append 5 to close the chain

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a rounded chain overlap sensor defined by alternating segment lengths and angles. Each angle is relative to the previous segment's angle. If the last value is 5, the chain is closed into a loop.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • distAngles: number[]

        alternating segment lengths and relative angles; append 5 to close the chain

      • roundedRadius: number

        the rounded radius of the chain's segments

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with a regular polygon overlap sensor.

      Parameters

      • x: number

        horizontal position

      • y: number

        vertical position

      • sideLength: number

        the length of each side of the polygon

      • polygonName: string

        'triangle', 'square', 'pentagon', 'hexagon', 'septagon', or 'octagon'

      • OptionalroundedRadius: number

        corner radius for a rounded polygon

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • Optionalx: number

        horizontal position

      • Optionaly: number

        vertical position

      • Optionalw: number

        width of the sensor

      • Optionalh: number

        height of the sensor

      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • Optionalx: number

        horizontal position

      • Optionaly: number

        vertical position

      • Optionalw: number

        width of the sensor

      • Optionalh: number

        height of the sensor

      • OptionalroundedRadius: number
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • x: number

        horizontal position

      • y: number

        vertical position

      • d: number
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • points: [number, number][]
      • roundedRadius: number
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • vertices: [number, number][]
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • x: number

        horizontal position

      • y: number

        vertical position

      • vertices: [number, number][]
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • x: number

        horizontal position

      • y: number

        vertical position

      • distAngles: number[]
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • x: number

        horizontal position

      • y: number

        vertical position

      • distAngles: number[]
      • roundedRadius: number
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite

    • Creates a new sprite with an overlap sensor instead of a collider.

      Parameters

      • ani: string | Image | Ani
      • x: number

        horizontal position

      • y: number

        vertical position

      • sideLength: number
      • polygonName: string
      • OptionalroundedRadius: number
      • OptionalphysicsType: string

        physics type is DYNAMIC by default, can be STATIC or KINEMATIC

      Returns Sprite