击中对象时删除相应的文本

时间:2014-03-12 09:20:42

标签: lua corona

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

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

2 个答案:

答案 0 :(得分:1)

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

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

你不应该将e.target等设置为nil或任何东西。如果您使用了balloon:addEventListener(' collision',ballCollision),则可能必须使用e.object1和e.object2。

答案 1 :(得分:0)

使用此示例代码

  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

此代码用于图像,您也可以将其用于文本