Tutorial: Types

This tutorial will introduce you to Types.

What is a Type?

A type is a way of grouping all the variables required for a type of game object. A type is first declared and then game objects are created from the definition.

Declaring a Type

If you were to use individual variables to keep track of the data of a game object, it might require at least four :

ObjectX#
ObjectY#
ObjectXSpeed#
ObjectYSpeed#

Types allow you to organize all these variables into a single unit. Here's how to declare a type :

Type TObj
    x#
    y#
    xSpeed#
    ySpeed#
EndType

Creating a Game Object From a Type

Once the type has been declared, you can create a game object from the type this way :

Dim player As TObj

Initial Values

After the game object has been created, you can then assign it's initial values :

player.x# = GetScreenWidth() / 2
player.y# = GetScreenHeight() / 2
player.xSpeed# = 2
player.ySpeed# = 2

An Example

Here's an example of using a game object created from a type in a simple movement demo.

SetFPS 60

Type TObj
    x#
    y#
    xSpeed#
    ySpeed#
EndType

Dim player As TObj

player.x# = GetScreenWidth() / 2
player.y# = GetScreenHeight() / 2
player.xSpeed# = 2
player.ySpeed# = 2

Do

    If UpKey() Then player.y# = player.y# + -player.ySpeed#
    If DownKey() Then player.y# = player.y# + player.ySpeed#
    If LeftKey() Then player.x# = player.x# + -player.xSpeed#
    If RightKey() Then player.x# = player.x# + player.xSpeed#

    Cls 0
        Circle player.x#, player.y#, 10, False
    Sync
Loop
Categories: Beginner Tutorials : Programming Tutorials : Tutorials