0
\$\begingroup\$

I have an object going across the screen with an animation using the following code:

CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFrames("ninjastar.plist");
CCSpriteSheet projectileSheet = CCSpriteSheet.spriteSheet("ninjastar.png");
addChild(projectileSheet);
ArrayList<CCSpriteFrame> projectileSprites = new ArrayList<CCSpriteFrame>();

for (int i = 1; i <= 4; i++) {
    projectileSprites.add(CCSpriteFrameCache.spriteFrameByName("ninjastar" + i + ".png"));
}

CCAnimation projectileAnimation = CCAnimation.animation("throw", 0.1f, projectileSprites);
CCSprite projectile = CCSprite.sprite(projectileSprites.get(0));
CCAction projectileAction = CCRepeatForever.action(CCAnimate.action(projectileAnimation, false));
projectile.setPosition(CGPoint.ccp(winSize.width + (projectile.getContentSize().width / 2.0f), actualY));
    actionMove = CCMoveTo.action(actualDuration, CGPoint.ccp(-projectile.getContentSize().width / 2.0f + 320, actualY));
projectileSheet.addChild(projectile);
projectile.setTag(1);
_projectiles.add(projectile);
CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);
projectile.runAction(actions);
projectile.runAction(projectileAction);

I'm using "spriteMoveFinished" to remove the sprite once it is done going across the screen:

public void spriteMoveFinished(Object sender) {
CCSprite sprite = (CCSprite)sender;
_projectiles.remove(sprite);
sprite.stopAllActions();
removeChild(sprite, true);
}

However, when the sprite gets to the end of the screen it just stays stuck there on the last frame. How do I remove it completely?

\$\endgroup\$
1
\$\begingroup\$

The projectile is added to projectileSheet, but being removed from current layer or scene, rather than removing from projectileSheet. The last line of spriteMoveFinished should be

projectileSheet.removeChild(sprite, true);

or better use

sprite.removeFromParentAndCleanup(true);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.