• 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 object's sprite with Alarm? (SOLVED IN COMMENTS)

P

Power_Michael

Guest
Hey everyone, I currently need help with "changing an object's sprite with an Alarm". But to make things trickier, I need the alarm (or several alarms) to change the sprite of the same object several times....about 4 times. Think of a tree...it starts as a little spr_sapling....then alarm 0 changes it to spr_smallTree....then what? I have a general understanding of GML...but alarms are not my best friend so if anyone has a solution to making multiple sprite changes to a single object with alarms....that would be great...and below I will put some other solutions I thought of as well as the MUCH NEEDED source code so you guys can see what I am dealing with.

Other solution (OBJECTS OVER ALARMS?): I want to make a tree grow from a sapling to a big tree...and I have 4 stages, or 4 sprites. So I thought...I could just make the sapling have an alarm that counts down and makes it turn into stage 2...or sprite 2. A bigger tree...and so on...but because I am awful with alarms I was thinking how about I just make it change OBJECT! I have done this thing before...but while I do think there are better ways doing this...like with alarms...the idea of just making the object change to another object with the next sprite...and then that object changes into the next object and so on...

Before the code is displayed, just note I already have one successful sprite change. The problem is....or what I think i need to do...is after one alarm...not only does the sprite change, but the alarm triggers another alarm...if that is possible....let me know...here is the code:

Code:
For the Create event:

I have a drag and drop "alarm[0]" action...it takes 1000 steps....this is the only drag and drop I have...so moving on


Step Event
Code:
if(alarm[0] == -1) {
    alarm[0] = room_speed*10;
}
Alarm[0]

Code:
if(alarm[0] == -1) {
    alarm[0] = room_speed*50;
}
with(obj_player)
{
    sprite_index = spr_smallTree;
}

alarm[0] = -2;

if alarm[0] = -2
{
   
}

That is all...if you want me to interpret something better or have any more questions for me, just comment below...Thank you so much!

-Michael
 
S

Supercoder

Guest
Your problem is in the alarm event. When an alarm event triggers, the variable is actually set to 0, not -1. Just take out that if statement in the alarm code and you should be good.

edit:
Though it would only be setting the obj_player to the same sprite over and over. You'll need a variable that counts up every time the alarm is triggered, and if statements that check the variable for different actions.
 
A

Aura

Guest
Welcome to the GMC~♫

Why are you setting the alarm to -2? The alarm event triggers at 0 and is considered inactive at -1.

Apart from that, you can do that with the help of a single alarm if the interval between the sprite change is the same:

Create:
Code:
alarm[0] = room_speed * 10; //Multiplying it by 10 gives us time equal to 10 seconds
Alarm 0:
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree2;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
}
alarm[0] = room_speed * 10;
You could use a switch statement, but stick to the if statement for now.

If the interval between sprite change is supposed to be different then instead of resetting the alarm at the end of the Alarm 0 event, do it seperately for each sprite:

Alarm 0:
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 10;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 20;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
   alarm[0] = room_speed * 30;
}
 
P

Power_Michael

Guest
Your problem is in the alarm event. When an alarm event triggers, the variable is actually set to 0, not -1. Just take out that if statement in the alarm code and you should be good.

edit:
Though it would only be setting the obj_player to the same sprite over and over. You'll need a variable that counts up every time the alarm is triggered, and if statements that check the variable for different actions.
Yes I thought about the object changing the sprite to the same sprite over and over....alright I will take into account!
 
P

Power_Michael

Guest
Welcome to the GMC~♫

Why are you setting the alarm to -2? The alarm event triggers at 0 and is considered inactive at -1.

Apart from that, you can do that with the help of a single alarm if the interval between the sprite change is the same:

Create:
Code:
alarm[0] = room_speed * 10; //Multiplying it by 10 gives us time equal to 10 seconds
Alarm 0:
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree2;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
}
alarm[0] = room_speed * 10;
You could use a switch statement, but stick to the if statement for now.

If the interval between sprite change is supposed to be different then instead of resetting the alarm at the end of the Alarm 0 event, do it seperately for each sprite:

Alarm 0:
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 10;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 20;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
   alarm[0] = room_speed * 30;
}
I see! I will give it a try and let you know what happens...thank you so much! And by the way....the -2 stuff was just a mindless attempt towards a solution and should not be there...
 
P

Power_Michael

Guest
Welcome to the GMC~♫

Why are you setting the alarm to -2? The alarm event triggers at 0 and is considered inactive at -1.

Apart from that, you can do that with the help of a single alarm if the interval between the sprite change is the same:

Create:
Code:
alarm[0] = room_speed * 10; //Multiplying it by 10 gives us time equal to 10 seconds
Alarm 0:
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree2;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
}
alarm[0] = room_speed * 10;
You could use a switch statement, but stick to the if statement for now.

If the interval between sprite change is supposed to be different then instead of resetting the alarm at the end of the Alarm 0 event, do it seperately for each sprite:

Alarm 0:
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 10;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 20;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
   alarm[0] = room_speed * 30;
}

So I did everything you told me...and it worked perfectly....almost, but it was what I needed. So thank you so much! If anyone else wants to do something like this as well...what the code should look like..refined

Create event:
Code:
alarm[0] = room_speed * 10; //Multiplying it by 10 gives us time equal to 10 seconds

Nothing needed in the step event



Alarm[0]
Code:
if (sprite_index == spr_tree1) {
   sprite_index = spr_tree2;
   alarm[0] = room_speed * 10;
}
else if (sprite_index == spr_tree2) {
   sprite_index = spr_tree3;
   alarm[0] = room_speed * 10;
}
else if (sprite_index == spr_tree3) {
   sprite_index = spr_tree4;
   alarm[0] = room_speed * 10;
}

In this I have left the (room_speed * 10) being multiplied by 10....at this rate...the first sprite will change into the second sprite after 31 seconds. Then, 10 seconds later, the second will change into the third at 41 seconds. Then 10 seconds later...it will change into the large sprite...and it will stay that way. So 31, 10, 10....increasing the (room_speed * x) x value will increase the time it takes to change...hope this helps someone else...because it certainly helped me! Thank you again!
 
You can do it without if statements if you use an array:

Create Event
Code:
spr[0] = spr_tree1;
spr[1] = spr_tree2;
spr[2] = spr_tree3;
growth_stage = 0;
sprite_index = spr[growth_stage];
alarm[0] = room_speed * 10;
Alarm[0] Event
Code:
sprite_index = spr[++growth_stage]; //++growth stage increments the variable growth_stage by 1 then returns it for the array to use
if (growth_stage < 2) {
   alarm[0] = room_speed * 10;
}
 
Top