• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Changing Sprite Properties/Events

J

Jaften

Guest
Hello all. Complete beginner here so bare with me.

I have, and am very proud of this (thats how new I am!), worked out how to change an object's sprite at the press of a button. The next stage of my game mechanic is proving tricky though. I am looking for the object to have different events based on which sprite it currently takes the form of.

So when it is sprite A it can do x, when it is sprite B it can do y and when it is sprite C it can do z.

I thought about creating 3 objects, but is there a way to switch between objects (as the main sprite) at the press of a button, or does any other solution come to mind?

Hope this makes sense and greatly appreciate any advice to point me in the right direction.
Thanks in advance.
 
A

Aura

Guest
If you want to use different objects for that, you can use instance_change().

http://docs.yoyogames.com/source/da...ances/instance functions/instance_change.html

But if you can handle all of this in a single object then you can use an if-else statement to perform different actions depending on the current sprite.

Code:
if (sprite_index == spr_A) {
   //Do x...
}
else if (sprite_index == spr_B) {
   //Do y...
}
else if (sprite_index == spr_C) {
   //Do z...
}
 
D

Dantevus

Guest
I am relatively new to this particular software, so someone might have a much easier way, but my first thought is:

if (sprite_index = blahdeeblah)
{
STUFF
}
else if (sprite_index = blahdeeblah)
{
STUFF
}
else if (sprite_index = blahdeeblah)
{
STUFF
}

If you are looking for them to have completely different events you can separate those into one if statement that if not met, nothing occurs.

-Dantevus
 
G

Gre_Lor12

Guest
Welcome to the community Jaften! :D

Couldn't he/she use a case statement to keep it cleaner? I forget when case statements cannot be used:

Code:
switch (sprite_index)
   {
   case Spr_A:
        //DO X
        break;

   case Spr_B:
        //DO Y
        break;

   case Spr_C:
        //DO Z
        break;
   }
Jaften this is essentially multiple if statements in one, making it look cleaner.

I don't know if switch statements can use Sprite Index's though... I would need to check lol
My apologies if I am wrong.
 
J

Jaften

Guest
Thank you brilliant people. Ill be trying these out tonight
 
Top