【问题标题】:Duplicate interface definition for class MainScene?MainScene 类的接口定义重复?
【发布时间】:2014-02-24 13:26:05
【问题描述】:

我的 MainScene.h 文件中有这一行。

 @interface MainScene : CCNode <CCPhysicsCollisionDelegate>

我重新声明它添加了这一行:

 @interface MainScene : CCScene

我正在尝试在我的 objective-c/cocos2d iOS 应用程序上添加碰撞,以及将转换到游戏场景的主菜单场景。我正在关注一个关于碰撞效果的教程 (https://www.makegameswith.us/gamernews/369/build-your-own-flappy-bird-with-spritebuilder-and) 和这个关于主菜单场景的教程 (http://www.albertopasca.it/whiletrue/2014/02/how-to-make-flappy-bird-like-game-using-cocos2d/)。主菜单场景代码(github上传)使用CCScene,碰撞效果以及整个游戏场景代码使用CCNode。

所以我的问题是....我如何使它工作?如何将 CCNode 和 CCScene 结合在一起?

我想将以下代码添加到我的 MainScene.h:

 @interface IntroScene : CCScene


 + (IntroScene *)scene;
 - (id)init;


 @end

用 MainScene 替换 IntroScene

但如果我这样做,我会在 MainScene.m 文件中收到警告:

你们可能会问我警告是什么,但我稍后会发布。

这是我得到的错误,让我想知道如何让 CCNode 和 CCScene 一起工作,如果有的话:

在 IntroScene.m 中

 - (void)onPlayClicked:(id)sender
 {
[[CCDirector sharedDirector] replaceScene:[MainScene scene]
                           withTransition:[CCTransition transitionCrossFadeWithDuration:1.0f]];
 }

ARC 语义错误:选择器“场景”没有已知的类方法 语义问题:不兼容的指针类型将“MainScene*”发送到“CCScene *”类型的参数

更新....我目前的代码:

IntroScene.h(错误:方法原型后应为“;”)

 #import "cocos2d.h"
 #import "cocos2d-ui.h"

 // -----------------------------------------------------------------------

 /**
  *  The intro scene
  *  Note, that scenes should now be based on CCScene, and not CCLayer, as previous versions
  *  Main usage for CCLayer now, is to make colored backgrounds (rectangles)
  *
  */
 @interface IntroScene : CCScene

 // -----------------------------------------------------------------------

 //+ (IntroScene *)scene;
 //- (id)init;

 // -----------------------------------------------------------------------

 + (id)scene
 {
CCScene *scene = [CCScene node];

IntroScene *layer = [IntroScene node];

[scene addChild: layer];

return scene;
 }

 -(id) init
 {

if( (self=[super init] )) {

}
return self;
 }

 - (void) dealloc
 {

 [super dealloc];
 }

 @end

IntroScene.m (Error: No known class method for selector 'scene')

 // Import the interfaces
 #import "IntroScene.h"
 #import "MainScene.h"

 // -----------------------------------------------------------------------
 #pragma mark - IntroScene
 // -----------------------------------------------------------------------

 @implementation IntroScene

 // -----------------------------------------------------------------------
 #pragma mark - Create & Destroy
 // -----------------------------------------------------------------------

 + (IntroScene *)scene
 { 
return [[self alloc] init];
 }

 // -----------------------------------------------------------------------

 - (id)init
 {
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);

CCSprite *fxBackground = [CCSprite spriteWithImageNamed:@"MarioBackground-static.png"];
fxBackground.anchorPoint = ccp(0, 0);
fxBackground.position    = ccp(0, 0);
[self addChild:fxBackground];

CCLabelTTF *label = [CCLabelTTF labelWithString:@"SUPER TORTOISE BROS."
                                       fontName:@""
                                       fontSize:40.0f];
label.positionType = CCPositionTypeNormalized;
label.color = [CCColor whiteColor];
label.position = ccp(0.5f, 0.6f);
[self addChild:label];

CCSpriteFrame *frame = [CCSpriteFrame frameWithImageNamed:@"buttonPlay.png"];
CCButton *playButton = [CCButton buttonWithTitle:@"" spriteFrame:frame];
playButton.anchorPoint = ccp(0, 0);
playButton.position = ccp(30, 120);
[playButton setTarget:self selector:@selector(onSpinningClicked:)];
[self addChild:playButton];

frame = [CCSpriteFrame frameWithImageNamed:@"buttonScore.png"];
CCButton *scoreButton = [CCButton buttonWithTitle:@"" spriteFrame:frame];
scoreButton.anchorPoint = ccp(0, 0);
scoreButton.position = ccp(self.contentSize.width - 130, 120);
//  [scoreButton setTarget:self selector:@selector(todo:)];
[self addChild:scoreButton];


// done
return self;
 }

 // -----------------------------------------------------------------------
 #pragma mark - Button Callbacks
 // -----------------------------------------------------------------------

 - (void)onPlayClicked:(id)sender
 {
 [[CCDirector sharedDirector] replaceScene:[MainScene scene]
                           withTransition:[CCTransition  transitionCrossFadeWithDuration:1.0f]];
 }


 // -----------------------------------------------------------------------
 @end

