Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 通过迭代创建按钮?_Loops_Button_Lua_Coronasdk - Fatal编程技术网

Loops 通过迭代创建按钮?

Loops 通过迭代创建按钮?,loops,button,lua,coronasdk,Loops,Button,Lua,Coronasdk,我一直在使用Corona引擎,我试图通过一个循环创建多个按钮,而不是显式地创建每个按钮。问题是循环似乎只生成一个按钮,这表明它只迭代了一次 下面是我到目前为止得到的 已更新 --> Create Level Selection: local levelSelectionGroup = display.newGroup( ); --> Level Selected: local function levelSelected() print(id); end -->

我一直在使用Corona引擎,我试图通过一个循环创建多个按钮,而不是显式地创建每个按钮。问题是循环似乎只生成一个按钮,这表明它只迭代了一次

下面是我到目前为止得到的

已更新

--> Create Level Selection:

local levelSelectionGroup = display.newGroup( );

--> Level Selected:

local function levelSelected()
    print(id);
end

--> Button Creation:

local function createLevelSelection()
    local levelsToBeMade = 30; -- Ignore these random numbers for now.
    local positionX = 1; -- Ignore again.
    local positionY = 1; -- Ignore again.

    for buttonNumber=1, levelsToBeMade do
        print(buttonNumber);

        positionX = (positionX + 10); -- Ignore again.
        positionY = (positionY + 10); -- Ignore again.

        levelButton[buttonNumber] = widget.newButton{ 
            id = buttonNumber,
            label = buttonNumber,
            default = "images/levelButton.png",
            over = "images/levelButtonPressed.png",
            width = 50,
            height = 50,
            onRelease = levelSelected
        }

        levelButton[buttonNumber].x = positionX;
        levelButton[buttonNumber].y = positionY;

        levelSelectionGroup:insert(levelButton[buttonNumber]);
    end
end
控制台上说

attempt to index global 'levelButton' (a nil value)

您可能需要重新考虑您的算法并更改此while循环

如果要生成的级别数保持不变,您可能希望执行for循环,直到达到第31个循环

 for i=1,levelstToBeMade  do

    levelstToBeMade = (levelsToBeMade - 1); -- Ignore numbers.
    positionX = positionX + 10; -- Ignore numbers.
    positionY = postionY + 10; -- Ignore numbers.

    levelButton[levelsToBeMade] = widget.newButton{ 
        id = levelsToBeMade,
        label = levelsToBeMade,
        default = "images/levelButton.png",
        over = "images/levelButtonPressed.png",
        width = 50,
        height = 50,
        onRelease = levelSelected
    }

    levelButton[levelsToBeMade].x = positionX;
    levelButton[levelsToBeMade].y = positionY;

 end
希望这有帮助


干杯

我想您的变量或范围可能有问题。因此,请确保levelstobemake变量和positionX和positionY变量是正确的。如果您完全确定,这应该是可行的:(我看不出您的代码中有任何错误,但是,我想循环更可信。)

如果它不起作用,只需检查控制台,看看循环是否在期望的时间执行

上次编辑:
哦,我没注意到。您以前从未创建过levelButton表!在开始创建级别按钮之前,您应该这样创建:本地级别按钮={},位于for循环的外部(对不起,我不知道,所以这可能不适用)

什么是
levelButton
,您确定它存在吗?也许应该是
levelButtons


如果没有问题,请检查以确保
newButton()
实际返回预期的表格:
print(levelButton[buttonNumber])
输入错误:
levelstToBeMade
?啊。遗憾的是,这仍然没有解决it不循环的问题。我认为您需要将它们推/插入到一个显示组中。我尝试了一下,但仍然没有成功,至少将它们都分组在一起很好。公平点,可悲的是,这并没有绕过原始问题。正如我在问题中所说,它似乎只迭代循环一次,但我有一种感觉,这不是由于循环本身,而是在循环过程中遇到了一个问题,导致循环停止。这就是为什么我在那里放了一个打印函数。所以,您可以检查循环是否完成,或者它是否在内部和点中给出错误。如果你能告诉我控制台打印的是什么,那就太好了,如果你检查一下我问题中的更新代码,你会发现我里面有一个“打印”函数。控制台吐出的错误在问题的底部。哦,我没注意到。您以前从未创建过levelButton表!在开始创建级别按钮之前,您应该这样创建:locallevelbutton={},位于for loopAh ha的外部!答对 了谢谢,这就是我们需要的!(我建议在答案中加上这一点,以便其他人将来找到!)不确定拼写错误在哪里,但现在已经全部解决了。
for i=1, levelsToBeMade do

    print( "levelButton+1).." will be created." )

    positionX = positionX + 10; -- Ignore numbers.
    positionY = postionY + 10; -- Ignore numbers.

    levelButton[#levelButton+1] = widget.newButton{ 
        id = #levelButton,
        label = #levelButton,
        default = "images/levelButton.png",
        over = "images/levelButtonPressed.png",
        width = 50,
        height = 50,
        onRelease = levelSelected
    }

    levelButton[#levelButton].x = positionX;
    levelButton[#levelButton].y = positionY;
end