使用未声明的标识符更新

时间:2013-06-09 05:10:22

标签: iphone ios objective-c xcode

我一直在关注教程,我正在努力正确地实现代码。我对Objective-C知之甚少,而另一个用户在此之前的步骤中出错,但他们的解决方案没有提供任何帮助。该项目的整个代码发布在下面,包括问题发生的地方。

欢呼帮助。

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

    _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Sprites.pvr.ccz"]; // 1
    [self addChild:_batchNode]; // 2
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Sprites.plist"]; // 3

    _ship = [CCSprite spriteWithSpriteFrameName:@"SpaceFlier_sm_1.png"];  // 4
    CGSize winSize = [CCDirector sharedDirector].winSize; // 5
    _ship.position = ccp(winSize.width * 0.1, winSize.height * 0.5); // 6
    [_batchNode addChild:_ship z:1]; // 7
    // 1) Create the CCParallaxNode
    _backgroundNode = [CCParallaxNode node];
    [self addChild:_backgroundNode z:-1];

    // 2) Create the sprites we'll add to the CCParallaxNode
    _spacedust1 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
    _spacedust2 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
    _planetsunrise = [CCSprite spriteWithFile:@"bg_planetsunrise.png"];
    _galaxy = [CCSprite spriteWithFile:@"bg_galaxy.png"];
    _spacialanomaly = [CCSprite spriteWithFile:@"bg_spacialanomaly.png"];
    _spacialanomaly2 = [CCSprite spriteWithFile:@"bg_spacialanomaly2.png"];

    // 3) Determine relative movement speeds for space dust and background
    CGPoint dustSpeed = ccp(0.1, 0.1);
    CGPoint bgSpeed = ccp(0.05, 0.05);

    // 4) Add children to CCParallaxNode
    [_backgroundNode addChild:_spacedust1 z:0 parallaxRatio:dustSpeed positionOffset:ccp(0,winSize.height/2)];
    [_backgroundNode addChild:_spacedust2 z:0 parallaxRatio:dustSpeed positionOffset:ccp(_spacedust1.contentSize.width,winSize.height/2)];
    [_backgroundNode addChild:_galaxy z:-1 parallaxRatio:bgSpeed positionOffset:ccp(0,winSize.height * 0.7)];
    [_backgroundNode addChild:_planetsunrise z:-1 parallaxRatio:bgSpeed positionOffset:ccp(600,winSize.height * 0)];
    [_backgroundNode addChild:_spacialanomaly z:-1 parallaxRatio:bgSpeed positionOffset:ccp(900,winSize.height * 0.3)];
    [_backgroundNode addChild:_spacialanomaly2 z:-1 parallaxRatio:bgSpeed positionOffset:ccp(1500,winSize.height * 0.9)];
    // Add to end of init method
    [self scheduleUpdate];

    // Add new update method
    - (void)update:(ccTime)dt{

        CGPoint backgroundScrollVel = ccp(-1000, 0);
        _backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, dt));

    }
    }
return self;
}

错误上下文

 // Add to end of init method
 [self scheduleUpdate];

 // Add new update method
 - (void)update:(ccTime)dt {

CGPoint backgroundScrollVel = ccp(-1000, 0);
_backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, dt));

 }

导致错误的行

  - (void)update:(ccTime)dt {

2 个答案:

答案 0 :(得分:1)

您不能在其他方法中声明方法。他们必须分开。

答案 1 :(得分:1)

在Objective-C中,您无法在方法内定义方法。因此,您应该将方法update的定义放在方法init之外。

-(id) init
{
    if( (self=[super init]))
    {
         //Implementation of init...
          [self scheduleUpdate];
    }
    return self;
}

// Add new update method
 - (void)update:(ccTime)dt
{
     CGPoint backgroundScrollVel = ccp(-1000, 0);
     _backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, dt));
 }