Skip to main content

Game Builder Blocks - Sprites Tab

This article will dive deeper into sprites in Game Builder. Specifically, their properties and functions.

D
Written by David Burnham
Updated this week

The sprites are game objects that can be considered the lifeblood of the game. Add as many sprites as needed (different sprites or the same ones). The sprites have speed and scale. The world’s gravity can apply on sprites.

Each sprite has a sprite sheet. The sprite sheet is made up of frames. When an animation runs, for example animation of "step", the frames of the sprite sheet are shown in a specified order to cause the illusion that the sprite is moving. You can preview the different images by clicking the Show Preview link on the sprite properties page.

Setting Properties from the Sprites' Setting Icon

The following properties can be updated from the settings button of each sprite. A sprite is added with all the default values. When the settings change, the sprite is affected immediately. For example, if the Show option is unchecked, the sprite is hidden from the screen. The sprite starts with these settings when the game first runs.

Some sprites’ properties can only be set from the sprites settings icon, some can only be set in the code, while others can be set in both places.

In the table below, the column "See Also", names the block that can be used in the code, to set the property.

You can also drag a sprite on the screen to set its x,y values.

Name

Values

Used For

See Also

Name

The default name is the spritesheet’s name (i.e monkey, frog, etc).

Name the sprite. Refer to the sprite with this name in different parts of the code.

X

Based on world’s size.

Default: 300

The sprite’s x position.

Set X

Y

Based on world’s size.

Default: 200

The sprite’s y position.

Set Y

Rotation

0 - right (default)

90 - down

180 - left

270 - up

The direction in degrees that the sprite points to (which way the sprite is looking).

Set Rotation

Allow Gravity

Checked - gravity affects the sprite.

Unchecked - gravity doesn’t affect the sprite.

This option applies only when the game has gravity.

If the sprite should "stand in the air" and not let gravity affect it, then uncheck this option.

Set Allow Gravity

Immovable

Checked - sprite will not move during a collision event.

Unchecked - sprite will move during a collision event (default).

This option applies when two sprites collide.

Check this option when a sprite should not move during a collision with another sprite.

Collide world bounds

Checked - sprite cannot move beyond world bounds (default).

Unchecked - sprite can move beyond world bounds.

This option defines whether the sprite collides with the world’s bounds or if it can move beyond it.

Collide other sprites

Checked - sprite will not be cross the boundary of another sprite and will not be able to occupy the same space (default).

Unchecked - sprite can overlap other sprites.

Note: For a sprite to collide with another sprite, both sprites need to have this option checked.

This option defines whether two sprites will be able to pass through each other or if they will stop when they get to each other.

Draggable

Checked - sprite can be clicked, dragged, and dropped to a new position while the game is running.

Unchecked - sprite can not be moved by dragging while the game is running (default).

This option will allow the user to move the sprite during the game by clicking and dragging the sprite.

Show

Checked - sprite is shown in the game (default).

Unchecked - sprite is hidden, not shown in game.

Show or hide the sprite on the game screen.

To start a game with the sprite hidden in the game, uncheck this option.

Show

Hide

Blocks

The table below lists blocks that can be used in the code.

The table’s column: Category is the name of the tab where the block appears. The tabs can be found in the game builder, under the code editor.

The column titled Physics states whether the block is available in the arcade option, P2 option or both.

The events’ blocks (event handlers) are called when a specific event happens (for example when a key is pressed on the keyboard or when two sprites collide). Events are predefined. Event handlers may be defined in the code. Event handlers are called at the occurrence of the corresponding event. When an event handler is not specified in the code, the corresponding event goes unhandled.

List of Blocks

Block Name

Category

Description

Physics

Events

Called when a key is pressed on the keyboard.

Both

Events

Called when the screen is swiped.

Both

Events

Called when two sprites collide (bump) with each other.

Both

Events

Called when a draggable object is dragged and the motion from the drag stops.

Both

Events

Called when a sprite collides with one of the world bounds.

Both

Events

Called when the sprite collides with a tile in the tilemap.

P2

Events

Called when the sprite is clicked on (using the mouse).

Both

Events

Called when the game window is clicked on (using the mouse).

Both

Events

Called on each frame change.

Both

Movements

Makes the sprite move.

Arcade

Movements

Makes the sprite jump.

Arcade

Movements

Sets the sprite’s x position.

Both

Movements

Sets the sprite’s y position.

Both

Movements

Returns the sprite’s x position.

Both

Movements

Returns the sprite’s y position.

Both

Movements

Changes the sprite's X position by a specified amount.

Both

Movements

Changes the sprite's Y position by a specified amount.

Both

Movements

Rotates the sprite.

Both

Movements

