【问题标题】:Exclude collision listener to one object将碰撞侦听器排除到一个对象
【发布时间】:2013-03-05 04:34:20
【问题描述】:

在下面代码的第 6 行,我添加了一个 Runtime:addEventListener("collision", onCollision),但它正在与具有身体的所有事物进行交互,在这种情况下,如何将碰撞侦听器排除到一个对象“BCloud1.png”

function getRandomStar()
    local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
    local randomStar = display.newImage(temp.imgpath) -- Set image path for object

    if ( temp.imgpath == "BCloud1.png" ) then
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )
    Runtime:addEventListener("collision", onCollision)
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    end

    if ( temp.imgpath == "BCloud2.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )  
    end

    if ( temp.imgpath == "BCloud3.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )  
    end

    randomStar.myName = "star" -- Set the name of the object to star
    randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
    randomStar.x = math.random(-30, _W);    
    randomStar.y = -35;
    randomStar.rotation = math.random(0,20) -- Rotate the object

    starMove = transition.to(randomStar, {
        time=randomStar.movementSpeed, 
        y=500,
        onComplete = function(self) self.parent:remove(self); self = nil; 
        end
        }) -- Move the star
end--END getRandomStar()

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    如果你想使用

    Runtime:addEventListener("collision", onCollision)
    

    并控制什么与什么发生碰撞,然后使用碰撞过滤器。它们比看起来要容易得多。

    这是我学习它们时使用的教程。

    Collision Filters

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      相关资源
      最近更新 更多