2012-08-10 44 views
0

在我的遊戲中,我爲我的敵人制作了一個CCSprite(cocos2d)的子類。 因爲我想控制動畫,所以從這個子類的每個實例中,我必須將我的主類中的動畫代碼翻譯成這個子類,並將敵人加載到這個子類中。在CCSprite子類中實現動畫的正確方法是什麼?

我發現這相當困難,但......一段時間後,它神奇地開始工作。

不幸的是,在創建和設置這個子類的屬性後,我開始出現奇怪的崩潰。因爲他們在cocosDenshion和其他與我的敵人課程無關的地方,經過我的代碼和網絡的深入研究,我確信它的某種數據損壞,我幾乎完全肯定它的因爲我完全用錯誤的方式完成了他的動畫代碼。

說實話,我甚至無法將我的想法放在這裏,以及這個實際上如何工作:S ...我完全卡住了。任何幫助深表感謝!

所以我的主要問題是:什麼是在CCSprite子類中實現動畫的正確方法? /我在這裏做錯了什麼?

簡化我的代碼在這裏:(它觸發動畫每2秒,以顯示我多麼想 使用它)

#import "cocos2d.h" 

@interface Npc : CCSprite 
{ 
    CCAction *_runAnimation; 
    NSString* name; 
    int strength; 
} 

@property (nonatomic, retain) CCAction *runAnimation; 
@property int strength; 
@property (nonatomic, retain) NSString* name; 

- (Npc*)loadAnimation; 
- (void)animate; 
@end 

#import "Npc.h" 

@implementation Npc 

@synthesize runAnimation = _runAnimation; 
@synthesize name; 
@synthesize strength; 

-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect 
{ 
    if((self=[super initWithTexture:texture rect:rect])) 
    { 
    } 
    return self; 
} 

- (Npc*)loadAnimation 
{ 

    int lastFrame = 11; 
    NSString *creatureFile = @"vis 1"; 

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: 
    [NSString stringWithFormat:@"%@.plist", creatureFile]]; 
    CCSpriteBatchNode* sheet = [CCSpriteBatchNode batchNodeWithFile: 
           [NSString stringWithFormat:@"%@.png", creatureFile]]; 

    self = [Npc spriteWithTexture:sheet.texture]; 
    NSMutableArray* animFrames = [[NSMutableArray alloc] init];  

    for (int x = 0; x < lastFrame; x++) 
    { 
     [animFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"%@ %d.png", creatureFile, x]]];    
    } 

    CCAnimation* anim = [CCAnimation animationWithFrames: animFrames delay: 0.1]; 

    self.runAnimation = [CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]; 

    [self runAction:_runAnimation]; 

    return self; 
} 

- (void)animate 
{ 
    [self runAction:self.runAnimation]; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 

    [name release]; 
} 

@end 

#import "HelloWorldLayer.h" 
#import "Npc.h" 

@implementation HelloWorldLayer 

+(CCScene *) scene 
{ 
    CCScene *scene = [CCScene node]; 
    HelloWorldLayer *layer = [HelloWorldLayer node]; 
    [scene addChild: layer]; 
    return scene; 
} 


-(id) init 
{ 

if((self=[super init])) 
{ 
    timer = 0; 

    creatureTemp = [Npc spriteWithFile:@"Icon.png"]; 
    creature = [creatureTemp loadAnimation]; 
    creature.position = ccp(100,100); 
    [self addChild:creature]; 

    [self schedule:@selector(nextFrame:)]; 

} 
return self; 
} 


- (void)nextFrame:(ccTime)dt 
{ 
    timer += dt; 
    if (timer > 2.) 
    { 
     [creature animate]; 
     timer = 0.; 
    } 
} 

- (void) dealloc 
{ 
    [super dealloc]; 
} 
@end 

- -----------------編輯--------------------

我改變了我的代碼有教程的幫助雷Wenderlich:http://www.raywenderlich.com/3888/how-to-create-a-game-like-tiny-wings-part-1

這是我認爲更接近它應該是什麼。不幸的是,它仍然在我的iPhone(不是模擬器)上在SimpleAudioEngine上崩潰(我正確實施),所以我仍然做錯了什麼。在NPC類的頂部

@synthesize batchNode = _batchNode; 

全國人大類的初始化:

