Create, Compete & Win at Redbrick Connect 2024! 🎉

Sprite

APIs that are added to Sprite (GUI) objects.

Properties

The properties of a sprite are based on the area of the sprite that is its parent, or if the parent is a GUI Scene, the entire screen area.

.anchor

.anchor : Object { x, y }

The location the sprite’s offset is relative to.

  • x : The reference position in screen horizontal orientation. Default is “Center”.
    - “Far-Left” : Left end of parent.
    - “Center” : Horizontal center of parent.
    - “Far-Right” : Right end of parent.
  • y : The reference position in screen vertical orientation. Default is “Center”.
    - “Bottom” : Bottom end of parent.
    - “Center” : Vertical center of parent. - “Top” : Top end of parent.

.offset

.offset : Object { x : { value, unit }, y : { value, unit } }

The offset from the sprite’s anchor position.

  • x
    - value : Screen horizontal offset value. Default is 0.
    - unit : Screen horizontal offset units. Default is “px”.
        - “px” : Absolute pixel unit.
        - “%” : A relative unit based on the width of the parent.
  • y
    - value : Screen vertical offset value. Default is 0.
    - unit : Screen vertical offset units. Default is “px”.
        - “px” : Absolute pixel unit.
        - “%” : A relative unit based on the width of the parent.

.size

.size : Object { x : { value, unit }, y : { value, unit } }

The size of the sprite.

  • x
    - value : The width (length in the horizontal direction) value. - unit : Width unit. Default is “px”. - “px” : Absolute pixel unit. - “%” : A relative unit based on the width of the parent.
  • y
    - value : The height (length in the vertical direction) value.
    - unit : Height unit. Default is “px”.
        - “px” : Absolute pixel unit.
        - “%” : A relative unit based on the width of the parent.

.constantAspectRatio

.constantAspectRatio : Boolean

If true, the sprite will maintain its original aspect ratio. The side of the width and height that exceeds the aspect ratio will be reduced.

Methods

.setText()

.setText(text)

Sets the text that appears in the sprite.

  • text : Text content.

.setTextSize()

.setTextSize(size)

Sets the text size that appears in the sprite.

  • size : The size of the text. The unit is px.

.show()

Make the sprite visible.

.hide()

Make the sprite invisible.

.onClick()

.onClick(callback)

Sets the specified callback function to run when the sprite is clicked.

  • callback : The function to execute when clicked. We recommend using the arrow function to maintainthis context.