我開始嘗試用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
哈哈妥善保留它!謝謝。這是一個巨大的進步與Cocos2D前進 – VagueExplanation 2011-04-29 19:50:59