尝试移除不再使用的显示对象时出现错误。
我遇到了这个错误:
尝试索引?空值
在 update 函数的开头。可能是什么原因呢?
--创建物品掉落
local createDrop=function()
drop = display.newImage("drop.png")
drop.x=math.random(drop.width, W-drop.width); drop.y=-50
drop.type="drop"
drops:insert(drop)
physics.addBody(drop)
print(drops.numChildren)
end
local dropTimer = timer.performWithDelay ( 1000, createDrop, 0)
--移除不在屏幕内的物品掉落
local function update()
for i=1,drops.numChildren do
if(drops[i].y>H) then
drops[i]:removeSelf()
drops[i] = nil
end
end
end
--Runtime:addEventListener("enterFrame", function() print(drop.y) end) --检查是否已删除屏幕外的物品掉落
Runtime:addEventListener("enterFrame", update)
我用另一种方式来做。这将确保屏幕外的每一滴都会被清除:
W = display.contentWidth H = display.contentHeight local drops = display.newGroup() local physics = require "physics" physics.start()
local drop = {} local dropIndex = 0
local function createDrop() dropIndex = dropIndex + 1 drop[dropIndex] = display.newImage("drop.png") drop[dropIndex].x = math.random(drop[dropIndex].width, W - drop[dropIndex].width) drop[dropIndex].y = -50 drop[dropIndex].type = "drop" drops:insert(drop[dropIndex]) physics.addBody(drop[dropIndex]) -- print(drops.numChildren) end local dropTimer = timer.performWithDelay(1000, createDrop, 0)
local function update() for i=1, #drop do if(drop[i] ~= nil and drop[i].y >= H) then drop[i]:removeSelf() drop[i] = nil print("removed... drop[" .. i .. "]") end end end timer.performWithDelay(10, update, 0) -- you can also use enterFrame event instead
```
继续编程............. :)
如果你只想在物体达到 >H 时移除它,可以使用对象的碰撞器。
它比使用 enterFrame 更便宜,无需将物体插入表中,比在其中使用 for 循环的 enterFrame 更快。我敢说更有效率。
--[[
这是与您的“drop”对象发生碰撞的传感器,它会在碰撞时自动删除。
它的位置在 y = H+10
]]
local removeSensorPoint = display.newRect(0,H+10,W,2)
removeSensorPoint.alpha = 0
removeSensorPoint.type = "removeSensor"
physics.addBody(removeSensorPoint,"static",{isSensor = true})
local createDrop = function()
drop = display.newImage("drop.png")
drop.x = math.random(drop.width, W-drop.width); drop.y=-50
drop.type = "drop"
physics.addBody(drop)
drop.collision = function(self,event)
if event.phase == "began" and event.other.type == "removeSensor" then
event.target:removeSelf()
end
end
drop:addEventListener("collision")
print(drops.numChildren)
end
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
错误来自于您的运行时,它试图移除已经被移除的对象,而运行时仍在运行。如果您正在尝试删除到达屏幕边界的对象,可以参考以下代码。我在这里删除了水滴组/表,因为我认为这不是必要的。
local physics = require("physics") physics.start() local W = display.contentWidth local H = display.contentHeight local createDrop=function() local drop = display.newImage("drop.png") drop.x=math.random(drop.width, W-drop.width); drop.y=-50 drop.type="drop" physics.addBody(drop) local function update() if(drop.y > H) then drop:removeSelf() drop = nil print("remove drop") Runtime:removeEventListener("enterFrame", update) end end Runtime:addEventListener("enterFrame", update) end local dropTimer = timer.performWithDelay ( 1000, createDrop, 0)