Returns the sprite’s rotation.

Both

Movements

Changes the sprite's rotation by a specified amount.

Both

Movements

Sets the sprite’s speed.

Arcade

Movements

Applies a thrust force on the sprite.

P2

Movements

Rotates the sprite to the left.

P2

Movements

Rotates the sprite to the right.

P2

Movements

Returns the sprite’s velocity in the x axis.

P2

Movements

Returns the sprite’s velocity in the y axis.

P2

Movements

Sets the value for the Allow Gravity setting in the program.

Both

Movements

Returns the distance to the other sprite specified.

Both

Movements

Gets value from another sprite.

Both

Display

Shows the sprite.

Both

Display

Hides the sprite.

Both

Display

Destroys the sprite.

Both

Display

Disables the spite from the game.

Both

Display

Brings back a disables sprite.

Both

Display

Sets the sprite’s scale.

Both

Display

Gets the sprite's scale.

Both


On Key

The On Key block (event handler) is called each time a key is pressed on the keyboard. This block can be used to control elements of the game using the keyboard. For example: moving a sprite by pressing keys on the keyboard.

The block is added in a sprite’s code and the key that you want to trigger the event is selected in the drop down.

Arguments

Name

Type

Description

Key

Drop down

The key that was pressed on the keyboard.

Example

In the example below, when the right arrow key on the keyboard is pressed, the sprite moves 1 step.


On Swipe

The On Swipe block is called each time a touch-screen is swiped. This block allows the user to control behaviors of the sprites or the game by swiping the screen.

For example, to move the sprites in the direction of the swipe. Pressing an arrow key on the keyboard throws swipe event with the correct direction (for example, pressing on the right arrow key throws a swipe event with right as the direction).

The block should be added in a sprite’s code.

Arguments

Name

Type

Description

Direction

Drop Down

The direction of the swipe.

Example

In this example, when the screen is swiped to the right, the sprite rotates to the right and moves.

Tip

When no touchpad is available, use the arrow keys on the keyboard to simulate a swipe event (and trigger the On Swipe block).


On Collide

The On Collide block (event handler) is called each time two sprites collide. The On Collide block is added in one of the colliding sprite’s code. The second sprite is chosen in the drop down. The block should be added in a sprite’s code.

Arguments

Name

Type

Description

Sprite

Drop Down

The sprite that this sprite collided with.

Example

If this code is placed in the banana sprite's code, then it will jump anytime the banana collides with the monkey.

Tip

The On Collide block can be added in either one of the sprite’s code. It should be added in the code of the sprite whose code is easier to maintain or in the sprite that might be duplicated.

For example: if the game includes a monkey and two bananas.

Option 1: The On Collide block is added in the monkey sprite’s code. In this case, two On Collide blocks need to be added (one with banana1 and the other with banana2).

Option 2: On Collide block is added in the banana sprite’s code. The banana sprite is then duplicated. This way, the On Collide block, is duplicated as well.


On Drag End

The On Drag End block (event handler) is called each time a draggable sprite is dragged and then stops being dragged. It is important to note that this is triggered when the dragging motion stops, not when the sprite is released. This block should be defined in a sprite's code and will only execute when that particular sprite is dragged.

On Drag End is only applicable when the Draggable option is enabled in the sprite options.

Example

In the example below, when the sprite is dragged and then stops moving, gravity is set to true.


On Collide With World Bounds

The block is called when a sprite collides with one of the world’s boundaries. The direction argument specifies the side of the boundary to take the action on. The block should be defined in a sprite’s code.

When using P2 physics, the On Collide World Bounds has no arguments, it will be called when the sprite collides with any world bounds.

Arguments

Name

Type

Description

Directions

Drop Down

The boundary which the sprite collided with.

Example

In this example, if the sprite runs into the left boundary, it will move 5 steps.


On Collide With Tilemap

Applicable only in P2 physics.

The block is called when a sprite collides with a tile. The block has no argument. This means that when a sprite collides with any of the tiles in the game, the same set of instructions will run. The block should be defined in a sprite’s code.

Example

In this example, when the sprite collides with any tile in the tilemap, the sprite is destroyed.


On Click

This block is called when a sprite is clicked on. A click of the mouse on other areas or sprites will not be handled by the On Click block, written in this sprite’s code. This block should be defined in a sprite’s code.

Example

In this example, when the sprite is clicked on, the sprite will jump.


On Game Tap

The block is called when a mouse click is recorded anywhere in the game. This event is triggered for all clicks, even if you click on another sprite with a click event. This block should be defined in a sprite’s code.

Example

In this example, when the game window is clicked, the sprite will jump.


On Update

