【问题标题】:Lua Create multiple collisionsLua 创建多个碰撞
【发布时间】:2013-05-09 01:11:31
【问题描述】:

我正在尝试创建一个破坏者游戏,其中包含可以在消失前被击中两次的边缘。

我试过了:

 --FOR STRONGER DEFENDERS
        for i = 1, len do
            for j = 1, level_W do
                if(level[i][j] == 2) then               
                    local strong = display.newImage('images/strongdefender.png')
                    strong.name = 'strong'
                    strong.x = def_W * j - offset
                    strong.y = def_H * i
                    physics.addBody(strong, {density = 1, friction = 0, bounce = 0})
                    strong.bodyType = 'static'
                    strongs.insert(strongs, strong)
                end
            end
        end
        for i = 1, len do
            for j = 1, level_W do
                if(level[i][j] == 2) then
                local defender = display.newImage('images/defender.png')
                    defender.name = 'defender'
                    defender.x = def_W * j - offset
                    defender.y = def_H * i
                    physics.addBody(defender, {density = 1, friction = 0, bounce = 0})
                    defender.bodyType = 'static'                    
                end
            end
        end

level 是一个用 0 和 2 填充的表格。2 是防守者图像在游戏中的位置。

我的碰撞事件是这样的:

function onCollision(e)


        if(e.other.name == 'defender' or e.other.name == 'strong' and (ball.x + ball.width * 0.5) < (e.other.x + e.other.width * 0.5)) then
            xSpeed = -5
        elseif(e.other.name == 'defender' or e.other.name == 'strong' and (ball.x + ball.width * 0.5) >= (e.other.x + e.other.width * 0.5)) then
            xSpeed = 5
        end

        if(e.other.name == 'defender') then
            audio.play(defencePop)
            ySpeed = ySpeed * -1
            e.other:removeSelf()
            e.other = nil
            defenders.numChildren = defenders.numChildren - 1

            --SORT SCORE
            score = score + 1
            scoreNum.text = score * points
            scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
            scoreNum.x = 54 
        elseif(e.other.name == 'strong') then
            audio.play(defencePop)
            ySpeed = ySpeed * -1
            e.other:removeSelf()
            e.other = nil
            defenders.numChildren = defenders.numChildren - 1

            --SORT SCORE
            score = score + 1
            scoreNum.text = score * points
            scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
            scoreNum.x = 54     

        end




        --defenders.numChildren < 0
        if(strongs.numChildren < 0) then
            bgAlert('win')
            gameStatus = 'win'
        end
    end -- removeDefender

当球与元素碰撞时,它们都消失了。我怎样才能让一个人一次消失?

【问题讨论】:

    标签: lua coronasdk collision


    【解决方案1】:

    我建议您将强块放在屏幕上并为它们添加一个事件侦听器,当与较强的砖发生碰撞时,较弱的砖会被创建。这个想法是在发生碰撞时删除对象并添加一个新对象。
    首先,您将显示强对象并将它们添加到物理中。您还可以向对象添加本地碰撞事件侦听器

     for i = 1, len do
         for j = 1, level_W do
             if(level[i][j] == 2) then               
                 local strong = display.newImage('images/strongdefender.png')
                 strong.name = 'strong'
                 strong.x = def_W * j - offset
                 strong.y = def_H * i
                 physics.addBody(strong, {density = 1, friction = 0, bounce = 0})
                 strong.bodyType = 'static'
                 strong.collision = onBrickCollision --onStrongCollision is the name of the collision handler function
                 strong:addEventListener("collision" , strong) --add collision listener
                 strongs.insert(strongs, strong)
             end
         end 
     end
    

    事件监听器是这样的

    function onBrickCollision(self , event)
    if event.phase == "began" and event.other.name == "ball" then
        if (ball.x + ball.width * 0.5) < (self.x + self.width * 0.5) then
            xSpeed = -5
        else
            xSpeed = 5
        end
        if self.name == "strong" then
            audio.play(defencePop)
            ySpeed = ySpeed * -1
            --Create defender on the position of strong and add it to physics
            local defender = display.newImage('images/defender.png')
            defender.name = 'defender'
            set the position same as the object which is hit by ball
            defender.x = self.x
            defender.y = self.y
            physics.addBody(defender, {density = 1, friction = 0, bounce = 0})
            defender.bodyType = 'static'
            defender.collision = onBrickCollision
            defender:addEventListener("collision", defender)
            --remove the strong brick
            self:removeSelf()
            self = nil
    
            --SORT SCORE
            score = score + 1
            scoreNum.text = score * points
            scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
            scoreNum.x = 54     
    
        elseif self.name == "defender" then
            audio.play(defencePop)
            ySpeed = ySpeed * -1
            self:removeSelf()
            self = nil
            defenders.numChildren = defenders.numChildren - 1
    
            --SORT SCORE
            score = score + 1
            scoreNum.text = score * points
            scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
            scoreNum.x = 54 
        end
    end
    

    希望代码是不言自明的 :)
    如果您有任何其他问题,请随时问我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2014-04-25
      • 2021-12-13
      • 1970-01-01
      • 2013-10-28
      • 2016-08-24
      • 2017-08-24
      相关资源
      最近更新 更多