CORONA:如何在触摸或点击时消除物体?

时间:2012-08-23 06:11:23

标签: corona

我有像石头和苹果这样的物体不断下降,现在我需要在触摸时消灭苹果并得到我触摸的苹果数量....请帮助......我对CORONA来说是全新的...... 。 这是我的代码:

function newApples()    
rand = math.random( 100 )

if (rand < 60) then
    j = display.newImage("s1.png");
    j.x = 60 + math.random( 160 )
    j.y = -100
    physics.addBody( j, { density=0.9, friction=0.3, bounce=0.3} )
elseif (rand < 80) then
    j = display.newImage("s2.png");
    j.x = 60 + math.random( 160 )
    j.y = -100
    physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
else
    apple = display.newImage("apple1.png");
    apple.x = 60 + math.random( 160 )
    apple.y = -100
    physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )
    apple.name = "apple"
end 
end
local dropApples = timer.performWithDelay( 500, newApples, -1 )

function onTouch(event)

  print("this")
  event:removeSelf()

end

apple:addEventListener("tap",onTouch)

4 个答案:

答案 0 :(得分:5)

我认为此代码可以帮助您:

local physics = require("physics")
physics.start()
local appletouchcount=0
function newApples()    
rand = math.random( 100 )

if (rand < 60) then
j = display.newImage("s1.png");
j.x = 60 + math.random( 160 )
j.y = -100

elseif (rand < 80) then
j = display.newImage("s2.png");
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
else
apple = display.newImage("apple1.png");
apple.x = 60 + math.random( 160 )
apple.y = -100
physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )
apple.name = "apple"


function onTouch(event)
appletouchcount= appletouchcount+1
 print("this")
event.target:removeSelf()
print("total"..appletouchcount)
end

apple:addEventListener("touch",onTouch)
end 
end

local dropApples = timer.performWithDelay( 500, newApples, -1 )

appletouchcount是你触摸过的苹果计数。

答案 1 :(得分:2)

使用新文本显示您想要的人数:

local physics = require("physics")
physics.start()
local appletouchcount=0
local text = display.newText("Total ", 0, 0, native.systemFont, 16)
text.x=150;text.y=50
function newApples()    
rand = math.random( 100 )

if (rand < 60) then
    j = display.newImage("s1.png");
    j.x = 60 + math.random( 160 )
    j.y = -100

elseif (rand < 80) then
    j = display.newImage("s2.png");
    j.x = 60 + math.random( 160 )
    j.y = -100
    physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
else
    apple = display.newImage("apple1.png");
    apple.x = 60 + math.random( 160 )
    apple.y = -100
    physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )
    apple.name = "apple"


function onTouch(event)
if event.phase=="ended" then
text.text="Total "..appletouchcount
    appletouchcount= appletouchcount+1
  print("this")
  event.target:removeSelf()
print("total"..appletouchcount)

end
print(appletouchcount)
end
apple:addEventListener("touch",onTouch)
end 

end


local dropApples = timer.performWithDelay( 500, newApples, -1 )

答案 2 :(得分:1)

在我想说一件事之前,即删除不必要的对象,它会导致内存问题使应用程序变慢。但即使lua语言有内存管理。 我认为以下代码可以达到你的例外

local physics = require("physics")
physics.start()
local appletouchcount=0;local count={total1=0,total=0,touch=0,loss=0}
local total=display.newText("Total:0 \n AppleTotal:0 \n AppleGot:0 \n AppleLoss:0",display.contentCenterX*0.25, 60, native.systemFont, 26)




    local collisionListener=function(self,event)
print(event.other.type)
if(event.phase=="began")then
    if(event.other.type=="apple")then
        count.loss=count.loss+1
        event.other:removeSelf();event.other=nil
    else
        event.other:removeSelf();event.other=nil
    end
end
    end


    function newApples(event)    
     count.total1=event.count
   total.text="Total:"..count.total1.." \n AppleTotal:"..count.total.." \n AppleGot:"..count.touch.." \n AppleLoss:"..count.loss
   rand = math.random( 100 )

   if (rand < 60) then
   j = display.newCircle(0,0,40)--display.newImage("s1.png");
   j.x = 60 + math.random( 160 )
   j.y = -100
   j.type="other"
   physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
   elseif (rand < 80) then
   j = display.newCircle(0,0,40)--display.newImage("s2.png");
  j.x = 60 + math.random( 160 )
  j.y = -100
  j.type="other"
  physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
 else
 apple =display.newCircle(0,0,70) --display.newImage("apple1.png");
  apple.x = 60 + math.random( 160 )
 apple.y = -100
apple.type="apple"
apple:setFillColor(255,0,0)
 count.total= count.total+1
 physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )
 apple.name = "apple"


function onTouch(event)
count.touch=count.touch+1
total.text="Total:"..count.total1.." \n AppleTotal:"..count.total.." \n AppleGot:"..count.touch.." \n AppleLoss:"..count.loss

 event.target:removeSelf()
 print("total"..appletouchcount)
 end

 apple:addEventListener("touch",onTouch)
 end 
 end

 botwall=display.newRect(0,display.contentHeight,display.contentWidth,10)
 botwall:setFillColor(22,125,185,255)
 botwall.type="botwall"
 botwall.collision=collisionListener
 physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
 botwall:addEventListener("collision",botwall)

  local dropApples = timer.performWithDelay( 500, newApples, -1 )

答案 3 :(得分:0)

要使某些东西不可见,你只需将其alpha设置为0.这使它100%透明。     apple.alpha = 0;

文档:http://docs.coronalabs.com/api/type/DisplayObject/alpha.html