Forums

OverviewV-Play 1 SupportFlappy bird with a pause › Reply To: Flappy bird with a pause

OverviewV-Play 1 SupportFlappy bird with a pause › Reply To: Flappy bird with a pause
#7014

David

Welcome in our forums armotechsolutions,

the easiest way to add a pause function would be to add a pause state to the States in the GameScene.qml which stops the game and brings up the waitToPlay screen (It might be better to use a different screen than waitToPlay, but you have to create a new one for this).

    State {
      name: "pause"
      PropertyChanges {target: waitToPlay; opacity: 1}
      PropertyChanges {target: physicsWorld; gravity: Qt.point(0,0)}

      StateChangeScript {
        script: {
          stopGame()
        }
      }
    }

 

You need to stop the player from it’s movement so you have to add according functions stop and resume to the Player.qml which stop the movement and store the velocity to use it when the player should resume to action.

  property variant linearVelocityBeforeStop: Qt.point(0,0)

  function stop() {
    linearVelocityBeforeStop = collider.body.linearVelocity
    collider.body.linearVelocity = Qt.point(0,0)
    spriteSequence.running = false
  }

  function resume() {
    collider.body.linearVelocity = linearVelocityBeforeStop
    spriteSequence.running = true
  }

 

You need to modify the startGame and stopGame functions in GameScene.qml and add the new player functions to stop and resume the player.

  function startGame() {
    level.start()
    player.resume()
  }

  function stopGame() {
    level.stop()
    player.stop()
  }

 

The pipes won’t work correctly so you have to change some parts in Pipes.qml. The stopped property was introduced there and the code should look like this in the end.

property bool stopped: false

  function reset() {
    var animationIsRunning = animation.running
    if(animationIsRunning)
      stop()

    pipeElement.x = scene.gameWindowAnchorItem.width+pipeElement.width/2
    pipeElement.y = utils.generateRandomValueBetween(-variationDistance, variationDistance)-scene.height/3
    stopped = false

    if(animationIsRunning)
      animation.start()
  }

  function start() {
    if(stopped) {
      animation.start()
    } else {
      delayTimer.restart()
    }
  }

  function stop() {
    animation.stop()
    delayTimer.stop()
    stopped = true
  }

 

Afterwards you need a function which can toggle the states which can also be added the the GameScene.qml.

  function togglePauseState() {
    if(scene.state === "pause") {
      scene.state = "play"
    } else {
      scene.state = "pause"
    }
  }

 

Now you have to toggle the scene state by adding following lines to your FlappyBirdMain.qml.

  onApplicationResumed: {
    gameScene.togglePauseState()
  }
  onApplicationPaused: {
    gameScene.togglePauseState()
  }

 

That’s it, you need to improve the one or other animation and add other guides for a smooth experience but I assume you can do this by your own.

Cheers,

David

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support