变量不可分配missing__block

时间:2014-02-25 09:48:49

标签: objective-c objective-c-blocks

我试图在我的游戏中添加一个10计数循环,但是当我运行代码时,它会生成一个“变量not assignedable missing__block”错误消息。谁能告诉我哪里出错了,并指出我正确的方向?

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
    CGPoint currentLocation = [[touches anyObject] locationInNode:self];
    CGPoint previousLocation = [[touches anyObject] previousLocationInNode:self];
    CGRect shipRect = _ship.frame;
    if (CGRectContainsPoint(shipRect, previousLocation))
    {
       [self launch:10 p1:currentLocation p2:previousLocation rect1:shipRect];
    }
 }

-(void)launch:(int)count p1:(CGPoint) currentLocation p2:(CGPoint) previousLocation rect1:(CGRect) shipRect
{
    CGPoint lvPosition = CGPointMake(_ship.position.x - (previousLocation.x - currentLocation.x), _ship.position.y);
_ship.position = lvPosition;

    SKAction *sound = [SKAction playSoundFileNamed:@"slideup.mp3" waitForCompletion:NO];
    SKAction *moveNode = [SKAction moveByX:lvPosition.x y:10.0 duration:3.0];

   [_ship runAction: sound];
   [_ship runAction: moveNode completion:^{[self launch:count-- p1:currentLocation p2:previousLocation rect1:shipRect];}]; /*variable not assignable missing__block error*/

}

2 个答案:

答案 0 :(得分:1)

我认为您正在尝试减少未定义为count的变量__block。您必须使用__block声明要在块内更改的每个变量,例如__block int count;

[编辑] 在您的情况下,最简单的解决方案是将count-1设置为递归调用,因为稍后不需要count--的结果

答案 1 :(得分:1)

__block局部变量不能在块中分配。

然而,使它__block可能不是你想要的。方法名称runAction:completion:听起来像是在操作完成后会调用“完成”块一次。由于它被调用一次,之后没有使用count,所以后递减是没有意义的。