Question

So in my game I am trying to make the character be able to go left or right depending on the button the player pushes.

I want to flip the sprite so that it faces the right way in whatever direction it is going.

I have a bool as a property of the player class called isGoingLeft and thats how I check when the player is facing left.

The way I flip the player is like this

//IN THE METHOD FOR THE RIGHT BUTTON
if (player.isGoingLeft) { player.flipX = 180; NSLog(@"Flip Right"); player.isGoingLeft = FALSE; }

//IN THE METHOD FOR THE LEFT BUTTON

if (!player.isGoingLeft) {
           player.flipX = 180;
            player.isGoingLeft = TRUE;
            NSLog(@"Flip Left");
        }

This works when I start off going right, and then turn left (the sprite flips correctly). But when I try to go right again, the player moves right, it just doesn’t flip again. The NSLog saying that it flipped back to facing right runs and the bool is changed. I have no idea what is going on

Was it helpful?

Solution

flipX is a BOOL type, so any value greater than 0 will make the sprite flipped.

Use this instead:

player.flipX = YES;
player.flipX = NO;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top