0

我正在尝试为我正在开发的游戏制作几百个关卡,所以显然我想使用 for 循环而不是单独创建每个按钮。

我做这一切都很好,但是我当前使用的代码使得每个按钮的调用块都是相同的。显然,如果我要将不同级别的按钮转到不同的级别,那么每个级别的调用块必须不同。

为了区分我在哪个级别,我调用[[LevelManager sharedInstance] nextLevel]了与级别编号对应的次数。我试图通过获取用户触摸按钮的触摸位置,获取行/列号,然后运行上述代码一定次数来更改此数字。但是,在触摸按钮后运行整个调用块之前它没有更新触摸位置,这显然使我的代码无法工作。

有没有办法在调用块完成之前手动更新触摸位置?有没有办法以某种方式将每个按钮存储在一个数组中并为每个按钮建立一个不同的调用块?我不确定解决我的问题的最佳方法是什么。谢谢,我的代码如下。

初始化时调用

for (int l = 0; l < NUMBER_OF_WORLDS; l++) {
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < 5; k++) {
                NSString *tempTitle = [NSString stringWithFormat:@"%i",(k+1)+(l*5)];
                CCButton *tempButton;
                tempButton = [CCButton buttonWithTitle:tempTitle]
                tempButton.block = ^(id sender) {
                    int row = floorf(touchLocation.y/(screenBounds.size.height/6)) + 1;
                    int column = floorf(touchLocation.x/(screenBounds.size.width/4)) + 1;
                    int buttonIndex = row*column;
                    for (int m = 1; m < buttonIndex; m++) {
                        [[LevelManager sharedInstance] nextLevel];
                    }
                    CCScene *gameplayScene = [CCBReader loadAsScene:@"LevelScene"];
                    [[CCDirector sharedDirector] replaceScene:gameplayScene];
                    levelNumber = i;
                };
                tempButton.position = ccp((screenBounds.size.width/4)*j + levelImagePlaceholder.contentSize.width*tempButton.scale, screenBounds.size.height/6*k + 50 + l*screenBounds.size.height);
                [self addChild:tempButton];
                i++;
            }
        }
}

获取触摸位置的方法

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CCLOG(@"touch ran");
    touchLocation = [touch locationInNode:self];
}
4

1 回答 1

1

您已经有可用的行/列。他们是 j 和 k。可以在按钮的“按下时”块中引用这些变量。

于 2014-06-23T01:10:38.960 回答