私はTiledを使用してCocos2Dの実験を開始し、プレーヤーのスプライトとアクションを他のすべてと一緒にCCLayer内にコーディングしました。先に進む前に、プレーヤーを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で、ヒーローを合成すると、「プロパティのタイプ「ヒーロー」がivarのタイプ「ヒーロー」と一致しません」というエラーが表示されます。
@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