使用CCAnimationFrameDisplayedNotification显示特定CCAnimationFrame时调用方法

时间:2013-07-12 09:24:27

标签: animation cocos2d-iphone nsdictionary nsnotificationcenter

我想调用一个在显示特定帧时旋转精灵的方法。我从this post得到了一些想法,但我遇到了如何实现它的问题。如何定义我的字典以访问第3帧并将其链接到选择器?以下是我的所作所为。请注意,它正在进行中。

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {



    CCSpriteBatchNode* batchNode;
    CCSpriteFrameCache* frameCache;

    frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:@"cat-hd.plist"];
    batchNode = [CCSpriteBatchNode batchNodeWithFile:@"cat-hd.pvr.ccz"];

    [self addChild:batchNode];

    CCSprite* wallbg = [CCSprite spriteWithSpriteFrameName:@"firstBg.png"];
    wallbg.position= ccp(240.0, 160.0);

    //wallbg.anchorPoint = ccp(0.5, 0.5);
    [batchNode addChild:wallbg];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateAnimationFrame:) name:CCAnimationFrameDisplayedNotification object:nil];

    NSMutableArray* catAnimationArray = [[NSMutableArray alloc]init];

    for(int i = 1; i < 7; i++) // i< number of frames in the plist File Name
    {
        CCLOG(@"item %d added", i);
        [catAnimationArray addObject:
         [frameCache spriteFrameByName:
          [NSString stringWithFormat:@"blackCat%d.png", i]]];    }

    CCSprite* catSprite = [CCSprite spriteWithSpriteFrameName: @"blackCat1.png"];

    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    catSprite.position = ccp(screenSize.width/2, screenSize.height/2);

    CCAnimation *animation = [CCAnimation animationWithSpriteFrames:catAnimationArray delay:0.3];

    CCAnimationFrame* thirdFrame = [animation.frames objectAtIndex:2];
    NSDictionary* uInfo =       //how should I define my dictionary?


    id action =[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation :animation]];


    [catSprite runAction:action];

    [batchNode addChild:catSprite];
  }

return self;
}

// on "dealloc" you need to release all your retained objects

-(void)rotateAnimationFrame:(NSNotification*)notification {

NSDictionary *userInfoDictionary =  [notification userInfo];

if ([[notification name] isEqualToString:@"CCAnimationFrameDisplayedNotification"]) {
    CCSprite* thirdAnim = [CCSprite spriteWithSpriteFrame:@"blackCat3.png"];
    CCRotateTo *rotateRight = [CCRotateBy actionWithDuration:0.2 angle:40.0];
    [thirdAnim runAction:rotateRight];


}

0 个答案:

没有答案