本文介绍了“尝试使用前瞻性'游戏'作为'MathGame'的超类”在Cocos2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a Cocos2d game for iphone, and I have my main game mode, Game, which inherits from CCLayer.

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

I'm trying to make another game mode, MathGame, which inherits from Game, but when I try to compile, I get this error in MathGame.h:

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

I get the error even if the implementation and interface of MathGame are empty. And it only happens if I try to include MathGame.h in another file.

这是游戏的代码class:

Here's the code for the Game class:

// 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

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

I can't find anything helpful online. Any ideas?

推荐答案

你只需要一个导入周期:

You simply have an import cycle:


  1. 游戏进口 SplashScreenLayer

  2. SplashScreenLayer 导入 MathGame

  3. MathGame 进口游戏

  1. Game imports SplashScreenLayer
  2. SplashScreenLayer imports MathGame
  3. MathGame imports Game

您的解决方案:

MathGame 中保留 import ,并将其他导入更改为。

Leave the import inside the MathGame, and change the other imports to @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






上面回答你的问题,让我解释一下我在阅读有关前向变更和进口周期时已经知道的一些事情:


With your question answered above, let me explain a few things I already know from reading about forward declerations and import cycles:

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

First, go read about them! They are a very important part of Objective-C, and you don't want to miss it!

其次,每当你需要私有变量的类时使用@class或者方法参数。使用导入继承和属性。

Secondly, use @class whenever you need that class for private variables or method parameters. Use imports for inheritance and strong properties.

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

Thirdly, don't forget to #import your forwarded classes in the implementation file!

这篇关于“尝试使用前瞻性'游戏'作为'MathGame'的超类”在Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:12