Forums

OverviewV-Play 2 Support (Qt 5) › Reset problems

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #9152

    Michael

    Hi guys,

    Having trouble getting my level to reset. Just going to explain the situation first and then I’ll give you whatever code you’d like to see.

    I start my game app, click through the menu to a select level scene, pick level 1 and play. When my player dies, I go to a game over scene. From here I can return to the select level scene and start the level again. However, the level doesn’t reset itself when I die. It used to, as I built the game using the Flappy Bird tutorial. I’d click the MouseArea and the level would reset. But now that I go to the game over screen instead, I don’t where to call the reset function from in my code. My gamescene looks like this.

    import VPlay 2.0
    import QtQuick 2.0
    import "../common"
    import "../Entities"
    
    SceneBase {
        id:gameScene
        // the filename of the current level gets stored here, it is used for loading the
        property string activeLevelFileName
        // the currently loaded level gets stored here
        property variant activeLevel
        // set the name of the current level, this will cause the Loader to load the corresponding level
        function setLevel(fileName) {
            activeLevelFileName = fileName
        }
    
        sceneAlignmentY: "bottom"
    
        EntityManager {
            id: entityManager
        }
    
       Scene {
           id: scene
    
       property string gameState: "wait"
    
       function startGame() {
                 scene.gameState = "play"
                      }
    
       function stopGame() {
                window.state  = "gameOver"
                    }
    
       function reset() {
            scene.gameState = "wait"
            pipe1.x = 400
            pipe1.y = 30+Math.random()*200
            target1.x = 210
            target1.y = 190
            target2.x = 420
            target2.y = 260
            target3.x = 330
            target3.y = 280
            player.x = 160
            player.y = 180
            scene.score = 0
                       }
    
       property int score: 0
    
       // background
       Image {
            id: bg
                source: "qrc:/assets/bg.png"
                anchors.horizontalCenter: scene.horizontalCenter
                anchors.bottom: scene.gameWindowAnchorItem.bottom
              }
    
       Border {
                id: border
                anchors.top: scene.gameWindowAnchorItem.top
                          }
    
       Pipe {
             id: pipe1
             x: 480
             y: 30+Math.random()*200
                        }
    
       Target1 {
              id: target1
              x: 210
              y: 190
                      }
    
       Target2 {
             id: target2
             x:420
             y:260
                }
    
       Target3 {
                id: target3
                x: 330
                y:280
            }
    
       Ground {
              anchors.horizontalCenter: scene.horizontalCenter
              anchors.bottom: scene.gameWindowAnchorItem.bottom
                   }
    
       Player {
                id: player
                x: 160
                y: 180
    
              }
    
       PhysicsWorld {
              debugDrawVisible: false // set this to false to hide the physics overlay
              gravity.y: scene.gameState != "wait" ? -31 : 0 // -9.81 would be earth-like gravity, so this one will be pretty strong
            }
    
       MouseArea {
            anchors.fill: scene.gameWindowAnchorItem
            onPressed: {
              if(scene.gameState == "wait") {
                scene.startGame()
                player.push()
              } else if(scene.gameState == "play") {
                player.push()
              }
            }
          }
    
       SoundEffectVPlay {
              id: pointSound
              source: "qrc:/assets/sfx_point.wav"
              volume: 1.0
            }
    
       SoundEffectVPlay {
              id: collisionSound1
              source: "qrc:/assets/sfx_hit.wav"
              volume: 1.0
            }
    
       SoundEffectVPlay {
              id: collisionSound2
              source: "qrc:/assets/sfx_hit.wav"
              volume: 1.0
            }
    
       SoundEffectVPlay {
              id: collisionSound3
              source: "qrc:/assets/sfx_die.wav"
              volume: 1.0
            }
    
       SoundEffectVPlay {
              id: collisionSound4
              source: "qrc:/assets/sfx_die.wav"
              volume: 1.0
            }
    
       Text {
              text: scene.score
              color: "white"
              anchors.horizontalCenter: scene.horizontalCenter
              y: 30
              font.pixelSize: 30
            }
    
    
        // back button to leave scene
        MenuButton {
            text: "Back to menu"
            // anchor the button to the gameWindowAnchorItem to be on the edge of the screen on any device
            anchors.left: gameScene.gameWindowAnchorItem.left
            anchors.leftMargin: 10
            anchors.top: gameScene.gameWindowAnchorItem.top
            anchors.topMargin: 10
            onClicked: {
                backButtonPressed()
                activeLevel = undefined
                activeLevelFileName = ""
            }
        }
    }
    
        // name of the current level
        Text {
            anchors.left: gameScene.gameWindowAnchorItem.left
            anchors.leftMargin: 10
            anchors.top: gameScene.gameWindowAnchorItem.top
            anchors.topMargin: 10
            color: "white"
            font.pixelSize: 20
            text: activeLevel !== undefined ? activeLevel.levelName : ""
        }
    
        // load levels at runtime
        Loader {
            id: loader
            source: activeLevelFileName != "" ? "../levels/" + activeLevelFileName : ""
            onLoaded: {
                // reset the score
                score > 0
                // since we did not define a width and height in the level item itself, we are doing it here
                item.width = gameScene.width
                item.height = gameScene.height
                // store the loaded level as activeLevel for easier access
                activeLevel = item
                //start level from new
                if (state.gameOver){
                    scene.reset ()}
                }
          }
    
    
        // we connect the gameScene to the loaded level
        Connections {
            // only connect if a level is loaded, to prevent errors
            target: activeLevel !== undefined ? activeLevel : null
            // increase the score when the rectangle is clicked
            onRectanglePressed: {
                // only increase score when game is running
                if(gameRunning) {
                    score++
                }
                if (state.gameOver){
                    scene.reset ()}
            }
        }
    }
    

    I’ve tried to call the reset function from the loader and the connections but this doesn’t work. As usual, I’m sure there’s a very straightforward explanation but my brain is fried at the moment. If anyone could help that would be great. I experimented with breaking up the reset function and applying it to all the entities separately but that didn’t work and I’ve tried to call it from a few other places. If it’s possible to fix this some other way I would love to hear about it as I’ve wasted too much time on this already.

    All the best,

    Michael

Viewing 1 post (of 1 total)

RSS feed for this thread

You must be logged in to reply to this topic.

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