我试图在我的图层的init方法中预加载动画。 然后,如果触摸屏幕,我会调用动画。 一旦我触摸屏幕,应用程序崩溃,没有错误消息,似乎与调用预加载的动画有关。 我想这样做,因为每次触摸屏幕时创建动画似乎都很昂贵 - 这似乎有效。 任何提示非常感谢。
示例代码:
在我的标题中:
@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]; }I am trying to preload an animation in the init method of my layer. I then call the animation if the screen is touched. The app crashes with no error message as soon as I touch the screen and seems it is to do with calling the preloaded animation. I would like to do it this way as it seems expensive to create the animation every time the screen is touched - which does seems to work though. Any tips greatly appreciated.
Sample Code:
In my header:
@interface Test : CCLayer { NSMutableArray *wake; CCSprite* ani; CCAnimate *animate; } @endIn my implementation:
-(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; }Handling the touch:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // run the animation [ani runAction:animate]; }最满意答案
Cocos2d中的动画不能重复使用。 你需要每次都创建一个新的。
Problem solved by creating properties for the array and animation on the class using nonatomic,retain.
更多推荐
发布评论