RickON
RickON

Reputation: 395

Cloning a Sprite Cocos2d

I have a sprite that might be moved if there is no collision. For that I want to pass it along with its possible transition to a "Collision Detection" Method.

So, I am thinking about cloning it to a new object and adding the transition to its boundingbox/rect.

I couldn't get anywhere so far... here's what I have already tried:

1)

    CCSprite *futureSprite = [CCSprite spriteWithTexture:[selSprite texture] rect:[selSprite textureRect]];

    CGPoint futurePos = ccpAdd(futureSprite.position, translation);
    futureSprite.position = futurePos;

2)
    CCSprite *futureSprite;

    futureSprite = selSprite;
    CGPoint futurePos = ccpAdd(futureSprite.position, translation);
    futureSprite.position = futurePos;

Should I follow any other path?

Thanks!

Upvotes: 1

Views: 3089

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

For copying the point of the original sprite to the copy:

CCSprite *futureSprite = [CCSprite spriteWithTexture:selSprite.texture
                                                rect:selSprite.textureRect];

// futureSprite has the same position as selSprite
futureSprite.position = selSprite.position;

// OR: use translation as position since futureSprite.position will be 0,0
futureSprite.position = translation;

No ccpAdd needed. Just assign the original sprite's position to the new sprite's position property.

Upvotes: 1

Related Questions