The block is called on each frame change. Define this block when you need something to happen each time a frame changes. For example, if you want to display the sprite’s velocity each time a frame changes. This block should be defined in a sprite’s code.

Example

In this example, on each frame change, the sprite will take a step forward.


Step

Applicable only in Arcade physics.

Makes the sprite move. The argument of this block defines the number of pixels the sprite will move.

Arguments

Name

Type

Description

Number of Steps

number

The number of pixels to move.

Example

If the following code is written in a monkey sprite code section, then the monkey will move 100 pixels to the right when the program is run.


Jump

Applicable only in Arcade physics.

Makes the sprite jump. The sprite will jump to a certain height (based on the gravity of the game). The argument defines how many times higher (or lower) the sprite will jump, but the height will always still depend on the gravity setting.

Arguments

Name

Type

Description

How High

number

How high (or low) the sprite will jump.

Example

If the following code is written in a monkey sprite code section, then the monkey will jump with a power of 1. The height will depend on the gravity setting.

Tip

When the game has no gravity (gravity = 0), the sprites will not jump.


Set X

Sets the sprite’s x position based on the argument of the block. The sprite is placed at the new position.

Arguments

Name

Type

Description

New X

number

The sprite’s new x position.

Example

If the following code is written in the monkey’s sprite, then the monkey is placed at an X value of 300.


Set Y

Sets the sprite’s y position based on the argument of the block. The sprite is placed at the new position.

Arguments

Name

Type

Description

New Y

number

The sprite’s new y position.

Example

If the following code is written in the monkey’s sprite, the monkey will be placed at a Y value of 200.


Get X

Returns the sprite’s x position so that it can be used in the program.

Returns

Number - the sprite's x position.

Example

If the following code is written in the monkey’s sprite, the monkey will be placed at an x position 100 more than the current position.


Get Y

Returns the sprite’s y position so that it can be used in the program.

Returns

Number - the sprite's y position.

Example

If the following code is written in the monkey’s sprite, the monkey will be placed at a y position 100 more than the current position.


Change X By

Changes the sprite’s x position by the specified amount.

Arguments

Name

Type

Description

Change Amount

number

The amount the x position will change from the current position.

Example

If the following code is written in the monkey’s sprite, the monkey will be placed at an x position 100 more than the current position.


Change Y By

Changes the sprite’s y position by the specified amount.

Arguments

Name

Type

Description

Change Amount

number

The amount the y position will change from the current position.

Example

If the following code is written in the monkey’s sprite, the monkey will be placed at a y position 40 more than the current position.


Set Rotation

Rotates the sprite based on the argument.

Arguments

Name

Type

Description

Degrees

number

The direction in degrees of where the sprite should rotate to.

For example, the following values set the sprite’s rotation to:

  • 0 - right

  • 90 - down

  • 180 - left

  • 270 - up

Example

If the following code is written in the monkey’s sprite then the monkey is rotated to face right. (its current rotation has no effect on the result)

Tip

The argument degrees is the absolute value where the sprite should be rotated to and is not affected by the current sprite’s rotation.


Get Rotation

Returns the sprite’s rotation so that it can be used in a program.

Returns

Number - the rotation of the sprite in degrees.

Example

If the code below is written in the frog’s sprite, then the frog will rotate 180 degrees away from where it was previously facing.

Specifically, if the frog is facing right or is rotated to 180 degrees, the following code will cause the frog to rotate 360 degrees so that it faces right. Current rotation (180 degrees + 180 degrees = 360 degrees). After the Set Rotation instruction, the frog will face right.


Change Rotation By

Changes the sprite’s rotation by the specified amount.

Arguments

Name

Type

Description

Change Amount

number

The number of degrees to rotate the sprite in a clockwise direction. Every 90 degrees will point the sprite in a new direction.

Example

If the following code is written in the monkey’s sprite, the monkey will be turned 90 degrees clockwise. For example if the monkey was facing down, it will now face left.


Set Speed

Applicable only in arcade physics.

Sets the speed of the sprite. This block changes the speed of the sprite in multiples of its default speed.

Arguments

Name

Type

Description

Speed

number

How many times faster (or slower) the sprite will move compared to the default speed.

  • 1 - default speed

Example

If the following code is placed in a monkey sprite's code section, the monkey will move 3 times the default speed.


Thrust

Applicable only in P2 physics.

Applies thrust force to the sprite which pushes the sprite. For example, the thrust can push the sprite forward.

Arguments

Name

Type

Description

Force

number

How much force to apply to the sprite.

Example

In the following example, if the gravity of the game is set to 1000, the sprite will be pushed forward. If the gravity of the game is set to 2000, the sprite will not be pushed forward.

Tip

The force applied should be higher than the gravity of the game, otherwise the force will not be enough to overcome the gravity and the sprite will not be moved.


