Problem with Alarms

L

Latteid

Guest
Hello, I been trying to make an enemy that follows a pattern, it shoots 3 times, then move a bit, and then back to the beginning. The problem is that I get to the second part of it, but for some weird reason the object doesn't react at all to the alarm that restart everything, the step code is looking like this:
Code:
if global.battle = 1
{   if pace < 4
{
    if(!firing)
    {
    instance_create(x,obj_boss.y-32,obj_bullet);
    firing=true;
    alarm[0]=60;
    pace += 1
    movespeed = 0;
    }
}
}

if pace >= 4

{
    movespeed = 2;
    alarm[1]=30;
}
Battle means that his pattern starts, pace is a variable I made to try to organize it and firing is to make it shoot just once (the point of alarm 0 is to make firing false let him shoot again)
He shoot correctly, and the start to move, but it seems like he doesn't do anything with the alarm, so he keeps moving forever. Right now the only thing that the alarm 1 has is the code for destroy instance, so it makes it even more weird.
I hope someone here can help me with this.
 
S

Shihaisha

Guest
By putting an alarm[1] into the step event you make it rewind back to 30 in each step, so it never goes out. This pattern is prevented for alarm[0] by setting "firing" to true, but for alarm[1] you didn't do anything to prevent it from infinite rewinds. That's why.
 
L

Latteid

Guest
Thanks for the reply, I see the problem, but I don't quite understand what do I need to add to prevent that.
 
S

Shihaisha

Guest
Well, you can fix your code by checking if alarm[1] is not set already, and resetting "pace" back to 0. I think it's better to use alarms for shooting instead of step event. Setting "firing" variable is not required, since 4 shots are counted by "pace" + alarm. Here is a simpler example (I don't know how "movespeed" was working in your code, so I left it the same):

Create event:
Code:
alarm[0]=60;
pace = 0;
movespeed = 0;//or 1, if your enemy begins to move after creation
alarm[0] event:
Code:
if (pace < 4 && global.battle == 1)
{
instance_create(x,obj_boss.y-32,obj_bullet);
alarm[0]=60;
pace += 1;
movespeed = 0;
}
else
{
alarm[0] = 30;
movespeed = 1;
pace = 0;//set back to 0 for next series of shots
}
 
C

CheeseOnToast

Guest
alarms are assigned a value so you can also check how much time they have left by doing
Code:
if alarm[0]<1 {//code here}

//Full example:

if alarm[0] < 1 // check if an alarm is running or not
{
alarm[0]=30 // if its not running enable it. 
}
 
Top