【问题标题】:Unexpected idenifier errors in xCode on cocos 2d gamecocos2d 游戏中的 xCode 出现意外的标识符错误
【发布时间】:2015-02-13 19:10:03
【问题描述】:
#import "MainScene.h"

static const CGFloat scrollSpeed = 80.f;

@implementation MainScene {
     CCSprite *_hero;
     CCPhysicsNode *_physicsNode;
     CCNode *_ground1;
     CCNode *_ground2;
     NSArray *_grounds;
     NSTimeInterval _sinceTouch;
}

- (void)didLoadFromCCB {
    _grounds = @[_ground1, _ground2];
    self.userInteractionEnabled =TRUE;
}

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    [_hero.physicsBody applyImpulse:ccp(0, 400.f)];

}
   // _sinceTouch+=delta;
    _hero.rotation = clampf(_hero.rotation, -30.f, 90.f);
    if (_hero.physicsBody.allowsRotation) {
        float angularVelocity = clampf(_hero.physicsBody.angularVelocity, -2.f, 1.f);
        _hero.physicsBody.angularVelocity = angularVelocity;
    }



//position of hero
    - (void)update: (CCTime) delta {

    _hero.position = ccp(_hero.position.x + delta * scrollSpeed, _hero.position.y);
    _physicsNode.position = ccp(_physicsNode.position.x - (scrollSpeed *delta), _physicsNode.position.y);
// loop the ground
    for (CCNode *ground in _grounds) {

        // get the world position of the ground
        CGPoint groundWorldPosition = [_physicsNode convertToWorldSpace:ground.position];

        // get the screen position of the ground
        CGPoint groundScreenPosition = [self convertToNodeSpace:groundWorldPosition];

        // if the left corner is one complete width off the screen, move it to the right
        if (groundScreenPosition.x <= (-1 * ground.contentSize.width)) {
            ground.position = ccp(ground.position.x + 2 * ground.contentSize.width, ground.position.y);
            // clamp velocity
            float yVelocity = clampf(_hero.physicsBody.velocity.y, -1 * MAXFLOAT, 200.f);
            _hero.physicsBody.velocity = ccp(0, yVelocity);

            NSTimeInterval _sinceTouch;
            if ((_sinceTouch > 0.5f)) {
                [_hero.physicsBody applyAngularImpulse:-40000.f*delta];
            }

        }
        - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
            [_hero.physicsBody applyImpulse:ccp(0, 400.f)];
            [_hero.physicsBody applyAngularImpulse:10000.f];
            _sinceTouch = 0.f;
        }
    }
}



@end

![错误][1]

你好,互联网,

我是一名高中生,在我的高级项目中,我正在制作一个飞扬的鸟克隆,这将在几周后完成,我需要一些帮助来解决代码中的一些错误,第一个是 _hero 的意外类型名称此行 // _sinceTouch+=delta; _hero.rotation = clampf(_hero.rotation, -30.f, 90.f); 和下一行中的意外标识符或“(”。

这一行还有一个- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 未声明使用标识符“touchbegan”。

http://i.stack.imgur.com/qEZCc.png

【问题讨论】:

  • 给出意外类型名称错误的代码超出了任何方法。代码必须在方法内部,而不是在方法之间。
  • 你能详细说明一下吗,对不起 imma n00b :)
  • 你是一个 n00b,你“写”了上面的代码,嗯?
  • 我在很多教程的帮助下做到了

标签: ios objective-c


【解决方案1】:
// _sinceTouch+=delta;

_hero.rotation = clampf(_hero.rotation, -30.f, 90.f);
if (_hero.physicsBody.allowsRotation) {
    float angularVelocity = clampf(_hero.physicsBody.angularVelocity, -2.f, 1.f);
    _hero.physicsBody.angularVelocity = angularVelocity;
}

不在词法范围内。它周围没有任何功能。删除该代码或将其放在相关区域。

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
   [_hero.physicsBody applyImpulse:ccp(0, 400.f)];
   [_hero.physicsBody applyAngularImpulse:10000.f];
   _sinceTouch = 0.f;
}

被限制在另一个函数内,这是被禁止的。将其移到

之前的右侧
@end

这个错误就会消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2018-10-30
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多