Rotate Left

Applicable only in P2 physics.

Rotates the sprite to the left. Angular damping is applied on the sprite: Every frame, half of the sprite's velocity is lost. This prevents the sprite from rotating forever.

Arguments

Name

Type

Description

Speed

number

The speed in which to rotate the sprite to the left.

Example

Rotates the sprite to the left when the left arrow key is pushed.


Rotate Right

Applicable only in P2 physics.

Rotates the sprite to the right. Angular damping is applied on the sprite. Every frame, half of the sprite's velocity is lost. This prevents the sprite from rotating forever.

Arguments

Name

Type

Description

Speed

number

The speed in which to rotate the sprite to the right.

Example

Rotates the sprite to the right when the right arrow key is pushed.


Get Velocity X

Applicable only in P2 physics.

Returns the sprite’s velocity in the x axis so that it can be used in the program.

Returns

Number - the velocity in the x axis of the sprite.

Example

In this code, the Get Velocity X is retrieved and compared in a conditional statement.

Tip

When the sprite is not rotated to the right or to the left, its x velocity will be zero.


Get Velocity Y

Applicable only in P2 physics.

Returns the sprite’s velocity in the y axis.

Returns

Number - the velocity in the y axis of the sprite.

Example

In this code, the Get Velocity Y is retrieved and compared in a conditional statement.


Set Allow Gravity

Sets the sprite’s Allow Gravity to either true or false. The original value is set under the sprite properties, but this allows it to be changed as the program runs. This only updates gravity for the sprite that the code is executed on.

Arguments

Name

Type

Description

Gravity

Boolean

Set to true, gravity will be turned on for this sprite, false it will not.

Example

If the following code is written in the monkey’s sprite, the monkey will be impacted by gravity.


Get Distance From

Returns the distance between two sprites as a positive number so that it can be used in the program. The first sprite is the sprite where the code exists and the second sprite is designated as the input argument.

Returns

Number - a positive value representing the distance between the two objects.

Arguments

Name

Type

Description

Other Sprite

Sprite

Returns the distance between the two sprites

Example

If the following code is written in the monkey’s sprite, the monkey will jump if it comes within 100 of the banana.


From ... Get

This block allows you to retrieve either the X, Y, or Rotation value from another sprite in the program. That can then be used to elsewhere in your program.

Returns

Number - the value of the option selected as the second argument.

Arguments

Name

Type

Description

Other Sprite

Drop Down

The other sprite to check the distance from

Value to Get

Drop Down

Choice between X, Y, or Rotation

Example

If the following code is written in the monkey’s sprite, the monkey's X position will update to the same X position as the banana when the program runs.


Show

Shows the sprite or the widget.

Example

If this is written in the monkey’s sprite, then the monkey is shown. If this is written in the Timer widget, then the timer is shown.


Hide

Hides the sprite or the widget.

Example

If this is written in the monkey’s sprite, then the monkey is hidden. If this is written in the Timer widget, then the timer is hidden.

Tip

When hiding a sprite, it still exists in the same position, but it is not shown. This means, for example, that another sprite can still collide with the hidden sprite. To make the sprite disappear but with the option to bring back, use the Enable/Disable blocks.


Destroy

Deletes the sprite.

Example

If this is written in the banana sprite, then the banana is destroyed (deleted) when it collides with the monkey.

Tip

Pay attention that events like On Key and On Collide will not be triggered once the sprite is destroyed.


Disable

Disables the sprite by removing it from the game. Unlike a hidden sprite (using the Hide block), other sprites will not be able to collide with a disabled sprite. Unlike a destroyed sprite (using the Destroy block), a disabled sprite can be brought back to the game by using the Enable block.

Example

If this is written in the monkey’s sprite, then the monkey is disabled (hidden without other sprites colliding with the monkey) if it collides with the left boundary.


Enable

Brings back a disabled sprite.

Example

If this is written in the monkey’s sprite and the monkey was disabled, then the monkey is brought back to the game when the game window is clicked.


Set Scale

Sets the scale of the sprite. The block changes the scale of the sprite based on its default scale.

Arguments

Name

Type

Description

Scale

number

How many times bigger or smaller than the default size, the sprite will be.

  • 1 - default scale

Example

In this example, the sprite's scale will be set to the current scale times 1.2 each time the game window is tapped. This will essentially cause the sprite to grow 20% each click.


Get Scale

Returns the current scale of a sprite so that the value can be used elsewhere in the program.

Example

In this example, the sprite's scale will be set to the current scale times 1.2 each time the game window is tapped. This will essentially cause the sprite to grow 20% each click.


Relevant Articles:

Did this answer your question?