2012-03-12 63 views
0

我有一个方法:cocos2d的动画作为孩子CCMenuItemSprite

-(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]; 
} 

任何人都可以帮助我如何正确positionate动画?现在放在屏幕上的其他地方,但我想把它放在它的上面......

回答

1

使位置相对于菜单的当前位置和大小。类似于

[sprite setPosition:ccp(anItem.boundingBox.size.width/2, anItem.boundingBox.size.height + sprite.boundingBox.size.height/2)];