0

消える前に 2 回ヒットできる瀬戸際を含むブレーク ブレーカー ゲームを作成しようとしています。

私が試してみました:

 --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

ボールが要素に衝突すると、両方とも消えます。一度に1つずつ消えるにはどうすればよいですか?

4

1 に答える 1

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

コードが自明であることを願っています :)
さらに質問があれば、お気軽にお尋ねください。

于 2013-05-20T15:38:25.510 に答える