himmerz
himmerz

Reputation: 1

How can I get the enemies to move?

Hello I am trying to get enemies to move left and right as if they are sliding backwards and forwards I know this can be done with the following code:

slide += slide_incr;                            
if(abs(slide)>30) slide_incr = -slide_incr; 

However this is of no use to me as I need to set a boolean so I can cycle through the frames for when the enemy is going right or going left.

Ive tried the follow code with no luck:

if(abs(eSlide)<=0)
{
      eSlide += eSlide_incr;    
}

if(abs(eSlide)>30) 
{
      eSlide_incr = -eSlide_incr;   

}

Any ideas on how I can implement it? Thanks

Upvotes: 0

Views: 263

Answers (2)

pentaphobe
pentaphobe

Reputation: 347

the first thing that stands out for me is that the contents of the block:

if (abs(eSlid) <= 0) {
    eSlide += eSlide_incr;
}

will never ever run (the absolute value will always be greater than or equal to 0)

as for your boolean facing, that can be achieved with:

bool isSlidingRight = eSlide_incr > 0;

(note: this would still use the left animation set for values of 0)

Upvotes: 1

Jonas Bystr&#246;m
Jonas Bystr&#246;m

Reputation: 26169

You want to hold a hysteresis state for if you're sliding forward or backward. You are also mixing up how to use the abs() function when bounds checking. Try something along the lines of:

eSlide += eSlide_incr;
if (abs(eSlide) >= 30) {
    eSlide_incr = -eSlide_incr;
}

Upvotes: 1

Related Questions