2014-09-20 15 views
0

我需要一些幫助,瞭解如何在我點擊「reset.png」按鈕時將numMiss,numHit和numPercent的分數重置爲0,同時這樣做也會再次從頭開始遊戲。如何讓我的腳本在按下按鈕時重置其分數?

另外,讓我知道是否有任何更正要在我的代碼中進行。

繼承人什麼我的代碼至今

--width and height 
WIDTH = display.contentWidth --320 
HEIGHT = display.contentHeight --480 

--display background 
local p = display.newImageRect("park.png" ,500, 570) 
p.x = WIDTH/2 
p.y = HEIGHT/2 

--display bouncing dog 
local RADIUS = 5 
local d = display.newImageRect("dogeball.png", 70, 70) 
d.x = 50 
d.y = 100 

--display treat 
local t = display.newImageRect("treat.png", 50, 50) 
t.x = 245 
t.y = math.random(HEIGHT) 

--displays the reset button 
local r = display.newImageRect("reset.png", 100,100) 
r.x = 280 
r.y = 480 


--starting value of gravity and bounce(will change) 
local GRAVITY = 0.3 
local BOUNCE = 0.75 

--downward force 
local velocity = 0 

--Tells the score to reset when true 
local reset = false 

--shows number of hits 
local numHit = 0 

--shows number of misses 
local numMiss = 0 

--Gets Percentage score 
local numPercent = 0 


--make hits and misses display 
scoreHits = display.newText("Hits = " .. numHit, WIDTH/7, 1, native.systemFont, 18) 
scoreMisses = display.newText("Misses = " .. numMiss, WIDTH/2.1, 1, native.systemFont, 18) 
scorePercent = display.newText("Hit % = " .. numPercent, WIDTH/1.2, 1, native.systemFont, 18) 

function enterFrame() 


    d.y = d.y + velocity 

    velocity = velocity + GRAVITY 

    local HIT_SLOP = RADIUS * 8 -- Adjust this to adjust game difficulty 
    if math.abs(t.x - d.x) <= HIT_SLOP 
     and math.abs(t.y - d.y) <= HIT_SLOP then 

     numHit = numHit + 1 
     scoreHits.text = "Hits = " .. numHit 

     --count 1 hit once dog and treat hit eachother 
     if (t.x - d.x) <= HIT_SLOP and (t.y - d.y) <= HIT_SLOP then 
      t.x = 400 --resets treat postioning 
      t.y = math.random(HEIGHT) --gives treat a random y coordinate 
     end 
    end 

    --puts the barrier at the bottom of the screen and tells dog to bounce from there 
    if (d.y > HEIGHT) then 

     d.y = HEIGHT 
     velocity = -velocity * BOUNCE 
    end 
    t.x = t.x - 5 --speed treat goes 
    if t.x < -350 then--position of the treat 
    t.x = 400 
    scoreMisses.text = "Misses = " .. numMiss 
     else if t.x < -100 then 
     t.y = math.random(HEIGHT) --random height after treat goes past dog 
      else if t.x < -99 then 
      numMiss = numMiss + 1 --calculates misses when goes past screen 
      scoreMisses.text = "Misses = " .. numMiss 
      end 
     end 
    end 

    --calculate percentage hits 
    numPercent = 100 * numHit/(numHit + numMiss) 
    scorePercent.text = "Hit % = " .. math.round(numPercent) --prints and rounds percentage 


    function tapped(event) --when tapped on reset, score gets reset 
     --reset function goes here 
     end 

    end 

    r:addEventListener("tap", tapped) 
end 



    function touched(event) 
    -- print(event.phase) 
    if event.phase == "began" then 
     velocity = velocity - 6 -- thrusts dog 
    end 
    return true 
end 





Runtime:addEventListener("enterFrame" , enterFrame) 
Runtime:addEventListener("touch", touched) 

回答

2

爲了讓你的形象,你需要添加一個事件偵聽器響應觸摸或輕敲事件的按鈕。 請參閱http://docs.coronalabs.com/api/event/touch/index.html

或者您使用小部件庫,它使您可以使用空白按鈕背景併爲每個按鈕設置僅標籤,當您包含其他語言的翻譯時,這將非常方便。 請參閱http://docs.coronalabs.com/api/library/widget/newButton.html

在我的遊戲中,我有一個函數gameInit(),它設置空洞遊戲和所有變量。這個函數在遊戲開始時被調用,並且當玩家想要進行重置時,它會重寫舊的變量。 (還有其他技術,具體取決於遊戲的複雜程度,例如,如果您希望下次玩家開始遊戲時存儲遊戲設置)

相關問題