触发时圆圈不会消失
我正在学习 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)))
I am learning Lua and using Corona (http://www.anscamobile.com/corona/) to make my first "Game". It is going OK, but my code isn't working as it should.
-----------------------------------------------------------------------------------------
--
-- 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.
The problem is that the circles aren't flickering as they should be. I know all the functions are triggering if I put a render in the reset function (e.g display.newText("test", 50, 50, nil, 53)), it will render, but it doesn't flicker.
It give me this error:
?: 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
EDIT: Fixed it
Fixed it -- I forgot to assign it a pos.
Changed
table.insert (starTable, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))
to
table.insert (starTable, i, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 starTable 中指定要放置圆的位置,否则它将被插入到表的末尾:
有关 Lua 表的更多信息:
http://lua-users.org/wiki/TablesTutorial
http://www.lua.org/pil/19.2.html
You should indicate a position in starTable where you want to put your circle otherwise it will be inserted at the end of table:
More about Lua tables:
http://lua-users.org/wiki/TablesTutorial
http://www.lua.org/pil/19.2.html