我开始尝试使用 Tiled 的 Cocos2D,并将玩家精灵和动作与其他所有内容一起编码在 CCLayer 中。在继续之前,我想将播放器子类化为 CCLayer,我希望这是正确的。
我的标题和主要代码如下:
英雄类.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
英雄类.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 中,当我合成 hero 时,它给出了错误“属性类型 'hero' 与 ivar 'hero' 的类型不匹配
@synthesize hero;
文件的其余部分给了我更多与任何引用 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