我正在尝试创建一个动画精灵类,它具有启动和停止动画的方法。当我将 CCSprite 变量从本地移动到 ivar 时,该类会引发异常。使用 CCSprite 变量作为局部变量,类工作,即精灵出现并被动画化。
该项目启用了 ARC,所有 coocs2d 文件都用 -fno-objc-arc 标记。我使用 TexturePacker 创建底层文件,尽管我认为这与问题无关。
我究竟做错了什么?
cocos2D 版本 2.01.00 Xcode 版本 4.6.2
类的调用方式:
CCSpriteBatchNode *animatedSprite = [[AnimatedSprite alloc] init];
animatedSprite.position = ccp(winSize.width / 2, winSize.height / 2);
[self addChild:animatedSprite];
类头
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface AnimatedSprite : CCSpriteBatchNode
{
}
@end
有效的课程
#import "AnimatedSprite.h"
@interface AnimatedSprite()
{
CCAction *action;
// CCSprite *sprite;
}
@end
@implementation AnimatedSprite
- (id)init
{
if (self = [super init])
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spriteSheet.plist"];
self = [CCSpriteBatchNode batchNodeWithFile:@"spriteSheet.png"];
NSMutableArray *animatedFrames = [NSMutableArray array];
for (int i=1; i<=4; i++)
{
[animatedFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"sprite%d.png",i]]];
}
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
// sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animatedFrames delay:0.1f];
action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
[sprite runAction:action];
[self addChild:sprite];
}
return self;
}
@end
抛出异常的类
#import "AnimatedSprite.h"
@interface AnimatedSprite()
{
CCAction *action;
CCSprite *sprite;
}
@end
@implementation AnimatedSprite
- (id)init
{
if (self = [super init])
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spriteSheet.plist"];
self = [CCSpriteBatchNode batchNodeWithFile:@"spriteSheet.png"];
NSMutableArray *animatedFrames = [NSMutableArray array];
for (int i=1; i<=4; i++)
{
[animatedFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"sprite%d.png",i]]];
}
// CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
sprite = [CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animatedFrames delay:0.1f];
action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
[sprite runAction:action];
[self addChild:sprite];
}
return self;
}
@end
堆栈跟踪
#0 0x01efc0ab in objc_release ()
#1 0x01efcbd9 in (anonymous namespace)::AutoreleasePoolPage::pop(void*) ()
#2 0x024ae468 in _CFAutoreleasePoolPop ()
#3 0x0156f8e4 in -[NSAutoreleasePool release] ()
#4 0x00abbc16 in _UIApplicationHandleEvent ()
#5 0x03214df9 in _PurpleEventCallback ()
#6 0x03214ad0 in PurpleEventCallback ()
#7 0x02481bf5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#8 0x02481962 in __CFRunLoopDoSource1 ()
#9 0x024b2bb6 in __CFRunLoopRun ()
#10 0x024b1f44 in CFRunLoopRunSpecific ()
#11 0x024b1e1b in CFRunLoopRunInMode ()
#12 0x00ab717a in -[UIApplication _run] ()
#13 0x00ab8ffc in UIApplicationMain ()
#14 0x000dcc66 in main at /iPhone Apps/Cocos/SpriteTest/SpriteTest/main.m:15
#15 0x000022b5 in start ()