-(id) initNpc 
{ 
    if((self=[super initWithSpriteFrameName:@"vis 1 0.png"])) 
    { 
     _normalAnim = [[CCAnimation alloc] init]; 

     NSMutableArray* animFrames = [[NSMutableArray alloc] init]; 

     int lastFrame = 11; 

     for (int x = 0; x < lastFrame; x++) 
     { 
      [animFrames addObject: 
      [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
       [NSString stringWithFormat:@"vis 1 %d.png", x]]]; 
     } 

     _normalAnim = [CCAnimation animationWithFrames: animFrames delay: 0.1]; 
     self.runAnimation = [CCAnimate actionWithAnimation:_normalAnim restoreOriginalFrame:NO]; 


    } 
    return self; 
} 

和的HelloWorldLayer

-(id) init 
{ 

    if((self=[super init])) 
    { 
     timer = 0; 


     _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"]; 
     [self addChild:_batchNode]; 
     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"]; 

     creature = [[[Npc alloc] initNpc] autorelease]; 
     creature.position = ccp(200,200); 

     [_batchNode addChild:creature]; 

     [self schedule:@selector(nextFrame:)]; 

     [[SimpleAudioEngine sharedEngine] preloadEffect:@"super1.mp3"]; 

    } 
    return self; 
} 

回答

0

於是,我知道了....這裏是代碼:

NPC。H:

#import "cocos2d.h" 

@interface Npc : CCSprite 
{ 
    CCAction *_runAnimation; 

    CCAnimation *_normalAnim; 
    CCAnimate *_normalAnimate; 

} 

@property (nonatomic, retain) CCAction *runAnimation; 


- (void)animate; 
- (id)initNpc; 

@end 

Npc.m

@synthesize runAnimation = _runAnimation; 
@synthesize batchNode = _batchNode; 

-(id) initNpc 
{ 
    if((self=[super initWithSpriteFrameName:@"vis 1 0.png"])) 
    { 
     _normalAnim = [[CCAnimation alloc] init];   
     NSMutableArray* animFrames = [[NSMutableArray alloc] init];   
     int lastFrame = 7;   
     for (int x = 0; x < lastFrame; x++) 
     { 
      [animFrames addObject: 
      [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
       [NSString stringWithFormat:@"vis 1 %d.png", x]]]; 
     }   
     _normalAnim = [CCAnimation animationWithFrames: animFrames delay: 0.1]; 
     self.runAnimation = [CCAnimate actionWithAnimation:_normalAnim restoreOriginalFrame:NO];  
    } 
    return self; 
} 

- (void)animate 
{ 
    [self runAction:_runAnimation]; 
} 

在HelloWorldLayer.m

-(id) init 
{ 

    if((self=[super init])) 
    { 

     _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"]; 
     [self addChild:_batchNode]; 
     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"]; 

     timer = 0; 

     creature = [[[Npc alloc] initNpc] autorelease]; 
     creature.position = ccp(200,200); 

     [_batchNode addChild:creature]; 

     [self schedule:@selector(nextFrame:)]; 

    } 
    return self; 
} 

- (void)nextFrame:(ccTime)dt 
{ 
    timer += dt; 
    if (timer > 2.) 
    { 
     [creature animate]; 
     timer = 0.; 

    } 
} 

以及有關cocosDenshion怪異的崩潰。這也解決了......它原來是SimpleAudioEngine中的一個已知錯誤,只有當我有一個異常斷點處於活動狀態時它纔會拋出異常。解決方法:做我的聲音,如果我需要一個異常斷點,我註釋掉的聲音一類...

- 不得不說,我寧願:

_batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"]; 
    [self addChild:_batchNode]; 
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"]; 

人大內上課,但這對我來說太過先進了。如果有人知道,讓我知道...真的很瞭解,更好地理解oop。 但它不是絕對必要的...

2

你重新分配自己在init in loadAnimation:

self = [Npc spriteWithTexture:sheet.texture]; 

那時我停止閱讀代碼。由於self已經是Npc類的一個實例,所以你不得不問自己爲什麼要使用像loadAnimation這樣的Npc實例方法。

+0

感謝您的回覆!我實際上已經意識到這一點,這是不好的......但我不知道如何以不同的方式做...你可能會指出我在正確的方向嗎? – yurki 2012-08-11 12:59:58

相關問題