2

Here is my function, it makes random point on the ground and makes my sprite go there. If the x position of location is less then position of my sprite, I flip my sprite so it looked left (it looks right by default). But this is not working at all. I tried different flip methods but none work! Please help what is wrong?

- (void) wander
{
    CGPoint rand = CGPointMake(CCRANDOM_0_1() * 270 , CCRANDOM_0_1() * 140 );

    float distance = ccpDistance([currentSprite position], rand);
    float speed = distance / 2;
    ccTime time = distance / speed;
    CCAction *move = [CCMoveTo actionWithDuration:time position:rand];

    if (currentSprite.position.x > rand.x)
    {

       currentSprite.flipX = YES;
    }
    else {
        currentSprite.flipX = NO;
    }

    [self runAction:move];
}

There is no problem with this code. Problem was that I was trying to flip sprite that was a child of my Pet object. When I tried flipping sprite everything worked just fine. If anybody could explain that, would be great.

Dvole
  • 1,865
  • 2
  • 19
  • 23

1 Answers1

2

You can also try flipping it by using the scale:

currentSprite.scaleX *= -1.f;

You should also ensure that currentSprite is the sprite that contains the texture you want to flip, and is not a parent of the sprite that contains the texture you want to flip. See the documentation about flipX for more info.

House
  • 73,224
  • 17
  • 184
  • 273
  • I think the problem is in another method, can you please check out this question http://gamedev.stackexchange.com/questions/34023/what-is-the-correct-way-of-changing-image-of-existing-ccsprite – Dvole Aug 10 '12 at 17:19
  • Have you tried the scale method? – House Aug 10 '12 at 17:23
  • Yes, didn't help. – Dvole Aug 10 '12 at 17:24
  • Actually I have found the problem. I have composite class, Pet that contains sprite as property. I tried flipping that sprite and that didnt work. When I tried to flip pet it work. Can you please explain that behavior? Thank you for quick responce anyway. – Dvole Aug 10 '12 at 17:25
  • I cannot explain that. If I'm reading correctly, it seems contrary to the documentation. Glad you were able to solve it. – House Aug 10 '12 at 17:32