精灵帧在动画中不旋转

时间:2013-07-15 14:43:44

标签: ios animation cocos2d-iphone nsnotificationcenter

我希望在运行时在下面的代码中旋转第三个动画帧。当显示帧以发送消息时,我使用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"];

}

0 个答案:

没有答案