“尝试在Cocos2d中使用前瞻性'游戏'作为'MathGame'的超类”

时间:2012-08-05 20:20:41

标签: objective-c inheritance import forward-declaration

我正在为iphone制作Cocos2d游戏,我的主游戏模式Game继承自CCLayer

我正在尝试制作另一种继承自MathGame的游戏模式Game,但是当我尝试编译时,我在MathGame.h中收到此错误:

  

尝试使用前瞻性课程'Game'作为'MathGame'的超类

即使MathGame的实现和接口为空,我也会收到错误。只有当我尝试将MathGame.h包含在另一个文件中时才会发生。

以下是Game类的代码:

// Game.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#import "SplashScreenLayer.h"

@interface Game : CCLayer
    // A bunch of stuff
@end

新游戏类型:

// MathGame.h
#import "Game.h"

@interface MathGame : Game
@end

主菜单包括:

// SplashScreen.h
#import "cocos2d.h"
#import "Game.h"
#import "MathGame.h"
#import "HowToPlayLayer.h"
#import "AboutLayer.h"

@interface SplashScreenLayer : CCLayer
    // A bunch of stuff
@end

我在网上找不到任何有用的东西。有什么想法吗?

2 个答案:

答案 0 :(得分:28)

您只需要一个导入周期:

  1. Game导入SplashScreenLayer
  2. SplashScreenLayer导入MathGame
  3. MathGame导入Game
  4. 您的解决方案:

    import保留在MathGame内,并将其他导入更改为@class

    总结一下:

    // Game.h
    #import "cocos2d.h"
    #import <GameKit/GameKit.h>
    
    @class SplashScreenLayer;
    @interface Game : CCLayer
        // A bunch of stuff
    @end
    
    The new game type:
    
    // MathGame.h
    #import "Game.h"
    
    @interface MathGame : Game
    @end
    
    And the main menu that includes both:
    
    // SplashScreen.h
    #import "cocos2d.h"
    #import "HowToPlayLayer.h"
    #import "AboutLayer.h"
    
    @class Game;
    @class MathGame;
    @interface SplashScreenLayer : CCLayer
        // A bunch of stuff
    @end
    

    上面回答了你的问题,让我解释一下我从阅读有关前向变换和导入周期的一些事情:

    首先,去了解它们!它们是Objective-C的一个非常重要的部分,你不想错过它!

    其次,只要您需要私有变量或方法参数的类,就使用@class。使用导入继承和属性。

    第三,不要忘记在实施文件中#import转发的课程!

答案 1 :(得分:0)

在我的情况下,我使用xx类并使用@class而不是#import .h文件。编译抱怨..