【问题标题】:how to get index of the current collided object in corona?如何获取电晕中当前碰撞物体的索引?
【发布时间】:2017-02-16 02:48:06
【问题描述】:

我有一个名为ob1 的表格,其中包含 10 个图像(这些图像是随机创建并水平移动的),我在屏幕中间有一个栏,我已经为两者启用了物理并实现了碰撞事件侦听器以删除 @987654322 @image 当它们与 bar 碰撞时,但问题是当 3-4 个图像接近 bar 并且当第一个 ob1 与 bar 随机碰撞时,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

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:
    -- add an ID field for each object like this:
    local ob1={}
    for i=1,obslimit do
        ob1[i].id = i -- ** this is what I mean ** --
        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
            -- object collided is obj1[1]
        elseif (event.object1.id == 2 or event.object2.id == 2) then
            -- object collided is obj1[2]
            -- and so on. I hope you got this so far.
        and
    end
    

    【讨论】:

    • 我尝试了你的解决方案,但没有运气:((它抛出 nil 引用错误),我只想删除此刻与 bar 碰撞的那个 partilcuar 对象,如果有办法就好了这样做。
    • @caesargetit 是的。我的意思是您尝试此解决方案的代码。抱歉没有澄清
    • 你的id 想法给了我另一个想法,即为表中的每个对象声明object.myName 并通过该名称访问它们,现在我的问题已经完全解决了
    【解决方案2】:

    试试这个:

    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
    

    结束

    bar.collision = onLocalCollision
    bar:addEventListener("碰撞", bar)

    【讨论】:

    • 我对每个table object 都使用了object.myName,现在问题已经解决了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多