Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › BoxCollider to RetryScene

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #9145

    Michael

    Hi,

     

    I’m having problems with a game I’m working on. I want the game to change to a retry scene when my player collides with an obstacle. I’ve created a retryscene.qml and referenced it in my main file and my state changer but I don’t know what instructions I should add to the boxcollider to make the change happen. My BoxCollider looks like this.

      BoxCollider {
        anchors.fill: target1
        bodyType: Body.Static
        collisionTestingOnlyMode: true
        fixture.onBeginContact: {
           scene.stopGame()
           // Play a collision sound
           collisionSound1.play();
           // Go to retry Scene
           
        }
    }

    Does anyone know what to write that will make this happen? 99% sure I’ve done everything else correctly, just don’t know what the command should be.

     

    Alternatively, if someone knows a better way to do this please let me know.

    #9146

    Alex
    Felgo Team

    Hi Michael,

    there are a number of ways to handle the scene changes, e.g. state machines that we use in some of our examples. I’d recommend having a look at this tutorial, and in your case especially this part can be of most interest.
    (anyway it’s a short tutorial so take some minutes and read the whole thing! 🙂

    If there are still questions left after, I’m happy to help you!

    Cheers,
    Alex

    #9147

    Michael

    Hi Alex,

    Thanks for your answer. I looked at the tutorial again and ultimately decided to change my approach. Now I’m using the state machine so that in my game scene I have this function

    function stopGame() {
                scene.gameState = "gameOver"
                    }

    and then in my state machine I’ve said

    // menuScene is our first scene, so set the state to menu initially
        state: "menu"
        activeScene: menuScene
    
        // state machine, takes care reversing the PropertyChanges when changing the state, like changing the opacity back to 0
        states: [
            State {
                name: "menu"
                PropertyChanges {target: menuScene; opacity: 1}
                PropertyChanges {target: window; activeScene: menuScene}
            },
            State {
                name: "selectLevel"
                PropertyChanges {target: selectLevelScene; opacity: 1}
                PropertyChanges {target: window; activeScene: selectLevelScene}
            },
            State {
                name: "credits"
                PropertyChanges {target: creditsScene; opacity: 1}
                PropertyChanges {target: window; activeScene: creditsScene}
            },
            State {
                name: "game"
                PropertyChanges {target: gameScene; opacity: 1}
                PropertyChanges {target: window; activeScene: gameScene}
            },
            State {
                name: "gameOver"
                PropertyChanges {target: retryScene; opacity: 1}
                PropertyChanges {target: window; activeScene: retryScene}
            }
        ]

    I also have added a RetryScene.qml

     

    import Felgo 3.0
    import QtQuick 2.0
    import "../common"
    
    SceneBase {
        id:retryScene
    
        // background
        Rectangle {
            anchors.fill: parent.gameWindowAnchorItem
            color: "darkred"
        }
    
        // back button to leave scene
        MenuButton {
            text: "Back"
            // anchor the button to the gameWindowAnchorItem to be on the edge of the screen on any device
            anchors.right: retryScene.gameWindowAnchorItem.right
            anchors.rightMargin: 10
            anchors.top: retryScene.gameWindowAnchorItem.top
            anchors.topMargin: 10
            onClicked: backButtonPressed()
        }
    
        // credits
        Text {
            text: "Game Over"
            color: "white"
            anchors.centerIn: parent
        }
    }

    and listed it in the gamewindow of my main.qml

     

    RetryScene {
            id: retryScene
            onBackButtonPressed: window.state = "selectLevel"
        }

    But when the stopGame function is called, nothing happens. Any ideas as to what’s happening?

    #9148

    Alex
    Felgo Team

    The state machine listens on the state property of the item where it’s defined. So if it is defined in your GameWindow, with the id window, then calling scene.gameState = “gameOver” will not have any effect, instead it should be window.state = “gameOver”, just like you are doing it in the onBackButtonPressed signal handler of your RetryScene (window.state = “selectLevel”).

    Cheers,
    Alex

    • This reply was modified 8 years, 11 months ago by  Alex.
    • This reply was modified 8 years, 11 months ago by  Alex.
    #9151

    Michael

    Thanks Alex,

    I knew it was something simple that I was overlooking. Works perfectly now.

    Michael.

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded