Page 1 of 1

making a pause function

Posted: Wed Oct 02, 2019 9:58 pm
by Accelerator
I am trying to make a replica of Flappy Bird. One part of the assignment is to make a pause function. I have implemented one but it does not work in the game. Here is the code.

Code: Select all

PauseState = Class{__includes = BaseState}

function PauseState:init()
    self.timer = 0
    self.enterParams = {}
end

function PauseState:update(dt)
    self.timer = self.timer + dt
    
    if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
        self.enterParams = {
            ['bird'] = self.bird,
            ['pipePairs'] = self.pipePairs,
            ['timer'] = self.timer,
            ['score'] = self.score
        }
        self.timer = self.timer + 10

        gStateMachine:change('play', self.enterParams)   
    end
end

function PauseState:enter(enterParams)

    scrolling = false
    self.bird = enterParams.bird
    self.pipePairs = enterParams.pipePairs
    self.timer = enterParams.timer
    self.score = enterParams.score
end

function PauseState:render()
    love.graphics.setFont(flappyFont)
    love.graphics.printf('The game paused!', 0, 64, VIRTUAL_WIDTH, 'center')

    love.graphics.setFont(mediumFont)
    love.graphics.printf('Press Enter to continue', 0, 100, VIRTUAL_WIDTH, 'center')
    love.graphics.print(self.timer .. tostring(self.score), 8, 38)
end

Re: making a pause function

Posted: Wed Oct 02, 2019 10:23 pm
by JJSax
WIthout really going through it, most likely if you made it as below it would stop it. You may have to do the same with the line below that adds 10 to self.timer too

Code: Select all

self.timer = self.timer + dt * 0
edit: 0 can be whatever variable obviously to speed/slow time.

Re: making a pause function

Posted: Wed Oct 02, 2019 11:22 pm
by raidho36
Pausing basically means not updating the gamestate. So you can put your gamestate update function in an if-case that checks against the pause flag.