• 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!

How do i make an event trigger only once ?

X

Xocolade

Guest
Hello ! I am new to Game Maker , and i've been trying to create a mega man style game . I created an enemy , which goes from one side to another . This is the code i used for the enemy to change it's direction when it collides with an object .
( Image_index ==0 means sprite facing the right direction )
( Image_index ==1 means sprite facing the left direction )

/// Changing Direction
// Changing to right
if (place_meeting(x+1,y,obj_movenemy)){
image_index = 1;
}
// Changing to left
if (place_meeting(x-1,y,obj_movenemy)){
image_index = 0;
}

Problem is : since this is running on a step event , it will constantly set the image index to 0 or 1 , meaning the enemy will bug out and keep changing its frame constantly . Is there any way i can set the image index only ONE time ?

Sorry for bad english , i am brazillian .
 

TheouAegis

Member
Well, if you're not moving it outside of the collision, of course it's going to be changing over and over

This is sloppy code, but you could just do

if !place_meeting(x,y,obj_movenemy) {
if place_meeting(x+1,y,obj_movenemy) image_index = 1;
else
if place_meeting(x-1,y,obj_movenemy) image_index = -1;
}
 

Relic

Member
The issue is based on checking x+1 and x-1. Because your enemy is probably moving more than 1 pixel each step, when a collision occurs your enemy will already be a significant amount of pixels into the move object. As you have seen, this won’t stop the code triggering twice, flipping the enemy back and forth.

Instead of only checking one pixel, check against the speed of the enemy.

Theou has done most of the leg work, but I would recommend changing the checks to x+hspeed (or whatever variable is used for horizontal speed)
 
Top