我一直在使用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)
答案 0 :(得分:1)
我猜你的变量或范围可能有问题。因此,请确保levelsToBeMade变量和positionX和positionY变量是正确的。如果你完全确定,这应该可以工作:(我的代码中没有看到任何错误,但是我认为for循环更可信。)
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
如果它不起作用,只需检查控制台并查看循环是否执行了所需的时间。
上次修改: 哦,没注意到。你以前从未创建过levelButton表!在开始创建关卡按钮之前,你应该像这样创建:local levelButton = {},在for循环之外
答案 1 :(得分:0)
您可能需要重新考虑您的算法并在循环时更改此内容。
如果要生成的级别数保持不变,则可能需要执行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
希望这有帮助。
干杯
答案 2 :(得分:0)
(对不起,我不知道Corona所以这可能不适用。)
什么是levelButton
,你确定它存在吗?也许它应该是levelButtons
?
如果情况确实如此,请检查以确保newButton()
实际上按预期返回一个表:print(levelButton[buttonNumber])