I have a CCNode class that looks like this:
#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
This should have an animated character in the middle of the screen and it does on an empty project. But when I include this on the actual project the animation stays paused on the frame I define as "spriteCover". No errors at all, just not playing the animation.
I have tried using -(id)init instead of -(void)didLoadFromCCB but is the same. I have traced several steps (you can see log section on the code above) and everything looks fine to me. Here are the printed lines:
---> <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>
I added two buttons that trigger actionA and actionB and when tapping twice on the actionB I get an error (as expected) because reason: 'runAction: Action already running'
I have also tried this, this, this here and several other sources but haven't yet solved the problem.
Just to document my solution, the way I solved the problem was getting the UIViewController preceding the CCScene with the animation. I noticed that when loading after a CCScene everything worked perfectly but when loading after a UIViewController the animation didn't start.