GXEntityCreate - boxgaming/gx GitHub Wiki

Creates a new game entity. In GX an entity is an object that can be placed into and interact with the game world. An entity has an x and y position, a height and width, as well as basic physics properties like velocity. An entity is created from a spritesheet image. Each row of the spritesheet should contain a separate animation sequence.

Syntax

entityId% = GXEntityCreate (imageFilename$, width%, height%, sequenceFrames%)

GXEntityCreate imageFilename$, width%, height%, sequenceFrames%, uid%

Parameters

  • The imageFilename% parameter indicates the path to the spritesheet image which will contain the animation sequences for the entity.
  • The width% and height% parameters define the size of a single animation frame for the entity.
  • The total number of animation frames, which will correspond to the number of rows of images in the spritesheet should indicated with the sequenceFrames% parameter.
  • The uid% parameter allows the application to specify a 10-character STRING identifier for the entity. This value can be used to lookup the entity's id with the GX function. This uid must be unique across all objects created in GX.

Examples

Example1: Create and animate an entity. This example uses the FUNCTION version of GXCreateEntity in which the entity id is returned to the calling application. Typically, this id is stored in a SHARED LONG variable so that it may be referenced in game event functions.

CONST SEQ_IDLE = 1
CONST SEQ_WALK_RIGHT = 2
CONST SEQ_WALK_LEFT = 3

GXSceneCreate 320, 200

DIM SHARED warhog AS LONG
warhog = GXEntityCreate("img/warhog.png", 16, 16, 5)
GXEntityAnimate warhog, SEQ_IDLE, 10

Example2: Create a new entity. When the user presses the right arrow key move the entity right. This example uses the SUB version of GXCreateEntity which requires a unique string id be specified. This uid can be used later to lookup the id of the entity.

'$INCLUDE: 'gx.bi'
CONST SEQ_IDLE = 1
CONST SEQ_WALK_RIGHT = 2
CONST SEQ_WALK_LEFT = 3

GXSceneCreate 320, 200

DIM SHARED warhog AS LONG
warhog = GXEntityCreate("img/warhog.png", 16, 16, 5)

SUB GXOnGameEvent (e AS GXEvent)
    SELECT CASE e.event

        CASE GXEVENT_UPDATE:

            IF GXKeyDown(GXKEY_RIGHT) THEN
                GXEntityVX GX("warhog"), 2
                GXEntityAnimate GX("warhog"), SEQ_WALK_RIGHT, 10
            END IF

    END SELECT
END SUB
'$INCLUDE: 'gx.bm'

See Also

GX GXEntityVX GXEntityVY GXEntityAnimate