1

我开始尝试使用 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
4

2 回答 2

2

这与您的问题不是 100% 相关.. 但您的属性还有另一个问题。

您将您的属性定义为保留,并在 dealloc 函数中释放它,但实际上您从未保留该对象。

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

在这个位置,_heroSprite 变量包含启用了自动释放的精灵......你不要保留它。

当然你不要松开它,因为它会被这条线保留:

[heroSpriteSheet addChild:_heroSprite];

但是当孩子从工作表中移除时,它会被释放。

所以这在 dealloc: 中是不必要的:self.heroSprite = nil;甚至[_heroSprite release];会使你的代码崩溃。

如前所述,代码可以工作,但是当您稍后查看它时,您可能会感到困惑。

您应该将探针声明为 (nonatomic, assign) 或正确保留它

self.herosprite = [CCSprite spriteWithSpriteFrameName:@"heroFrame1.png"];
于 2011-04-29T20:53:14.660 回答
1

尝试从以下位置更改您在DebugZoneLayer课堂上的属性:

@property(nonatomic, retain) CCSprite *hero;

至:

@property(nonatomic, retain) HeroClass *hero;
于 2011-04-29T18:57:35.053 回答