el.severo
el.severo

Reputation: 2287

remove ccmenuitem with animation cocos2d

I'm trying to remove a CCMenuItem with a custom animation and it crashes...

- (void)removeCCMenuItem:(id)sender {

    CCMenuItemSprite *menuItem = (CCMenuItemSprite *)sender;

    CCSprite *animationSprite = [CCSprite spriteWithSpriteFrameName:@"AnimatedImage_01.png"];

    [fixedSprite setScaleX: menuItem.contentSize.width/animationSprite.contentSize.width];
    [fixedSprite setScaleY: menuItem.contentSize.height/animationSprite.contentSize.height];

    animationSprite.position = ccp(menuItem.contentSize.width/2,menuItem.contentSize.height/2);
    [menuItem addChild:animationSprite];

    NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 1; i <= 5; ++i) {
        [animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"AnimatedImage_%02d.png", i]]];
    }

    CCAnimation *anim = [CCAnimation animationWithFrames:animFrames delay:0.05f];
    CCActionInterval *animAction = [CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO];

    id seq = [CCSequence actions: animAction, [CCCallFunc actionWithTarget:animationSprite selector:@selector(removeFromParentAndCleanup:)], [CCCallFunc actionWithTarget:menuItem selector:@selector(removeFromParentAndCleanup:)], nil];
    [menuItem runAction:seq];
}

Any idea why it crashes?

The log says:

reason: '-[CCMenuItemSprite isFrameDisplayed:]: unrecognized selector sent to instance

Upvotes: 1

Views: 621

Answers (1)

FBryant87
FBryant87

Reputation: 4614

You're running the action on a subclass of CCMenuItem, whereas in this case I think you need to run it on the sprite itself.

Try changing:

[menuItem runAction:seq];

To:

[animationSprite runAction:seq];

Upvotes: 2

Related Questions