我想在我的图层的init方法中预加载动画。如果屏幕被触摸,我会调用动画。只要我触摸屏幕,该应用就会崩溃,并且不会显示任何错误消息,并且看起来是调用预加载的动画。我想这样做,因为每次触摸屏幕时创建动画似乎都很昂贵 - 这看起来确实有效。任何提示不胜感激。Cocos2d:预加载动画导致崩溃
示例代码:
在我的头:
@interface Test : CCLayer {
NSMutableArray *wake;
CCSprite* ani;
CCAnimate *animate;
}
@end
我在执行:
-(id) init {
if((self=[super init])) {
// enable touches
self.isTouchEnabled = YES;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ani.plist" texture:[[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"ani.png"]]];
ani = [CCSprite spriteWithSpriteFrameName:@"ani1.png"]; //comes from .plist file
ani.anchorPoint=ccp(0,0);
ani.position = ccp(700,65);
[self addChild:ani z:30];
wake = [NSMutableArray array];
for(int i = 1; i <= 4; i++) {
[wake addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"ani%d.png",i]]];
}
animate = [CCAnimate actionWithAnimation:[CCAnimation animationWithFrames:wake delay:1.0f] restoreOriginalFrame:FALSE];
}
return self;
}
处理触摸:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// run the animation
[ani runAction:animate];
}
那么你需要每次加载数组?这肯定是一个性能问题? – Chev 2011-01-28 18:08:52