我有一个CCNode类,如下所示:
#import "CCAnimation.h"
#import "Gameplay.h"
#import "ZAFSingletonCenter.h"
@interface Gameplay ()
{
}
@property (nonatomic, strong) CCSprite *character;
@property (nonatomic, strong) CCAction *talkAction;
@end
@implementation Gameplay{
NSMutableString *gesture;
NSString *characterName;
NSString *plist;
NSString *gestureSprite;
NSString *framefilename;
}
- (void)regresar {
CCScene *mainScene = [CCBReader loadAsScene:@"MainScene"];
[[CCDirector sharedDirector] replaceScene:mainScene];
}
- (void)didLoadFromCCB {
ZAFSingletonCenter *temporal = [ZAFSingletonCenter sharedManager];
characterName = temporal.personajeActual;
gesture = [@"fotos" mutableCopy];
plist = [NSString stringWithFormat:@"%@/%@.plist",characterName,gesture];
gestureSprite = [NSString stringWithFormat:@"%@/%@.png",characterName,gesture];
framefilename = [NSString stringWithFormat:@"%@/%@/%@_%@_",characterName,gesture,characterName,gesture];
[self loadGesture:characterName withGesture:gesture];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:gestureSprite];
[self addChild:spriteSheet];
// *** This block should be moved outside the init or didLoadFromCCB
// *** and to the moveCharacter:withGesture method... once I make it work
NSMutableArray *actionFrames = [NSMutableArray array];
for (int i=1001; i<=1020; i++) {
[actionFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"%@%d.png",framefilename,i]]];
//NSLog(@"%@%d.png",_framefilename,i);
}
CCAnimation *gestureAction = [CCAnimation animationWithSpriteFrames:actionFrames delay:0.0666f];
CGSize viewSize = [[CCDirector sharedDirector] viewSize];
NSString *spriteCover = [NSString stringWithFormat:@"%@/%@/%@_%@_1011.png",characterName,gesture,characterName,gesture];
self.character = [CCSprite spriteWithImageNamed:spriteCover];
self.character.position = ccp(viewSize.width/2, viewSize.height/2);
[spriteSheet addChild:self.character z:99];
self.talkAction = [CCActionRepeatForever actionWithAction:
[CCActionAnimate actionWithAnimation:gestureAction]];
[self.character runAction:self.talkAction];
NSString *logMessage = self.character.debugDescription;
NSLog(@"---> %@",logMessage);
NSLog(@"---> %d",self.character.isRunningInActiveScene);
NSString *gestureMessage = gestureAction.debugDescription;
NSLog(@"---> %@",gestureMessage);
NSString *actionMessage = self.talkAction.debugDescription;
NSLog(@"---> %@",actionMessage);
// *** ending the block to be taken to the moveCharacter:withGesture method
}
- (void)actionA {
//testingButton A action
NSLog(@"A");
[self.character stopAction:self.talkAction];
}
- (void)actionB {
//testingButton B action
NSLog(@"B");
[self.character runAction:self.talkAction];
}
@end
这应该在屏幕中间有一个动画角色,它在一个空项目上。但是当我在实际项目中包含它时,动画在我定义为“spriteCover”的帧上暂停。没有错误,只是没有播放动画。
我尝试使用 - (id)init而不是 - (void)didLoadFromCCB但是相同。我已经跟踪了几个步骤(你可以看到上面代码的日志部分),一切看起来都很好。以下是印刷线:
---> <CCSprite = 0x15693a50 |
Rect = (281.50,568.50,147.00,268.00) |
tag = (null) | atlasIndex = 0>
---> 0
---> <CCAnimation = 0x15692de0 |
frames=20, totalDelayUnits=20.000000,
delayPerUnit=0.066600, loops=1>
---> <CCActionRepeatForever = 0x15694470 | Tag = -1>
我添加了两个触发actionA和actionB的按钮,当在actionB上点击两次时,我收到错误(正如预期的那样),因为reason: 'runAction: Action already running'
答案 0 :(得分:1)
我注意到您要针对多个用例回收CCAction
talkAction。
每次在某事物上调用CCAction
时,您都必须创建单独的runAction
个实例。
解决此问题的一种简单方法是覆盖talkAction
的getter以始终返回新的CCAction
。
答案 1 :(得分:0)
为了记录我的解决方案,我解决问题的方法是使用动画在CCScene之前获取UIViewController。我注意到在CCScene之后加载时一切都很完美但是在UIViewController之后加载时动画没有启动。