1
votes

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?

2

2 Answers

2
votes

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);
}
1
votes

I've solved the problem a while ago - just noticed that the question is still unanswered:

We need a makeDoubler function that will copy current frame displayed in one sprite to another. I have implemented it as a member function of a class that inherits from CCSprite, and it looks somewhat like the following.

In header

@interface MySprite : CCSprite
{
//...
}

// makes sprite's image the same 
-(void) makeDoublerTo:(CCSprite *)sprite;

@end

in implementation

@implementation MySprite

-(void) makeDoublerTo:(CCSprite *) sprite
{
    [sprite setDisplayFrame:[self displayedFrame]];
    [sprite setScaleX:[self scaleX]];
    [sprite setScaleY:[self scaleY]];
    [sprite setRotation:[self rotation]];
    [sprite setFlipX:[self flipX]];
    [sprite setFlipY:[self flipY]];
    [sprite setAnchorPoint:ccp(self.anchorPoint.x,self.anchorPoint.y)];
    sprite.position = ccp(self.position.x, self.position.y);
}

@end

You can use it like:

MySprite *sprite = [CCSprite spriteWithFile:@"Icon.png"];
CCSprite *sdoubler = [[CCSprite alloc] init];

and in the game loop (also adjust visibility and coordinates):

[sprite makeDoublerTo:sdoubler];
[sdoubler setVisible:YES];
...

I don't use blending and alpha-ing in this case, but you can easily extend the makeDoubler function.