【发布时间】: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 <CCPhysicsCollisionDelegate> //@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