0

我想在运行时在下面的代码中旋转第三个动画帧。我正在使用NSNotification显示框架以发送消息时执行此操作。动画在第三帧不旋转的情况下播放。我该如何解决这个问题?我想可能有“更简单”的方法来实现这一点,但我想使用 NSNotifications 来做到这一点。

-(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.6];

repeatMovement = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation :animation]];
    [catSprite runAction:repeatMovement];

    //CCAnimationFrame* thirdAnim = [CCSprite spriteWithSpriteFrame:@"catAnim3.png"];
    CCRotateTo *rotateRight = [CCRotateBy actionWithDuration:0.3 angle:40.0];
    rotateSprite = [catSprite runAction:rotateRight];

    [batchNode addChild:catSprite];

    NSDictionary* theInfo = [NSDictionary dictionaryWithObjectsAndKeys:rotateSprite,@"rotateSprite", nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:CCAnimationFrameDisplayedNotification object:self userInfo:theInfo];

    CCAnimationFrame* thirdFrame = [animation.frames objectAtIndex:2];

    thirdFrame.userInfo = theInfo;

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

}

return self;

}



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

    CCAction  *rotateAction = [[notification userInfo] objectForKey:@"rotateSprite"];

}
4

0 回答 0