2013-06-20 50 views
0

我正在嘗試創建一個動畫sprite類,該類有啓動和停止動畫的方法。當我將CCSprite變量從本地移動到伊娃時,該類將引發異常。通過將CCSprite變量作爲本地變量,類工作,即精靈出現並被動畫化。CCSprite引發的異常spriteWithSpriteFrameName

該項目啓用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() 

回答

-1

爲什麼你想創建一個類,做精靈動畫?已經有很多事情可以爲你工作。看看sprite and level helper.他們自動爲你做動畫。沒有必要重塑一些已經爲你準備的東西:)

+0

你給了他一個鏈接尋求幫助,這真是太好了。然而,不回答這個問題有點粗魯。提供替代信息很酷,但他/她發佈該問題的原因是爲了答案。 – AlienDev