我正在为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
我在网上找不到任何有用的东西。有什么想法吗?
答案 0 :(得分:28)
您只需要一个导入周期:
Game
导入SplashScreenLayer
SplashScreenLayer
导入MathGame
MathGame
导入Game
您的解决方案:
将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文件。编译抱怨..