GameMaker Help please my enemy wont move!?!

D

dragon_slayer25

Guest
I have spent the last week staring at my code for my enemy. all i want the enemy to do is move to the side when it collides with the wall it goes backwards. i have tried to look it up but to no luck. someone please help me i am only a beginner at coding.

here is my code for my enemy
//step event
if (place_meeting (x+(1), y, obj_wall))
{
phy_position_x -= (xspeed);
}

else if (place_meeting (x-(1), y, obj_wall))
{
phy_position_x += (xspeed);
}
if (position_empty (x+(1) , y) && start1 == false)
{
phy_position_x +=(xspeed);
start1 = true;
}
else if (position_empty (x-(1) , y) && start1 == true)
{
phy_position_x -= (xspeed);
start1 = false;
}

and here is my code in the create event

xspeed = (3);
start1 = false;


someone please help me with this so I can learn from this
 
I'm a GM:S 1.4 user and haven't used phy_position_x function -if its a function- but heres something;
Code:
//Step Event
x+=hspd;

if (position_meeting(x+1, y, obj_wall) //If there is a obj_wall in x+1 then move left
 {
  hspd = -(xspeed);
 }
if (position_meeting(x-1, y, obj_wall) //If there is a obj_wall in x-1 then move right
 {
  hspd = (xspeed);
 }
EDIT: you don't need to do it like "x-(1)" You can just type "x-1" like it is above.
 
D

dragon_slayer25

Guest
I'm a GM:S 1.4 user and haven't used phy_position_x function -if its a function- but heres something;
Code:
//Step Event
x+=hspd;

if (position_meeting(x+1, y, obj_wall) //If there is a obj_wall in x+1 then move left
 {
  hspd = -(xspeed);
 }
if (position_meeting(x-1, y, obj_wall) //If there is a obj_wall in x-1 then move right
 {
  hspd = (xspeed);
 }
EDIT: your don't need to do it like "x-(1)" You can just type "x-1" like it is above.
I put you code in but the x+=hspd does not let the game run i don't know what to do to fix this
 
W

Wraithious

Guest
I'm a GM:S 1.4 user and haven't used phy_position_x function -if its a function-
Thats actually a valid function, he's using physics, thats how you move around in The physics world. (Or 1 way to do it anyway)

As to the problem you may need to use
Code:
If(place_meeting(x-1,y,obj_wall))
{
phy_position_x = x + xspeed;
}else
If(place_meeting(x+1+sprite_width,y,obj_wall))
{
phy_position_x = x - xspeed;
}
Instead of +=, also note if colliding with the right wall you'd need to check for x plus your sprite width, assuming your sprite's x origin is at 0, if it's not adjust accordingly
 
Top