MainScene.h

 @interface MainScene : CCScene <CCPhysicsCollisionDelegate>
 {
 }

 +(MainScene*) scene;


 @end

MainScene.m 中的代码 sn-p:

 @implementation MainScene {
CCButton *_restartButton;
CCSprite *_hero;
CCPhysicsNode *_physicsNode;
CCNode *_ground1;
CCNode *_ground2;
NSArray *_grounds;
NSTimeInterval _sinceTouch;
NSMutableArray *_obstacles;
BOOL _gameOver;
BOOL Start;
CGFloat _scrollSpeed;
NSInteger _points;
CCLabelTTF *_scoreLabel;
CCLabelTTF *_highScore;
 }

我是否在上面 sn-p 的括号之间声明了我的 MainMenu 场景类?如果有怎么办?

不知道如何将它(如下)放入我的实现中......

 +(MainMenu) scene
  {
 CCScene *scene = [CCScene node];
 return scene;
 }

【问题讨论】:

  • 您的 MainScene.h 是否正在导入 IntroScene.m?该错误似乎表明它不知道@interface MainScene : CCScene。另外,MainScene 有 '+ (MainScene *)scene' 方法吗?
  • @PhillipMills - 是的,IntroScene.m 已导入 MainScene.h。这是 MainScene.h 中的代码:@interface MainScene : CCNode &lt;CCPhysicsCollisionDelegate&gt; //@end //@interface MainScene : CCScene + (MainScene *)scene; //- (id)init; @end 我收到以下警告: IntroScene.m [[CCDirector sharedDirector] replaceScene:[MainScene scene] 不兼容的指针类型将 'MainScene *' 发送到类型为 'CCScene *' MainScene.m @implementation MainScene { 的方法定义'找不到场景
  • 好的,所以“指针不兼容”问题是因为 MainScene 不是 CCScene 子类。另一个警告似乎是因为您的实现中没有+ (MainScene *)scene {…}。你需要决定MainScene 将是什么类型的东西,然后只在与其定义匹配的方法中使用它。
  • @PhillipMills - 是的,这就是问题所在。 MainScene 将成为游戏场景所在的位置。这是应用程序首次启动时 IntroScene 过渡到的场景。但是,我使用的教程需要两个不同的类,CCNode(用于对象碰撞)和 CCScene(用于场景转换)。所以我试图弄清楚如何让它们一起工作。如果没有 CCNode 类,我当前的 MainScene 代码将无法工作,如果没有 CCScene 类,场景转换将无法工作。

标签: ios objective-c cocos2d-iphone


【解决方案1】:

CCScene 继承自 CCNode。使用下面的接口声明

@interface MainScene : CCScene <CCPhysicsCollisionDelegate>
+ (MainScene *)scene;
@end

另外,请确保您已经实现了 + (MainScene *)scene 方法。根据您的要求在 init 或 onEnter 中启用类中的用户交互,这应该允许您利用对象碰撞以及场景转换。

编辑

你得到的是“;”错误,因为您试图在 IntroScene.h 的头文件中定义一个函数。您应该只在头文件中声明您的函数,即如果您希望它们是公开的。您的 IntroScene.h 应该如下所示。

@interface IntroScene : CCScene

+(IntroScene*) scene;
-(id) init;
@end

您的代码中还有其他各种缺陷。我建议通过教程来复习某些概念。 Raywenderlich's tutorial 将是一个不错的起点。

【讨论】:

  • 谢谢,但现在我在尝试实现该方法时遇到错误:+ (id)scene { CCScene *scene = [CCScene node]; IntroScene *layer = [IntroScene node]; [scene addChild: layer]; return scene; } -(id) init { if( (self=[super init] )) { } return self; } - (void) dealloc { [super dealloc]; } 我收到一条错误消息,提示为 Expected ';'在方法原型之后。如果我在方法原型后加一个分号,我会得到预期标识符或左括号的“(”警告。
  • 你能用最新的代码更新你的答案吗?这将有助于更好地理解您的代码..
  • 我收到以下错误:IntroScene.m 中此代码的选择器“场景”没有已知的类方法:[[CCDirector sharedDirector] replaceScene:[MainScene scene] 没有出现警告。
  • 您正在从 MainScene 类调用一个名为“scene”的静态函数,该函数未在头文件中声明。需要在头文件中声明+(MainScene*)scene;并在实现文件中实现该功能。
  • 有点困惑......我在我的 MainScene.h 文件中声明了 MainScene。没有出现任何错误,但现在出现了一条引用此代码的警告:@implementation MainScene { CCButton *_restartButton; CCSprite *_hero; CCPhysicsNode *_physicsNode; CCNode *_ground1; CCNode *_ground2; NSArray *_grounds; NSTimeInterval _sinceTouch; NSMutableArray *_obstacles; BOOL _gameOver; BOOL Start; CGFloat _scrollSpeed; NSInteger _points; CCLabelTTF *_scoreLabel; CCLabelTTF *_highScore; } 未找到“场景”的方法定义。
猜你喜欢
  • 2012-05-30
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多