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

cst*_*ack 14 import inheritance objective-c forward-declaration

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

我正在尝试制作另一种游戏模式,MathGame它继承自Game,但是当我尝试编译时,我得到了以下错误MathGame.h:

尝试使用前瞻性"游戏"作为"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
Run Code Online (Sandbox Code Playgroud)

新游戏类型:

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

@interface MathGame : Game
@end
Run Code Online (Sandbox Code Playgroud)

主菜单包括:

// 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
Run Code Online (Sandbox Code Playgroud)

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

Maz*_*yod 28

你只需要一个导入周期:

  1. Game 进口 SplashScreenLayer
  2. SplashScreenLayer 进口 MathGame
  3. 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在实现文件中转发的类!