当对象被击中时,删除相应的文本。

我的问题是,我可以让文字消失,但我想让文字在所在的气球被击中时消失。例如,我击中了包含“balloonText”的第一个气球,代码如何确定它是被击中的包含“balloonText”的气球?由于生成的所有气球都被称为“balloon”,因此如何区分?

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))
         balloonText = display.newText(hiragana_array[x+1], 495, 125)
         balloonTextt = display.newText(hiragana_array[x+2], 495, 175)
         balloonTexttt = display.newText(hiragana_array[x+3], 495, 225)
         balloonText:setFillColor( 1,1, 0 )
         balloonTextt:setFillColor( 1,1, 0 )
         balloonTexttt:setFillColor( 1,1, 0 )
         balloon.name = 'balloon'
         physics.addBody(balloon)
         balloon.bodyType = 'static'
         table.insert(balloons, balloon)
         end
    end
    target.text = #balloons
end
-- 球碰撞气球
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
点赞
用户869951
用户869951

e.other(或 e.object2)或 e.target(或 e.object1)上使用 removeSelf

function ballCollision(e)
   if (e.other.name == 'balloonText') then
        e.target:removeSelf()
        e.other:removeSelf()
   end
end

你不应该将 e.target 等设置为 nil 或其他值。如果你使用了 balloon:addEventListener('collision', ballCollision),你可能需要使用 e.object1e.object2

2014-03-13 21:46:28
用户3131449
用户3131449

使用此示例代码

    local myImage = display.newImageRect(view,"image_name.png",50,50 )
    myImage.x = _x
    myImage.y = _y
    physics.addBody( myImage )
    myImage.collision = onCollision
    myImage:addEventListener(   "collision", myImage );
    myImage.myName="balloonText";

function onCollision(e)
   if (e.other1.name == 'balloonText') then
        e.other1:removeSelf()
        e.other2:removeSelf()
   end
end

此代码是用于图像的,你也可以用它来处理文本。

2014-03-14 07:21:18