我正在学习Lua并使用Corona(http://www.anscamobile.com/corona/)制作我的第一个“游戏”。它没问题,但我的代码没有按预期工作。
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Your code here
display.setStatusBar(display.HiddenStatusBar)
local random = math.random
local cwidth = display.contentWidth local cheight = display.contentHeight
local starTable = { }
for i = 0,400 do
table.insert (starTable, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))
end
local function reset( event )
for i = 0,#starTable do
starTable[i].isVisible = false
end
end
local function animate(event)
for i = 0,#starTable do
if random(0, 155) == 5 then
starTable[i].isVisible = true
end
end
timer.performWithDelay(150, reset)
end
Runtime:addEventListener("enterFrame", animate);
--This is the fade in at the start
local rect = display.newRect(0, 0, display.contentWidth, display.viewableContentHeight)
rect:setFillColor(0,0,0)
transition.to(rect, {time=2750, alpha=0})
--It should be on the bottom of this file, so it is always on top.
问题在于圈子不应该闪烁。我知道如果我在重置函数中放置渲染(例如display.newText(“test”,50,50,nil,53)),所有函数都会触发,它会渲染,但它不会闪烁。
它给了我这个错误:
?: iRuntime error ...\users\david\documents\corona projects\test\main.lua:19: attempt to index field '?' (a nil value) stack traceback: [C]: ? ...\users\david\documents\corona projects\test\main.lua:19: in function
'_listener' ?: in function <?:514> ?: iRuntime error ...\users\david\documents\corona projects\test\main.lua:19: attempt to index field '?' (a nil value)
stack traceback: [C]: ? ...\users\david\documents\corona projects\test\main.lua:19: in function '_listener' ?: in function <?:514> ?: i
编辑:修正了
修正了它 - 我忘了给它分配一个位置。
更改
table.insert (starTable, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))
到
table.insert (starTable, i, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))
答案 0 :(得分:0)
您应该在starTable中指明要放置圆圈的位置,否则它将插入到表格的末尾:
table.insert (starTable, position, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))
关于Lua表的更多信息:
http://lua-users.org/wiki/TablesTutorial
http://www.lua.org/pil/19.2.html