Xcode 4.3.1语法错误

时间:2012-10-27 04:50:35

标签: ios objective-c xcode syntax

我对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

预期的标识符正是X-Code提出的标准。

3 个答案:

答案 0 :(得分:1)

如果计算括号,您会看到开口括号比关闭括号多两个。我已将它们缩进到这里,以便您可以清楚地看到问题。

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

编译器试图告诉您它不希望在消息发送表达式的中间找到分号。我对Cocos2D框架不够熟悉,不知道你究竟要做什么,但至少你可以看出问题是什么。

答案 1 :(得分:0)

这一行:

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

有5个空心括号,只有4个小括号。需要有相同的数字(并在正确的位置)。很可能你需要在replaceScene:之后摆脱两个空心括号中的一个。

BTW - 你为什么要使用这样的旧Xcode?你应该使用最新的 - 4.5.1。

答案 2 :(得分:0)

更改

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

要,

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

那应该解决这个问题。您在[

之前有额外的[CCTransitionFade transitionWithDuration:1 scene:[GamePlay node]]