1

石やリンゴなどのオブジェクトが絶えず落下しているので、触れたときにリンゴを消して、触れたリンゴの数を取得する必要があります。 :

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

何かを非表示にするには、そのアルファを 0 に設定するだけです。これにより、100% 透明になります。 apple.alpha = 0;

ドキュメント: http://docs.coronalabs.com/api/type/DisplayObject/alpha.html

于 2013-12-29T07:28:33.383 に答える