1

私は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
4

2 に答える 2

2

これはあなたの問題に100%関連しているわけではありません..しかしあなたはあなたの財産に別の問題を抱えています。

プロパティをretainとして定義し、dealloc関数で解放しますが、実際にはオブジェクトを保持することはありません。

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

この位置では、_heroSprite変数に自動解放が有効になっているスプライトが含まれています...保持しません。

もちろん、それを失うことはありません。これは、次の行によって保持されるためです。

[heroSpriteSheet addChild:_heroSprite];

ただし、子がシートから削除されると解放されます。

したがって、これはdeallocでは不要です。コードをクラッシュさせることさえありますself.heroSprite = nil;[_heroSprite release];

前に述べたように、コードは機能しますが、後でそれを見ると、混乱する可能性があります。

プローブを(非アトミック、割り当て)として宣言するか、次のコマンドで適切に保持する必要があります

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 に答える