1

我有像石头和苹果这样的物体不断下落,现在我需要在触摸时消失苹果并计算我触摸的苹果数量......请帮助......我对 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

4 回答 4

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 是您触摸的苹果的计数。

于 2012-08-23T07:00:12.953 回答
2

使用 new-text 在你想要的地方显示你的计数:

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 )
于 2012-08-23T10:27:18.567 回答
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 )
于 2012-08-23T10:36:25.170 回答
0

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

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

于 2013-12-29T07:28:33.383 回答