calling a method when a particular CCAnimationFram

2019-08-09 08:51发布

I would like to call a method that rotates a sprite when a particular frame in displayed. I got a few ideas from this post but I am having issues as to how I should implement it. How would I define my dictionary for accessing 3rd frame and link it to the selector? Below is what I've done. Please note that it's work-in-progress.

-(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条回答
登录 后发表回答