我在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一起工作,如果有的话:
in IntroScene.m
- (void)onPlayClicked:(id)sender
{
[[CCDirector sharedDirector] replaceScene:[MainScene scene]
withTransition:[CCTransition transitionCrossFadeWithDuration:1.0f]];
}
ARC语义错误:选择器'scene'没有已知的类方法 语义问题:不兼容的指针类型将'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(错误:选择器'scene'没有已知的类方法)&lt; - replaceScene行中代码的底部
// 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的代码片段:
@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;
}
我是否在上面代码段的括号中声明了我的MainMenu场景类?如果是这样的话?
我不确定如何将此(下方)放入我的实施中......
+(MainMenu) scene
{
CCScene *scene = [CCScene node];
return scene;
}
答案 0 :(得分: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将是一个很好的起点。