Login | Register

User changed sprites

August 30, 2007, 20:18 by GeoS
[loading]
-->
For this article, I'll explain how you can allow the user to change a sprite to whatever they want.

The obvious way to some may be to make the sprites external, and then just let the user mod out of game. However, this will allow them to do it in game.

We will have spr_main and obj_main. Now, make spr_main anything; just make it visible and clickable. Make it the sprite of obj_main. Now, let's code obj_main:

Create Event:

var var_main;
sprite_assign(spr_mainRes,spr_main);

So that's pretty self-explanitory, we assign a variable var_main. We also copy spr_main to spr_mainRes, we'll see why later.

Right Mouse Button Pressed Event:

mainMenu = show_menu_pos(x,y,'Change Sprite|Restore Sprite|Delete Instance',10);

if (mainMenu = 0)
{
var_main = get_open_filename("Bitmap (*.bmp)|*.bmp|"+"PNG (*.png)|*.png|",0);
sprite_index=spr_main;
sprite_replace(spr_main,var_main,0,0,1,0,0,16,16);
}

if (mainMenu = 1)
sprite_index=spr_mainRes;

if (mainMenu = 2)
instance_destroy();


We'll go through this, line by line.

mainMenu = show_menu_pos(x,y,'Change Sprite|Restore Sprite|Delete Instance',10);
When the right mouse button is pressed on obj_main, the right-mouse menu comes up (click your right button... yes, that menu). 'Change Sprite|Restore Sprite|Delete Instance' are the menu buttons, spearated by "|". Show_menu_pos returns a value according to what button is pressed, so Change Sprite is 0, Restore sprite is 1, etc. The last argument, 10, is what is returned if nothing is selected. So we assign what button is pressed in the menu to mainMenu.


if (mainMenu = 0)
{
var_main = get_open_filename("Bitmap (*.bmp)|*.bmp|"+"PNG (*.png)|*.png|",0);
sprite_index=spr_main;
sprite_replace(spr_main,var_main,0,0,1,0,0,16,16);
}


So, if mainMenu returns 0, or 'Create Sprite', we use get_open_filename, and make the file the user opened assign to var_main (the location, to be precise). We make sure the sprite is spr_main, and then we replace spr_main with the file of var_main. Look in your Gm Manual for more info about get_open_filename.


if (mainMenu = 1)
sprite_index=spr_mainRes;


If mainMenu returns 1, or 'Restore Sprite', we change the sprite to spr_mainRes.


if (mainMenu = 2)
instance_destroy();


Finally, if mainMenu returns 2, or 'Destroy Instance', we, well, destroy the instance.

There is some core basics. Hopefully you understood it, and can develop on it to suit your needs. If anything doesn't work or is out of place, tell me! I just about wrote this as I went along with it, and when it's this long, things can go wrong.

Additional Excercises:
1. Make another option in the menu that allows the player to move the instance.
2. Make yet another option in the menu that will change the alpha of the instance.
3. Make an object button, that when pressed, will create an instance with a user defined sprite, and then make that created instance have an improved version of the menu code we used here.

Watch out for my future articles, as I might go through game creators. I'm not going to doom myself by saying 'my NEXT article', but it might be, who knows...

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games