Open Game Maker 2D Tutorial - Lesson 4

In lesson 4, we will return to the general development in OGM2D.

That means you have to learn how to write scripts from now on.

And lesson 4 will show you how to create a sprite and control it by writing scripts.


Step 1 - Create a new empty game

Please follow Step 1 in Lesson 1 to create a new empty game.

Step 2 - Add image resource to the game

Please first download the image resource from the following link:


Active the image resource list and click the "Add" button to start to add image resource.

In the popup dialog, click "Add Images" button.

Select "plane.bmp" to add.

Click "OK" button to finish adding image resource.

Step 3 - Add new sprite to the game

Active the sprite resource list and click the "Add" button to start to add a new sprite.

In popup dialog, after input the "Sprite Name" and "Sprite Type", please click the "Add" button in the animation group box.

Then an animation dialog will be shown. And please first click the "..." button to select an image for the animation.

Select the "plane" image and click "OK" button.

Click "Locate the strip and frames" button to set the strip and frames of the animation.

In the popup dialog please click "Use whole image as the strip" first.

Then fill the "Row Count" and "Column Count" and click "OK" button.

After set the position and size of the frames, we need to set the time of the freames too.

And then, please click the following 4 buttons to set the origin and bounding box (for collision detection).

Click the "OK" button to add this animation to the sprite.

Finally, let's click the "OK" button of the sprite dialog to create the new sprite.

Now we may drag and drop the new sprite to the scene to test it.

Click "Run scene" button to see whether the "plane" sprite works well or not.

About the details of the fields in all the sprite and animation dialogs above, you may refer to the manual of OGM2D:

Step 4 - Move the sprite by script

Select "Plane1" in the scene, and double click its "OnUpdate" event.

Then the script code of the "OnUpdate" event will be added automatically.

Please copy the following script in blue and paste them into the "OnUpdate" event.

    int iSprId = OGE_GetSpr();
    int iSpeed = 2;
    if (OGE_IsKeyDown(Key_Up))    OGE_MoveY(iSprId, 0-iSpeed);
    if (OGE_IsKeyDown(Key_Right)) OGE_MoveX(iSprId, iSpeed);
    if (OGE_IsKeyDown(Key_Down))  OGE_MoveY(iSprId, iSpeed);
    if (OGE_IsKeyDown(Key_Left))  OGE_MoveX(iSprId, 0-iSpeed);

And then click "Run scene" button to test the script in "OnUpdate" event.

If it is OK then you should be able to use the keyboard to control the player. The arrow keys control its movement.

About the details of the sprite events, you may refer to the manual of OGE2D:

About the details of the sprite API functions, you may also refer to the manual of OGE2D:

Okay, that's all of lesson 4. See you later.