Lua 如何让我的脚本在按下按钮时重置其分数?
我需要一些帮助,了解如何在我点击“reset.png”按钮时将numMiss、numHit和numPercent的分数重置为0,同时这样做也会重新开始游戏 另外,如果我的代码中有任何修改,请告诉我 这是我到目前为止对代码的了解Lua 如何让我的脚本在按下按钮时重置其分数?,lua,coronasdk,reset,Lua,Coronasdk,Reset,我需要一些帮助,了解如何在我点击“reset.png”按钮时将numMiss、numHit和numPercent的分数重置为0,同时这样做也会重新开始游戏 另外,如果我的代码中有任何修改,请告诉我 这是我到目前为止对代码的了解 --width and height WIDTH = display.contentWidth --320 HEIGHT = display.contentHeight --480 --display background local p = display.newIm
--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 )
——宽度和高度
宽度=显示。内容宽度--320
高度=display.contentHeight--480
--显示背景
本地p=display.newImageRect(“park.png”,500570)
p、 x=宽度/2
p、 y=高度/2
--显示弹跳狗
局部半径=5
本地d=display.newImageRect(“dogeball.png”,70,70)
d、 x=50
d、 y=100
--展示款待
本地t=display.newImageRect(“treat.png”,50,50)
t、 x=245
t、 y=数学随机(高度)
--显示“重置”按钮
本地r=display.newImageRect(“reset.png”,100100)
r、 x=280
r、 y=480
--重力和反弹的起始值(将更改)
局部重力=0.3
局部反弹=0.75
--向下力
局部速度=0
--告诉分数在为真时重置
本地重置=错误
--显示点击数
局部numHit=0
--显示未命中的数量
本地numMiss=0
--获取百分比分数
本地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)
函数enterFrame()
d、 y=d.y+速度
速度=速度+重力
本地命中率=半径*8——调整此值以调整游戏难度
如果math.abs(t.x-d.x)要使图像成为一个按钮,您需要添加一个响应触摸或轻触事件的事件侦听器。
看
或者您可以使用widget库,该库允许您使用空白按钮背景并仅为每个按钮设置标签,当您包含其他语言的翻译时,这将非常方便。
看
在我的游戏中,我有一个函数gameInit(),用于设置洞游戏和所有变量。该函数在游戏开始时调用,也在玩家希望在重写旧变量时进行重置时调用。(还有其他技术,具体取决于游戏的复杂性,例如,如果您希望在玩家下次启动游戏时存储游戏设置)