我正在为iphone制作一款Cocos2d游戏,我有我的主游戏模式Game
,它继承了CCLayer
。
我正在尝试制作另一种游戏模式MathGame
,它继承自Game
,但当我尝试编译时,我在MathGame.h
:中遇到了这个错误
尝试使用正向类"Game"作为"MathGame"的超类
即使MathGame
的实现和接口为空,我也会收到错误。只有当我试图将MathGame.h
包含在另一个文件中时,才会发生这种情况。
这是游戏类的代码:
// 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
我在网上找不到任何有用的东西。有什么想法吗?
您只需要一个导入周期:
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作为私有变量或方法参数时,请使用该类。将导入用于继承和strong属性。
第三,不要忘记在实现文件中#import
您转发的类!
在我的例子中,我使用xx类并使用@class,但不使用#导入.h文件和编译抱怨。。