Chris
Chris

Reputation: 3

How would I get my script to reset its score upon button press?

I need some help understanding how would I reset the score of numMiss, numHit, and numPercent back to 0 as soon as I tap the "reset.png" button and while doing so will also start the game from the beginning again.

Also, let me know if there are any corrections to be made within my code.

Heres what I have of the code so far

--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 )

Upvotes: 0

Views: 160

Answers (1)

Dev Ze
Dev Ze

Reputation: 116

To make your image a button you need to add an event listener that responds to touch or tap events. see http://docs.coronalabs.com/api/event/touch/index.html

Or you use the widget library which gives you the possibility to use a blank button background and set only the label for each button what will be very handy when you include translations for other languages. see http://docs.coronalabs.com/api/library/widget/newButton.html

In my game I have a function gameInit() that sets the hole game and all variables. This function is called when the game starts and also when the player wants to do a reset as it over writes the old variables. (there are other technics, depending on the complexity of your game and if you for example want to store the game settings for the next time the player starts the game)

Upvotes: 2

Related Questions