触发时圆圈不​​会消失

发布于 2024-12-22 19:37:11 字数 2250 浏览 3 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-12-29 19:37:12

您应该在 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

You should indicate a position in starTable where you want to put your circle otherwise it will be inserted at the end of table:

table.insert (starTable, position, display.newCircle(random(0,cwidth), random(0,cheight), random(0.8, 2.3)))

More about Lua tables:
http://lua-users.org/wiki/TablesTutorial
http://www.lua.org/pil/19.2.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文