2011-04-29 45 views
1

我開始嘗試用Tiled進行Cocos2D,並將玩家精靈和動作編碼在CCLayer內以及其他所有內容中。在繼續之前,我想把玩家分成一個CCLayer,我希望這是正確的。Cocos2D:子類化精靈和動畫作爲CCLayer給我帶來麻煩

我的頭和主代碼如下:

HeroClass.h

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface HeroClass : CCLayer { 
    CCSprite *_hero; 
    CCAction *_heroSpriteFlyAction; 

} 

@property(nonatomic, retain) CCSprite *hero; 
@property(nonatomic, retain) CCAction *heroSpriteFlyAction; 

@end 

HeroClass.m

#import "HeroClass.h" 

@implementation HeroClass 

@synthesize hero =_hero; 
@synthesize heroSpriteFlyAction = _heroSpriteFlyAction; 

-(id) init{ 
    self = [super init]; 
    if (!self) { 
     return nil; 
    } 

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"heroTestSheet.plist"]; 

    CCSpriteBatchNode *heroSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"heroTestSheet.png"]; 

    [self addChild:heroSpriteSheet]; 

    NSMutableArray *heroSpriteFlyAnimFrames = [NSMutableArray array]; 
    for(int i = 1; i <= 2; ++i) { 
     [heroSpriteFlyAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"heroFrame%d.png", i]]]; 
    } 

    CCAnimation *heroSpriteFlyAnim = [CCAnimation animationWithFrames:heroSpriteFlyAnimFrames delay:0.03f]; 

    self = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"]; 

    _heroSpriteFlyAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:heroSpriteFlyAnim restoreOriginalFrame:NO]]; 
    [self runAction:_heroSpriteFlyAction]; 

    [heroSpriteSheet addChild:self]; 


    return self; 
} 

- (void) dealloc{ 
    self.hero = nil; 
    self.heroSpriteFlyAction = nil; 
    [super dealloc]; 
} 

@end 

我想我要實現的想法是,我可以訪問這個類中的東西作爲其他文件中的屬性。上面的代碼給出沒有錯誤,但我可能沒有正確地做。我在遷移過程中遇到的問題是我的CCLayer類DebugZoneLayer中發生了什麼,它創建了地圖,並且應該添加我的播放器精靈,但卻給了我錯誤。

在DebugZoneLayer.h中,我導入了HeroClass.h,並從英雄精靈的HeroClass製作了一個指針,並給它一個屬性。這裏沒有錯誤,但它可能是我會錯在何處開始:

#import "cocos2d.h" 
#import "HeroClass.h" 
@class HeroClass; 

// DebugZone Layer 
@interface DebugZoneLayer : CCLayer { 

    HeroControl *heroControl; 

    HeroClass *hero;  

    CCTMXTiledMap *theMap; 
    CCTMXLayer *blocksCollidable; 
    CCTMXLayer *invisiblePropertiesLayer; 
} 


@property(nonatomic, retain) CCSprite *hero; 

在DebugZoneLayer.m,當我合成的英雄,它給出的特性‘英雄’錯誤「類型不匹配的類型伊娃「英雄」

@synthesize hero; 

文件的其餘部分給我相關的任何引用英雄更多的錯誤,但至少這是它開始的地方。

編輯(更新)

只是想提一提,因爲這解決了我清除了在HeroClass.m一些重大問題這是導致飛機墜毀:

#import "HeroClass.h" 

@implementation HeroClass 

@synthesize heroSprite =_heroSprite; 
@synthesize heroSpriteSheet =_heroSpriteSheet; 
@synthesize heroSpriteFlyAction = _heroSpriteFlyAction; 

-(id) init{ 
    self = [super init]; 
    if (!self) { 
     return nil; 
    } 

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"heroTestSheet.plist"]; 

    _heroSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"heroTestSheet.png"]; 

    //[self addChild:_heroSpriteSheet]; 

    NSMutableArray *heroSpriteFlyAnimFrames = [NSMutableArray array]; 
    for(int i = 1; i <= 2; ++i) { 
     [heroSpriteFlyAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"heroFrame%d.png", i]]]; 
    } 

    CCAnimation *heroSpriteFlyAnim = [CCAnimation animationWithFrames:heroSpriteFlyAnimFrames delay:0.03f]; 

    _heroSprite = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"]; 

    _heroSpriteFlyAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:heroSpriteFlyAnim restoreOriginalFrame:NO]]; 
    [self runAction:_heroSpriteFlyAction]; 

    [_heroSpriteSheet addChild:_heroSprite]; 


    return self; 
} 

- (void) dealloc{ 
    self.heroSprite = nil; 
    self.heroSpriteSheet = nil; 
    self.heroSpriteFlyAction = nil; 
    [super dealloc]; 
} 

@end 

回答

1

試圖更改在DebugZoneLayer類你的財產:

@property(nonatomic, retain) CCSprite *hero; 

要:

@property(nonatomic, retain) HeroClass *hero; 
+0

哈哈妥善保留它!謝謝。這是一個巨大的進步與Cocos2D前進 – VagueExplanation 2011-04-29 19:50:59

2

這是不相關的問題100%..但你有你的財產另一個問題。

您將您的屬性定義爲保留,並將其釋放到dealloc函數中,但實際上您永遠不會保留該對象。

_heroSprite = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"]; 

在這個位置_heroSprite變量包含啓用autorelease的精靈......你不保留它。

當然你不要鬆開,因爲它會通過此行得到保留:

[heroSpriteSheet addChild:_heroSprite]; 

,但是當孩子從紙張清除就會得到釋放。

所以這是不必要的dealloc:self.heroSprite = nil;[_heroSprite release];甚至會崩潰你的代碼。

如前所述,代碼有效,但稍後當您查看它時,您可能會感到困惑。

你應該聲明proberty爲(非原子,分配)或

self.herosprite = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"]; 
+0

我在我的問題上面進行了更新。這仍然適用於相同的方式嗎? – VagueExplanation 2011-04-29 21:14:19

+0

很確定我可以在HeroClass.m中註釋dealloc中的所有內容 – VagueExplanation 2011-04-29 21:20:35