当按钮按下时,如何让我的脚本重置它的分数?

我需要帮助理解如何在点击“reset.png”按钮时将numMiss、numHit和numPercent的分数重置为0,并在重新开始游戏时执行此操作。

另外,请告诉我是否需要更正我的代码。

目前为止,我所拥有的代码:

-- 宽度和高度
WIDTH = display.contentWidth --320
HEIGHT = display.contentHeight --480

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

-- 显示跳跃的狗
local RADIUS = 5
local d = display.newImageRect("dogeball.png", 70, 70)
d.x = 50
d.y = 100

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

--显示重置按钮
local r = display.newImageRect("reset.png", 100,100)
r.x = 280
r.y = 480

-- 引力和弹跳的初始值(将更改)
local GRAVITY = 0.3
local BOUNCE = 0.75

-- 向下的力
local velocity = 0

-- 当为真时,告诉得分重设
local reset = false

-- 显示命中的次数
local numHit = 0

-- 显示未命中的次数
local numMiss = 0

-- 获取百分比得分
local numPercent = 0

-- 使得命中和未命中计分的信息显示出来
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  -- 调整此参数以调整游戏难度
    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

         -- 当狗和零食相撞时,将命中次数计为1
         if (t.x - d.x) <= HIT_SLOP and (t.y - d.y) <= HIT_SLOP then
            t.x = 400 -- 重置零食的位置
            t.y = math.random(HEIGHT) -- 为零食确定随机的y坐标
        end
    end

    --将障碍物放到屏幕底部,并告诉狗从那里弹起
    if (d.y > HEIGHT) then

        d.y = HEIGHT
        velocity = -velocity * BOUNCE
    end
    t.x = t.x - 5 --零食的速度
    if t.x < -350 then--零食的位置
    t.x = 400
    scoreMisses.text = "Misses = " .. numMiss
        else if t.x < -100 then
        t.y = math.random(HEIGHT) --零食经过狗后随机为其确定高度
            else if t.x < -99 then
            numMiss = numMiss + 1 --计算零食通过表层上未命中的次数
            scoreMisses.text = "Misses = " .. numMiss
            end
        end
    end

    --计算百分数
    numPercent = 100 * numHit / (numHit + numMiss)
    scorePercent.text = "Hit % = " .. math.round(numPercent) --打印并四舍五入百分数

    function tapped(event) -- 点击重置时,得分将被重置
        -- 重置函数在此
        end

    end

    r:addEventListener( "tap", tapped )
end

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

Runtime:addEventListener( "enterFrame" , enterFrame )
Runtime:addEventListener( "touch", touched )
点赞
用户3038364
用户3038364

为了将您的图像变为按钮,您需要添加响应触摸或点击事件的事件侦听器。 请查看http://docs.coronalabs.com/api/event/touch/index.html

或者您可以使用小部件库,该库为您提供了使用空白按钮背景并仅为每个按钮设置标签的可能性,这在包含其他语言的翻译时非常方便。 请查看http://docs.coronalabs.com/api/library/widget/newButton.html

在我的游戏中,我有一个名为gameInit()的函数,它设置了整个游戏和所有变量。游戏开始时会调用此函数,当玩家想要重置时也会调用它,因为它会覆盖旧变量。(根据您的游戏的复杂性以及是否想要存储游戏设置以供下一次玩家开始游戏,还有其他技术可用)

2014-09-21 14:16:37