0

私はばかではありませんが、ヘッダー ファイルを見て、ときどきバカのように感じることがあります。おそらく非常に複雑なセットアップで、解決できないエラーがあります。ここでは、私ができる限り詳細に単純化しています....

  • Model クラスを含む Controller クラスがあります。
  • アクションをキャプチャしてコントローラーと通信する Scene クラスがあります。
  • Model クラスと通信して Model の状態を出力する Layer クラスがあります。
  • Scene クラスには、出力専用の Layer クラスが含まれています。
  • シーンは、Cocos2D フレームワークによって決定されるレイヤーを保持する必要があります。
  • 特定の Scene クラスは、Controller クラスへの参照を保持する RootScene クラスから派生します。
  • 特定の Layer クラスは、Model クラスへの参照を保持する RootLayer クラスから派生します。
  • コントローラーはシーンの作成を担当し、シーンはレイヤーの作成を担当します。

問題は、レイヤーを作成し、コントローラーのモデルをレイヤーのモデルに渡すときに発生します( AScene.m)。「構造体または共用体ではないメンバー「モデル」のリクエスト」を取得します。キャストが機能せず、これらのクラスが互いに会話できるようにする方法がわかりません。問題の一部は、 Controller クラスに Model クラスが含まれていることだと思います。

Controller.h

#import <Foundation/Foundation.h>

@class Model;
@class AScene;

@interface Controller : NSObject {
    Model *Model;
}
@property (nonatomic, retain) Model *Model;
-(void)runScene;

Controller.m

#import "Controller.h"

#import "Model.h"
#import "AScene.h"

@implementation Controller

@synthesize Model;

- (void)runScene {
    AScene *newScene = [[AScene alloc] init];
    newScene.controller = self;
}

RootScene.h

#import "cocos2d.h"

@class Controller;

@interface RootScene : Scene {
    Controller *controller;
}
@property (nonatomic, retain) Controller *controller;

@end

RootScene.m

#import "RootScene.h"
#import "Controller.h"

@implementation RootScene

@synthesize controller;

- (id) init {
    self = [super init];
    if (self != nil) {
        //
    }
    return self;
}

- (void) dealloc {
    [controller release];
    [super dealloc];
}

@end

AScene.h

#import "RootScene.h"

@class ALayer;
@class Model;

@interface AScene : RootScene {
}

@end

AScene.m

#import "AScene.h"
#import "ALayer.h"
#import "Model.h"

@implementation AScene

- (id) init {
    self = [super init];
    if (self != nil) {
        ALayer *newLayer = [ALayer node];
        newLayer.model = controller.Model; // <-- Request for member 'Model' in something not a stucture or union
        [self addChild:statusScreenLayer];
    }
    return self;
}

- (void) dealloc {
    [super dealloc];
}

@end

RootLayer.h

#import "cocos2d.h"

@class Model;

@interface RootLayer : Layer {
    Model *model;
}
@property (nonatomic, retain) Model *model;

@end

RootLayer.m

#import "RootLayer.h"

#import "Model.h"

@implementation RootLayer

@synthesize model;

- (id) init {
    self = [super init];
    if (self != nil) {
        //
    }
    return self;
}

- (void) dealloc {
    [model release];
    [super dealloc];
}

@end

ALayer.h

#import "RootLayer.h"

@interface ALayer : RootLayer {
}
-(void) draw;

@end

ALayer.m

#import "ALayer.h"

@implementation ALayer

-(void) draw {
    // draw based on state of the model
}

@end
4

2 に答える 2

3

の実装はのヘッダーではありASceneません。#importController

編集:明示的な解決策。

AScene.m に次を追加します。

#import "Controller.h"
于 2009-10-24T21:54:02.923 に答える
1

ニコライは正しいようです。さらに、ベスト プラクティスでは、プロパティ (ivar など) を大文字で始めてはいけません。あなたは傷を求めているだけです。代わりにこれらを使用してください:

Controller.h

@interface Controller : NSObject {
    Model *model;
}
@property (nonatomic, retain) Model *model;

Controller.m

@synthesize model;
于 2009-10-26T19:50:43.053 に答える