B
BMartin21900
Guest
Hello,
So I am having trouble with the enemies in my game. The way they are supposed to work is if the player hooks on to them, they are dragged towards the player. If they hit a wall then they are set free while being dragged towards the player, they are set free. However, sometimes when the enemy hits a wall, they get stuck and can't move. Can anyone help me... I think it has something to do with collision or possibly the order of my code...
So I am having trouble with the enemies in my game. The way they are supposed to work is if the player hooks on to them, they are dragged towards the player. If they hit a wall then they are set free while being dragged towards the player, they are set free. However, sometimes when the enemy hits a wall, they get stuck and can't move. Can anyone help me... I think it has something to do with collision or possibly the order of my code...
GML:
switch(state){
//Normal on ground
case pEnemyState.normal:{
if(place_meeting(x-1, y, oWall))
hspd = 5;
else if (hspd>0)
hspd = 5;
if(place_meeting(x+1, y, oWall))
hspd = -5;
else if (hspd<0)
hspd = -5;
vspd = vspd + grv;
//ground check
if(place_meeting(x, y+1, oWall)){
on_ground = true;
}
else{
on_ground = false;
}
//Horizontal Collision
if(place_meeting(x + hspd, y, oWall)){
while(!place_meeting(x + sign(hspd), y, oWall)){
x = x + sign(hspd);
}
hspd = 0;
}
x = x + hspd;
//Vertical Collision
if(place_meeting(x, y + vspd, oWall)){
while(!place_meeting(x, y+ sign(vspd), oWall)){
y = y + sign(vspd);
}
vspd = 0;
}
y = y + vspd;
if(enemyHooked){
state = pEnemyState.hooked;
}
}break;
case pEnemyState.hooked:{
sprite_index = sWall;
//Horizontal Collision
if(place_meeting(x+1, y, oWall)) && (oPlayer.x>x){
enemyHooked = false;
pulled = true;
instance_destroy(oHook);
}
else if(place_meeting(x-1, y, oWall)) && (oPlayer.x<x){
enemyHooked = false;
pulled = true;
instance_destroy(oHook);
}
//Vertical Collision
else if(place_meeting(x, y+1, oWall)) && (oPlayer.y>y) {
enemyHooked = false;
pulled = true;
instance_destroy(oHook);
}
else if(place_meeting(x, y-1, oWall)) && (oPlayer.y<y){
enemyHooked = false;
pulled = true;
instance_destroy(oHook);
}
else{
hspd = 10;
vspd = 10;
dir = point_direction(x,y,oPlayer.x,oPlayer.y);
x += lengthdir_x(hspd, dir);
y += lengthdir_y(vspd, dir);
}
if(point_distance(x,y,oPlayer.x,oPlayer.y)<40) || (point_distance(x,y,oPlayer.x,oPlayer.y)>450){
pulled = true;
enemyHooked = false;
instance_destroy(oHook);
}
if(!enemyHooked){
vspd = -10;
if(oPlayer.x<x)
hspd = 2.5;
else
hspd = -2.5;
pulled = false;
state = pEnemyState.normal;
}
}
}