2011-01-28 93 views
1

我想在我的图层的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]; 
} 

回答

0

问题通过使用非原子对类使用数组和动画创建属性来解决,retain。

0

动画在cocos2d不是设计成为reu SED。你需要每次创建一个新的。

+0

那么你需要每次加载数组?这肯定是一个性能问题? – Chev 2011-01-28 18:08:52

0

你只需要保留动画,但数组可以是本地的。

self.myAnimation = [[CCAnimation animationWithFrames:myAniFramesArray delay:0.1f] retain]; 

请记住,对财产非原子,保留通过CHEV和释放你在适当的dealloc方法保留任何对象表示。