0
votes

So here's the gist of what I'm trying to do here.

I have an array of foreground sprites that I scroll forever as the player moves along. What I would like to do, is when the player starts passing a certain point on the Y axis, scale down those foreground sprites while still moving them.

I'd like to be able to scale the sprites from their bottom left hand corners when the player is going up, and I've got this working without any problems.

The real problem is that I'd also like to scale the sprites from their bottom right hand corners when the player is coming down. Now I thought that I could do this by setting each sprite's anchor point to 1,0 before scaling it, but that doesn't seem to work. The sprites still scale from their bottom left hand corners.

What am I missing here?

// do logic to identify the scale factor we want

for (CCSprite *sprite in foreground_sprites)
{
    CGPoint old_anchor = sprite.anchorPoint;
    [sprite setAnchorPoint:ccp(1,0)];
    [sprite setScale:scale_factor];
    [sprite setAnchorPoint:old_anchor];
}
2
your question says bottom right, and description says bottom left. If bottom left then anchor point (0,0) and for right (0,1) - Guru

2 Answers

0
votes

Have you tried messing with this property?

ignoreAnchorPointForPosition(false);

I'm using cocos2d-x, there should be something similar to that

0
votes

If I understand correctly, you want to scale from the bottom left while the player's Y position increases but scale using the bottom right while they are descending?

Well you can't just change the anchor point alone. The anchor point and position go hand in hand to position the sprite on the screen. So if you positioned the sprite on to the screen using an anchor point of (0,0) then if you want to switch it's anchor point to (1,0) while keeping it in the same location on the screen, you'll need to update the position.

CCSprite* sprite = ...;
sprite.anchorPoint = CGPointZero;
sprite.position = CGPointZero;

...

sprite.anchorPoint = CGPointMake(1.0f, 0.0f);
sprite.position = CGPointMake(sprite.position.x + (sprite.contentSize.width * sprite.scaleX * sprite.anchorPoint.x),
                              sprite.position.y + (sprite.contentSize.height * sprite.scaleY * sprite.anchorPoint.y));

Hopefully I understood your problem correctly and was able to help.