如何识别碰撞物体?(Corona SDK)
2014-4-16 13:14:50
收藏:0
阅读:110
评论:2
我是 Corona 开发的新手,并且我有一个问题无法识别三个对象之间的碰撞。 实际上,这是我的代码:
第一个对象是我的玩家:
player = display.newImageRect("ballon.png", 150,170 )
player.anchorX = 0.5
player.anchorY = 0.5
player.x = display.contentCenterX - 450
player.y = display.contentCenterY+250
physics.addBody(player, "static", {density=0.1, bounce=0.1, friction=0.1,})
player.myName="player"
screenGroup:insert(player)
第二个对象在一个函数内创建:
function addWindSlow(self,event)
height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windslow = display.newImage( "wind-neg.png")
windslow.x = display.contentWidth
windslow.y = height
windslow.speed = 4
windslow.myName="windslow"
physics.addBody(windslow, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windslow) end
function addWindFast(self,event)
height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windfast = display.newImage( "wind-pos.png")
windfast.x = display.contentWidth
windfast.y = height
windfast.speed = 4
windfast.myName="windfast"
physics.addBody(windfast, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windfast) end
最后,第三个对象相同,但称为“addWindFast()”,并带有不同的图像。
因此,一旦我的玩家发生碰撞,我需要执行某个操作...如果是windfast对象,则需要执行另一个操作。这是我的代码:
function onCollision( event )
if ( event.phase == "began" ) then
if event.other.windslow and event.other.isVisible==true then
print("windslow has been touch !")
end
end
end
我找到了一些关于预先碰撞的内容,一旦我得到了有关“识别”对象的信息,我将使用它...我将需要防止碰撞并避免如果玩家将要触摸windfast或windslow。
我们感谢你做出的一切!
点赞
用户1483762
你可以通过指定的 myName 属性来识别其他的对象。
例如:
function onCollision( event )
if ( event.phase == "began" ) then
if (event.other.myName == "windslow" and event.other.isVisible==true) then
print("windslow has been touched!")
elseif (event.other.myName = "windfast" and event.other.isVisible==true) then
print("windfast has been touched!")
end
end
2014-04-16 19:04:26
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
似乎
windslow和windfast是在onCollision事件处理程序之外定义的,所以你应该与它们进行比较:function onCollision( event ) if ( event.phase == "began" ) then if event.other == windslow and event.other.isVisible==true then print("windslow has been touch !") end if event.other == windfast and event.other.isVisible==true then print("windfast has been touch !") end end end -- (你的帖子中缺少这部分,但可能在你的代码中)上述假设你已经为你的玩家注册了碰撞事件。你也可以在每个 wind 上注册不同的碰撞处理程序:
windslow:addEventListener("collision", onCollisionSlow) windfast:addEventListener("collision", onCollisionFast)在这种情况下,两个碰撞处理程序只需要检查
if event.other == player。其中的优势是你正在分离每一个的代码。当然,你也可以在玩家处理程序中做同样的事情:function onCollisionSlow(other) print('collided with slow') end function onCollisionFast(other) print('collided with fast') end collisionHandlers = { [windslow] = onCollisionSlow, [windfast] = onCollisionFast, } function onCollision( event ) if ( event.phase == "began" ) then collisionFunc = collisionHandlers[event.other] if collisionFunc ~= nil and event.other.isVisible then collisionFunc(event.other) end end end