stopping at distance in platformer??

heres my monster code, in a perfect world he walks up to him and swings his sword, however he keeps ping ponging back and forth and not swinging.

any help or idea with my code?



if moving=true and hiding=false and see=false{
sprite_index=banditmove
if dir=1{hspeed=-1}
if dir=2{hspeed=1 }
}

//if hiding=true{
//hspeed=0
///sprite_index=mondead
//}

if hiding =true and hurt=false {
hurt=true alarm[0]=60}

if see=true and hiding=false{
hspeed=0
sprite_index=banditstand
}

if distance_to_object(owen)<70 and see=false and stopsee=false and fallow=false{
see=true}
if distance_to_object(owen)>80 and see=false and stopsee=true and fallow=true{
stopsee=false
fallow=false}


if see =true and stopsee=false {
stopsee=true alarm[1]=30}
{
if fallow=true and hiding=false {
if self.x-30 > owen.x{hspeed =-2 dir=1}
if self.x+30 < owen.x{hspeed =2 dir=2}
sprite_index=banditmove}

else

if fallow=true and hiding=false {
if self.x-10 > owen.x{hspeed =0 dir=1}
if self.x+10 < owen.x{hspeed =0 dir=2}
sprite_index=banditattack}



}
 

Slyddar

Member
That code seems...complex. Have you considered using state machines? It would simplify the amount of flag checks you are using and make this process much simpler.
 

Slyddar

Member
im using gm8. i dont like studio since it saves every time you run it.
The version is not relevant. GM8 still supports state machines. Yoyo's official tutorial shows how to use them on GMS2, but it's the same concept - https://www.yoyogames.com/blog/555/coffee-break-tutorials-finite-state-machines-gml

With that kind of knowledge you could set up your objects with various states, which will allow you to control the object much more effeciently. Once you have it working, you will wonder how you did without it. Believe me, we've all been at the stage you are, trying to juggle many flags in one step event, with each flag changing tripping over another. State machines will solve that problem for you.
 
Top