Powerset
Member
Good evening everyone,
For some strange reason my enemy wont work as programmed. I want the enemy to switch from moving to attack state one the player comes into distance. But some strange reason the code is totally broken. I've been looking at some GML/GMS2 tutorials for it all day but nothing seems to get it working. The only thing I can get to work is the enemy moving back and forth which is not what I wanted, as I wanted to add a attack. So basically, Player comes into range of Enemy > Enemy does Attack > Enemy continues to moving in whatever direction. The code is totally flopped and doesnt even work! Can you guys enlighten me and help share your wisdom and knowledge to solve this issue?
First, I have the enemy object create event.
Next, the enemy step event.
Of course instead of working as intended, as soon as the game loads, the enemy pops up doing a endless attack animation which is so weird, its not even in range of the player. All I want is for the enemy to walk around, player comes into area, enemy attacks and then continue to keep moving or attack again if player is there. I've been stuck all day can someone help me out? Is something wrong with the state machine? or just the way the code is set up? Thanks again its appreciated!
For some strange reason my enemy wont work as programmed. I want the enemy to switch from moving to attack state one the player comes into distance. But some strange reason the code is totally broken. I've been looking at some GML/GMS2 tutorials for it all day but nothing seems to get it working. The only thing I can get to work is the enemy moving back and forth which is not what I wanted, as I wanted to add a attack. So basically, Player comes into range of Enemy > Enemy does Attack > Enemy continues to moving in whatever direction. The code is totally flopped and doesnt even work! Can you guys enlighten me and help share your wisdom and knowledge to solve this issue?
First, I have the enemy object create event.
Code:
enum Estates
{
MOVING,
ATTACK,
}
state = Estates.MOVING;
vsp = 0;
grv = 0.5;
walkspeed = 0.5;
flash = 0;
hsp = walkspeed;
Code:
switch (state)
{
case Estates.MOVING: {
#region Moving
//Movement Calculations
vsp = vsp + grv;
//Horizontal Collision
if (place_meeting(x+hsp,y,oWall))
{
while (!place_meeting(x+sign(hsp),y,oWall))
{
x = x + sign(hsp);
}
hsp = -hsp;
}
x = x + hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,oWall))
{
while (!place_meeting(x,y+sign(vsp),oWall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;
#endregion
}
if(instance_exists(oPlayer)) && (distance_to_object(oPlayer) > 10) {
state = Estates.ATTACK;
break;
}
case Estates.ATTACK: {
#region Attack
if (sprite_index != sTestEnemyAttack)
{
sprite_index = sTestEnemyAttack;
image_index = 0;
}
if (animation_end())
{
state = Estates.MOVING;
}
#endregion
}
break;
}