cocos2d动画作为CCMenuItemSprite的子项

时间:2012-03-12 21:54:29

标签: objective-c cocos2d-iphone

我有一个方法:

-(void)myMethod:(id)sender {

    CCMenuItemSprite *anItem = (CCMenuItemSprite *)sender;

    // select animation
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Animation.plist"];

    CCSpriteBatchNode *animationBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"Animation.png"];
    [animationBatchNode addChild:anItem];

    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"Animation_01.png"];

    [sprite setScaleX: anItem.contentSize.width/sprite.contentSize.width];
    [sprite setScaleY: anItem.contentSize.height/sprite.contentSize.height];
    [sprite setPosition:ccp(anItem.position.x, anItem.position.y)];


    NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 1; i <= 7; ++i) {
        [AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Animation_%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:sprite selector:@selector(removeFromParentAndCleanup:)], [CCCallFunc actionWithTarget:anItem selector:@selector(removeFromParentAndCleanup:)], nil];
    [sprite runAction:seq];
    [anItem addChild:sprite];
}

任何人都可以帮助我正确定位动画吗?现在它被放置在屏幕上的其他位置,但我想把它放在它上面......

1 个答案:

答案 0 :(得分:1)

相对于菜单的当前位置和大小设置位置。像

这样的东西
[sprite setPosition:ccp(anItem.boundingBox.size.width/2, anItem.boundingBox.size.height + sprite.boundingBox.size.height/2)];