如何在按下按钮时让我的脚本重置其分数?

时间:2014-09-20 23:17:30

标签: lua corona reset

我需要一些帮助,了解一旦点击" 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 )

1 个答案:

答案 0 :(得分:2)

要使图像成为按钮,您需要添加一个响应触摸或点击事件的事件监听器。 见http://docs.coronalabs.com/api/event/touch/index.html

或者您使用小部件库,它可以使用空白按钮背景,并且只为每个按钮设置标签,当您包含其他语言的翻译时,这将非常方便。 见http://docs.coronalabs.com/api/library/widget/newButton.html

在我的游戏中,我有一个功能gameInit(),用于设置洞游戏和所有变量。当游戏开始时以及当玩家想要重置时,该函数被调用,因为它在写入旧变量时。 (还有其他技术,取决于游戏的复杂程度,如果你想在下次玩家开始游戏时存储游戏设置)