如何在 Corona 中获取当前碰撞对象的索引?
2017-2-15 18:48:7
收藏:0
阅读:103
评论:2
我有一个名为 ob1 的表格,其中包含 10 张图片(这些图片是随机创建并向水平方向移动的),我屏幕中央有一条障碍,我已经启用了物理效果,并实现了碰撞事件监听器,当 ob1 图像与障碍物发生碰撞时,会将其删除,但问题是当 3-4 张图片朝障碍物移动并且第一个 ob1 与障碍物碰撞时,随机的 ob1 对象被删除了,但是当前的 ob1 对象并没有被删除,我该如何获取当前发生碰撞的 ob1 对象的 ID?
代码如下:
local ob1={}
for i=1,obslimit do
ob1[i]=display.newImage( "images/ob1.png", 250,250)
ob1[i].isVisible=false
ob1[i].isAlive=false
physics.addBody( ob1[i], "dynamic", {friction=1,bounce=0.0})
ob1[i].gravityScale=0
ob1[i].isBodyActive=false
end
--function to deal ob1 collision
local function ob1cols( self,event )
if(event.phase=="began") then
--print(self.myName..event.other.myName)
local ob1_elem = require("mydata")
--ob1_elem.new.isBodyActive=false
ob1_elem.new.isVisible=false
ob1_elem.new.isAlive=false
end
end
--function to deal ob1 group pooling
local function getob1()
--calling ob1 from pool
for i=1, #ob1 do
if not ob1[i].isAlive then
--print( "got one" )
return ob1[i]
end
end
return nil
end
--function to deal obstacle spawning
local function obdecide(event)
if (mytime==100) then
--local ob1_elem = getob1()
local ob1_elem = require("mydata")
ob1_elem.new=getob1()
if (ob1_elem.new~=nil) then
ob1_elem.new.isVisible=true
ob1_elem.new.isAlive=true
ob1_elem.new.isBodyActive=true
ob1_elem.new.x=math.random( 200,350 )
--trying to add event listner for every object
ob1_elem.new.myName="cactus"
ob1_elem.new:setLinearVelocity( -20, 0 )
ob1_elem.new.collision=ob1cols
ob1_elem.new:addEventListener( "collision", ob1_elem.new)
end
elseif (mytime>100) then
mytime=0
end
end
点赞
用户4927949
尝试一下:
ob1[i].name="object";
local function onLocalCollision(event,self)
if (event.phase == "began") then
elseif (event.phase == "ended") then
if (event.other.name == "object") then
event.other:removeSelf()
end
end
end
bar.collision = onLocalCollision
bar:addEventListener("collision", bar)
2015-05-25 09:54:20
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
-- 为每个对象添加一个ID字段,如下所示: local ob1={} for i=1,obslimit do ob1[i].id = i -- 这是我的意思 ob1[i]=display.newImage( "images/ob1.png", 250,250) ob1[i].isVisible=false ob1[i].isAlive=false physics.addBody( ob1[i], "dynamic", {friction=1,bounce=0.0}) ob1[i].gravityScale=0 ob1[i].isBodyActive=false end
function onCollision (event) if (event.object1.id == 1 or event.object2.id == 1) then - 碰撞的对象是obj1 [1] elseif (event.object1.id == 2 or event.object2.id == 2) then -- 碰撞的对象是obj1 [2] --以此类推。我希望你到现在为止已经明白了。 and end
```