(Solved - See Last Answer) - Flipping Horizontal Actor Image Based on X Movement

chad.allenchad.allen Member, PRO Posts: 9
edited September 2014 in Working with GS (Mac)

I have some actors that randomly move around and stop after some time. You can also pick them up and move them manually.
My problem is that I want the image to be facing whichever direction they are facing.
I did make separate left and right images but found the flipping image horizontal to be a better solution, though either way I still have this problem I am about to explain.
First, I set two rules

Rule - when self.motion.linearVelocityX > 0

    • change attribute self.graphics.flip horizontally to true

Rule - when self.motion.linearVelocityX < 0

    • change attribute self.graphics.flip horizontally to false

I also tried replacing self.motion.linearVelocityX with Position X and updating a separate integer attribute to calculate X movement.
My issue is that when the actor stops moving for a second or so it flips back and forth very quickly.
I thought this to be an issue with the flip horizontal attribute changing fast because the linear velocity might be still changing around the 0 area so I set it to look for velocityX > 10 and velocityX < 10 so it had some leeway but that didn't help. I also tried placing the change flip horizontal in timers so they wouldn't fire immediately and this didn't fix it either.
I don't know if anyone else has this problem but if anyone can figure out a solution for me that would be very helpful. Or at least a suggestion where I could look in my code for problems.

Answers

  • HopscotchHopscotch Member, PRO Posts: 2,782

    @chad.allen, you are doing the right thing, even adding a bit of leeway to account for the occasional wiggle is correct.

    I suspect that you have a contradicting rule somewhere, or something in the "otherwise" section of your rules?

  • chad.allenchad.allen Member, PRO Posts: 9

    I don't have anything in the otherwise sections. Though I could just use that to set the flip horizontal I believe. I figured out something new though.
    I set a display text of left and right with the change attribute and found there were times when it was switching back and forth there was no text. So I added another rule for velocity = 0 and found that it is switching from left/right to stopped very quickly for a second. Even when I have the left and right set to start at 5 or -5 velocity it does this till I go up to a higher value.
    All that to say, the actor doesn't necessarily have a velocity of 0 when it looks like it has stopped. I will add some more drag and more specific boundaries for the left/right/ and stopped positions.

  • chad.allenchad.allen Member, PRO Posts: 9

    Just figured out something new as well! I had drag set to 10 because I thought it would slow the actor down and stop it quickly. I was about to go to 500 drag but decided to try 0 again (though I had this flipping rapid issue when I had drag at 0, I must have changed something). With drag at 0 my issue has been resolved. So, for those who need to do this flipping horizontal images.

    Rule - when self.motion.linearVelocityX > 0 - - change attribute self.graphics.flip horizontally to true

    Rule - when self.motion.linearVelocityX < 0 - - change attribute self.graphics.flip horizontally to false

    Rule - when self.motion.linearVelocityX = 0 - - change attribute self.graphics.flip horizontally to self.graphics.flip horizontally

    And change drag to 0 or mess around with it if its not working right.
    That should work fine.

Sign In or Register to comment.