2
\$\begingroup\$

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.

\$\endgroup\$
0
2
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ I think the problem is in another method, can you please check out this question gamedev.stackexchange.com/questions/34023/… \$\endgroup\$
    – Dvole
    Aug 10 '12 at 17:19
  • \$\begingroup\$ Have you tried the scale method? \$\endgroup\$
    – House
    Aug 10 '12 at 17:23
  • \$\begingroup\$ Yes, didn't help. \$\endgroup\$
    – Dvole
    Aug 10 '12 at 17:24
  • \$\begingroup\$ 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. \$\endgroup\$
    – Dvole
    Aug 10 '12 at 17:25
  • \$\begingroup\$ I cannot explain that. If I'm reading correctly, it seems contrary to the documentation. Glad you were able to solve it. \$\endgroup\$
    – House
    Aug 10 '12 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .