Legacy GM Enemy jump, wait, repeat

G

Gino Collins

Guest
Hi everyone,

I'm very new to Gamemaker and GML. I've been stuck on this for a few hours now and feel like this should be obviously simple. I am making a 2D platformer and want the enemies to simply jump up and down, wait a few seconds and then repeat. I finally got the jumping part working but can't seem to get a delay in between jumps.

My jump code is:

vsp += grav;


if (place_meeting(x, y+vsp,objSolid))
{
while(!place_meeting(x,y+sign(vsp),objSolid))
{
y += sign(vsp);
}
vsp = -3;
}

y += vsp;

objSolid is just the floor. I've been messing with alarms and timelines and can't seem to get anything top work correctly. The closest I've got is for the enemy to jump and then it gets stuck in midair and doesn't move. So basically no progress.

What am I missing??
 

TheouAegis

Member
if (place_meeting(x, y+vsp,objSolid))
{
alarm[0] = room_speed;
while(!place_meeting(x,y+sign(vsp),objSolid))
{
y += sign(vsp);
}
vsp=0;
}
y += vsp;

Alarm 0 event: vsp=-3;
 

Niels

Member
Basically use a alarm or make a costum alarm that starts counting when the enemy jumps. Then when it fires, it resets the alarm and the enemy jumps again
 
G

Gino Collins

Guest
if (place_meeting(x, y+vsp,objSolid))
{
alarm[0] = room_speed;
while(!place_meeting(x,y+sign(vsp),objSolid))
{
y += sign(vsp);
}
vsp=0;
}
y += vsp;

Alarm 0 event: vsp=-3;
That's pretty much what I thought but I must have something else messed up. I quickly tried it before work . It stops the jumping but doesn't trigger the alarm.
 

Niels

Member
That's because the alarm is reset to room_speed(60) on every step as long as the enemy is touching the ground, that way it never reaches 0 :)

Set the alarm[0] = room_speed; in the create event of the enemy object.
Then in the alarm event:
Vsp - =3;
alarm[0] = room_speed
 

TheouAegis

Member
Oh yeah, he had persistent gravity. I forgot about that first line in his code. I've gotten used to all the people who do the "if not place_meeting(x,y+1,obj_ground) vsp+=grv" method.

He'll need to adjust what he sets alarm[0] to in its alarm event, because that alarm will be ticking down while he's in the air when you do it that way.
 
G

Gino Collins

Guest
Thank you! So whats the difference in the gravity methods? The hardest part of all this is the WHY I'm using the code. There are so many different approaches to the same thing I get mixed up in the whys.
 

TheouAegis

Member
I like persistent gravity, like you had. It's how the real world works anyway. But the one drawback to it is that vsp will never be 0 until the end of the Step event (assuming there was a collision). If you only applied gravity when there wasn't ground below the enemy, then vsp would be 0 until you jumped again. That's what my code would have been suited for.

Persistent gravity has the drawback that you're always increasing vsp even if you're already on the ground, which forces the collision check code to run. But then, only applying gravity when not on the ground still requires a collision check. Now, people count that by setting a variable when colliding with the ground and clearing it when jumping or falling off a cliff, so that speeds things up a tad by replacing the extra collision check with a simple variable check. But it's all really minor differences in code. One's a wee bit faster than the others, but nothing to worry about.
 
Top