Cocos2d:预加载动画会导致崩溃(Cocos2d: Preloading animation causes a crash)

编程入门 行业动态 更新时间:2024-10-28 12:22:05
Cocos2d:预加载动画会导致崩溃(Cocos2d: Preloading animation causes a crash)

我试图在我的图层的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; } @end

In 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.

更多推荐

本文发布于:2023-08-04 11:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   动画   Cocos2d   crash   animation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!