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

GML I need help about code

W

WhyNotUsePaper

Guest
When i launch game enemy starts moving to right side then bounce when he hits left side he teleports to the right side then starts moving left and he do that forever.

Here is code


Code:
///scr_enemy_move()
hspd = dir * spd;
switch(dir)
{
    case 1:
        if(place_meeting(x+hspd,y,obj_wall))
        {
            while(!place_meeting(x+sign(hspd),y,obj_wall))
            {
                x+=hspd;
            }
            if(dir == 1)
            {
                dir = -1;
            }
        }
    break;
    
    case -1:
        if(place_meeting(x+hspd,y,obj_wall))
        {
            while(!place_meeting(x+sign(hspd),y,obj_wall))
            {
                x-=hspd;
            }
            if(dir == -1)
            {
                dir = 1;
            }
        }
    break;
}
x+=hspd;
 

StormGamez

Member
dont know why your using all that code to make the enemy bounce... just simplifying the code and dropping what you dont need should fix it.. i ended up taking you code and took out what wasnt needed and ended up with this...
Code:
hspd = dir * spd;

if(place_meeting(x+hspd,y,obj_wall)) {
    dir = -dir;
} 
x+=hspd;
i hope this reply helps

*PS
i belive you were getting that glitch because your adding to you enemys x co-ordinant in the switch statement and also outside of it using two seprate signs causing it to move in the opositye derecion after exiting it
 
Top