gpt4 book ai didi

objective-c - Cocos2d 中的 "Attempting to use the forward class ' 游戏 ' as superclass of ' MathGame '"

转载 作者:太空狗 更新时间:2023-10-30 03:18:50 27 4
gpt4 key购买 nike

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

我正在尝试创建另一种游戏模式 MathGame,它继承自 Game,但是当我尝试编译时,我在 MathGame 中遇到了这个错误.h:

Attempting to use the forward class 'Game' as superclass of 'MathGame'

即使 MathGame 的实现和接口(interface)为空,我也会收到错误消息。只有当我尝试将 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

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

最佳答案

您只需要一个导入周期:

  1. Game 导入 SplashScreenLayer
  2. SplashScreenLayer 导入 MathGame
  3. MathGame 导入 Game

您的解决方案:

保留 MathGame 中的 import,并将其他导入更改为 @class .

总结一下:

// Game.h#import "cocos2d.h"#import <GameKit/GameKit.h>@class SplashScreenLayer;@interface Game : CCLayer    // A bunch of stuff@endThe new game type:// MathGame.h#import "Game.h"@interface MathGame : Game@endAnd 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 的一个非常重要的部分,你不想错过它!

其次,只要您需要该类作为私有(private)变量或方法参数,就使用@class。将导入用于继承和strong 属性。

第三,不要忘记在实现文件中#import你转发的类!

关于objective-c - Cocos2d 中的 "Attempting to use the forward class ' 游戏 ' as superclass of ' MathGame '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com