简体   繁体   English

Xcode 4.3.1语法错误

[英]Xcode 4.3.1 syntax errors

I'm pretty new to coding on Xcode (or objective-c in general) and I can't seem to get rid of these errors: 我对使用Xcode(或一般来说为Objective-c)进行编码非常陌生,但似乎无法摆脱这些错误:

  //
    //  HelloWorldLayer.m
    //  FirstGame
    //
    //  Created by Kostas on 1/14/12.
    //  Copyright __MyCompanyName__ 2012. All rights reserved.
    //


    // Import the interfaces
    #import "HelloWorldLayer.h"
    #import "GamePlay.h"

    // HelloWorldLayer implementation
    @implementation HelloWorldLayer
    +(id) scene {

        CCScene *scene = [CCScene node];

        HelloWorldLayer *layer = [HelloWorldLayer node];
        // add layer as a child to scene
        [scene addChild: layer];
        // return the scene
        return scene;
    }

    // on "init" you need to initialize your instance

    -(id) init {

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

            [CCMenuItemFont setFontName:@"Marker Felt"];
            [CCMenuItemFont setFontSize:35];
            CCMenuItem *Play = [CCMenuItemFont itemFromString:@"PLAY"
                                target:self
                                selector:@selector(gotoGameplay:)];

            CCMenu *menu = [CCMenu menuWithItems: Play, nil];
            menu.position = ccp(240, 160);
            [menu alignItemsVerticallyWithPadding:10];
            [self addChild:menu];
        }

        return self;
    }

    -(void) goToGameplay: (id) sender {

        [[CCDirector sharedDirector] 
               replaceScene:[[CCTransitionFade 
                    transitionWithDuration:1 
                    scene:[GamePlay node]
                              ]];   **<-----Here is my error it says "Expected identifier"**
    }


    - (void) dealloc {


        [super dealloc];
    }
    @end

The expected identifier is just what X-Code came up with. 预期的标识符就是X-Code提出的。

If you count your brackets, you'll see that you have two more opening brackets than closing brackets. 如果算上括号,您会发现比起括号多了两个开括号。 I've indented them here so you can see the problem clearly. 我在这里缩进它们,以便您可以清楚地看到问题。

-(void) goToGameplay: (id) sender {
    [
      [CCDirector sharedDirector] 
      replaceScene:
      [ // <-- either this is extra
        [CCTransitionFade transitionWithDuration:1
                                           scene:[GamePlay node]
        ]
      ];
  //];   <-- or this is missing
}

The compiler is trying to tell you that it wasn't expecting to find a semicolon in the middle of a message send expression. 编译器试图告诉您,它不希望在消息send表达式的中间找到分号。 I'm not familiar enough with the Cocos2D framework to know what exactly you're trying to do, but at least you can see what problem is. 我对Cocos2D框架还不太熟悉,无法知道您到底想做什么,但是至少您可以看到问题所在。

This line: 这行:

[[CCDirector sharedDirector] 
           replaceScene:[[CCTransitionFade 
                transitionWithDuration:1 
                scene:[GamePlay node]
                          ]];

has 5 open brackets and only 4 close brackets. 有5个右括号,只有4个右括号。 There needs to be the same number (and in the right places). 必须有相同的数字(在正确的位置)。 Most likely you need to get rid of one of the two open brackets after replaceScene: . replaceScene:之后,您很可能需要摆脱两个开括号之一。

BTW - why are you using such and old Xcode? 顺便说一句-您为什么使用这样的旧Xcode? You should use the latest - 4.5.1. 您应该使用最新的-4.5.1。

Change 更改

[[CCDirector sharedDirector] 
               replaceScene:[[CCTransitionFade 
                    transitionWithDuration:1 
                    scene:[GamePlay node]
                              ]];

to, 至,

[[CCDirector sharedDirector] replaceScene:
        [CCTransitionFade transitionWithDuration:1
                                           scene:[GamePlay node]]];

That should fix the issue. 那应该解决问题。 You had an extra [ before [CCTransitionFade transitionWithDuration:1 scene:[GamePlay node]] [CCTransitionFade transitionWithDuration:1 scene:[GamePlay node]]之前,您有一个额外的[

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM