在输入特定文本后使其消失。

下面的代码在子弹击中气球时将气球移除。但我卡在了如何使具体的气球文本在被子弹击中时消失。例如,我想使具有“balloonText2”的气球在被子弹击中时消失。我如何检测它是我击中的第二个气球?

function createBalloons(a, b)
  for i = 1, a do
     for j = 1, b do

         local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
         balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
         balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
         balloonText3 = display.newText(hiragana_array[x+3], 495, 225)
         balloonText1:setFillColor( 1,1, 0 )
         balloonText2:setFillColor( 1,1, 0 )
         balloonText3:setFillColor( 1,1, 0 )
         balloon.name = 'balloon'
         physics.addBody(balloon)
         balloon.bodyType = 'static'
         table.insert(balloons, balloon)
         end
    end
    target.text = #balloons
end
--ball collides with balloon
function ballCollision(e)
   if (e.other.name == 'balloon') then
        e.target:removeSelf()
        e.other:removeSelf()
        audio.play(pop)
        score.text = score.text + 50
        score.anchorX = 0
        score.anchorY = 0
        score.x = 200
        score.y = 50
        target.text = target.text - 1
    end
点赞
用户1870706
用户1870706

你需要跟踪 balloonText。我建议做的是将文本的显示对象添加到气球对象中:

     local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
     balloon.balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
     balloon.balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
     balloon.balloonText3 = display.newText(hiragana_array[x+3], 495, 225)

然后在碰撞处理程序中:

    e.target:removeSelf()
    e.other.balloonText1:removeSelf()
    e.other.balloonText2:removeSelf()
    e.other.balloonText3:removeSelf()
    e.other:removeSelf()

或类似的东西。

2014-03-16 00:44:47
用户2409015
用户2409015

你能否为文本对象分配 name 属性。然后在碰撞函数中打印 e.other.name 并查看输出结果。我想 e.other 可能会得到 nil,这就是为什么它没有从显示组中删除的原因。

2014-03-20 06:30:21