views:

246

answers:

1

Let's say we have a CCSprite object that has an actions tied to it:

-(void) moveJack
{
    CCSpriteSheet *sheet = (CCSpriteSheet*)[self getChildByTag:kSheet];
    CCSprite *jack = (CCSprite*)[sheet getChildByTag:kJack];
...

    CCSequence *seq = [CCSequence actions: jump1, [jump1 reverse], jump2, nil];
    [jack runAction:seq]; 
}

If the sprite crosses over the screen boundary, I would like to display it at opposite side. So, original sprite is half displayed on the right side (for example), and half on the left side, because it has not fully crossed yet. Obviously (or is it), I need 2 sprites to achieve this. One on the right side (original), and one on the left side (a copy). The problem is - I don't know how to create exact copy of the original sprite, because tied actions have scaling and blending transformations (sprite is a bit distorted due to a transformations).

I would like to have something like:

CCSprite *copy = [[jack copy] autorelease];

so that I can add an exact copy to display it on the correct side (and kill it after transition is over). It should be a bitmap with all transformations applied... Is it possible?

Any ideas?

+1  A: 

Yes, you need two sprites. But you don't want to copy and autorelease it. Instead keep the second sprite in memory all the time. Since they use the same texture, the overhead in memory is minimal. And you should only display the other sprite when its at the screen edge.

Initialize two sprites with the same texture:

sprite = [CCSprite spriteWithFile:@"Icon.png"];
sprite.position = CGPointMake(sprite.position.x, [sprite contentSize].height / 2);
[self addChild:sprite];

spriteDouble = [CCSprite spriteWithTexture:[sprite texture]];
spriteDouble.position = sprite.position;
spriteDouble.visible = NO;
[self addChild:spriteDouble];

doubleAppearsDistance = [sprite contentSize].width / 2;

Then in an update loop check for screen border intersection, enable/disable the other sprite and position it properly:

if (position_.x > screenSize.width)
{
    position_.x -= screenSize.width;
}
else if (position_.x < 0)
{
    position_.x += screenSize.width;
}

spriteDouble.visible = NO;
if (position_.x >= (screenSize.width - doubleAppearsDistance))
{
    spriteDouble.visible = YES;
    spriteDouble.position = CGPointMake(sprite.position.x - screenSize.width, sprite.position.y);
}
else if (position_.x <= doubleAppearsDistance)
{
    spriteDouble.visible = YES;
    spriteDouble.position = CGPointMake(sprite.position.x + screenSize.width, sprite.position.y);
}
GamingHorror
This was not what I was asking - thanks though.The problem is that original sprite has a lot of transformations attached, so I cannot simply copy original image to another position - it is distorted with many transformations. Copy should have exact transformations applied.
iostriz