Open Game Maker 2D Tutorial - Lesson 5





In lesson 5, we will learn how to create a sprite with custom properties and custom event.


 


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:

gameres/button.bmp


And please follow Step 2 in Lesson 4 to add "button.bmp" to the game.




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





Select "button.bmp" to add.




This time we will not set the color key for the image.





Click "OK" button to finish adding image resource.







Step 3 - Add new sprite with custom property


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




In popup dialog, just input the "Sprite Name" and then click the "Add" button on the "Custom properties" tab sheet.





In popup dialog, set a string property named "Caption" for the sprite. And please also set its default value as "Label".





Click "OK" button to create the new sprite which has one custom property but has no any animations.





Select "Label" sprite in the sprite list and double click its "OnDraw" event.





And the script code of the "OnDraw" event will be added automatically.





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

    int iSprId = OGE_GetSpr();
   
    int iPosX = OGE_GetPosX(iSprId);
    int iPosY = OGE_GetPosY(iSprId);
   
    int iScreen = OGE_GetScreenImage();
    OGE_SetPenColor(iScreen, 0);
   
    Label label(iSprId);
   
    OGE_DrawText(iScreen, label.Caption, iPosX, iPosY);


Please note: the text in bold is showing you how to access the custom property of the sprite.





Click "Save file" button to save the script file.





Drag and drop the "Label" sprite to the scene.








And we may try to change the value of the custom property.





Click "Run scene" button to test it.





And the result screen should be like this:











Step 4 - Add new sprite with custom event

Please follow Step 3 in Lesson 4 to create a sprite named "Button" with the "button" image ("Panel" type, one animation with one frame).

And before click "OK" button to create the sprite, please select the "Custom events" tab and click the "Add" button on it.





In the popup dialog, input a custom event named "OnClick".





Then finally click "OK" button to create the sprite




Select "Botton" sprite in the sprite list and double click its "OnMouseUp" event.




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

    Button btn;
    btn.DoClick();





Now double click its "OnClick" event, and add the following script in blue.

    OGE_Print("Say hello!\n");





Click "Save file" button to save the script file.



Drag and drop the "Button" sprite to the scene, double click its "OnClick" event.








Add the following script in blue to the "OnClick" event.

    OGE_CallBaseEvent();
    OGE_Print("Say goodbye!\n");





Click "Save all files" button to save all changes.







And then click "Run scene" button to test it.



If all is OK, when you click the button, you can see some text printed out in the console box.








Okay, that's all of lesson 5. If you have any question about this lesson, please post them on the forum.


Thank you and see you later.