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

Forums

OverviewFelgo 3 Support (Qt 5) › Differents scenes to my game

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #16192

    Patricia

    Hi, Im developing a  simply game, but now Im having a problem with my the scenes.

    when i run my program I got a error: Cannot assign to non-existent property “activeScene”

    Also, I don’t know if I’m doing okey. I let you the part of my program related with this part.

    GameScene—->

    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
      // score
      property int score: 0
      // countdown shown at level start
      property int countdown: 0
      // flag indicating if game is running
      property bool gameRunning: countdown == 0
    
    
      // set the name of the current level, this will cause the Loader to load the corresponding level
      function setLevel(fileName) {
        activeLevelFileName = fileName
      }
    
    
      // background
      Rectangle {
        anchors.fill: parent.gameWindowAnchorItem
        color: "#dd94da"
      }
    
    
      // 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.right: gameScene.gameWindowAnchorItem.right
        anchors.rightMargin: 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
          // restarts the countdown
          countdown = 3
        }
      }
    
    
      // 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++
          }
        }
      }
    
    
      // name of the current level
      Text {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: gameScene.gameWindowAnchorItem.top
        anchors.topMargin: 30
        color: "white"
        font.pixelSize: 40
        text: score
      }
    
    
      // text displaying either the countdown or "tap!"
      Text {
        anchors.centerIn: parent
        color: "white"
        font.pixelSize: countdown > 0 ? 160 : 18
        text: countdown > 0 ? countdown : "tap!"
      }
    
    
      // if the countdown is greater than 0, this timer is triggered every second, decreasing the countdown (until it hits 0 again)
      Timer {
        repeat: true
        running: countdown > 0
        onTriggered: {
          countdown--
        }
      }
    }
    
    MenuButton------>
    
    Rectangle {
     id: button
     // this will be the default size, it is same size as the contained text + some padding
     width: buttonText.width+ paddingHorizontal*2
     height: buttonText.height+ paddingVertical*2
    
     color: "#e9e9e9"
     // round edges
     radius: 10
    
     // the horizontal margin from the Text element to the Rectangle at both the left and the right side.
     property int paddingHorizontal: 10
     // the vertical margin from the Text element to the Rectangle at both the top and the bottom side.
     property int paddingVertical: 5
    
     // access the text of the Text component
     property alias text: buttonText.text
    
     // this handler is called when the button is clicked.
     signal clicked
    
     Text {
     id: buttonText
     anchors.centerIn: parent
     font.pixelSize: 18
     color: "black"
     }
    
     MouseArea {
     id: mouseArea
     anchors.fill: parent
     hoverEnabled: true
     onClicked: button.clicked()
     onPressed: button.opacity = 0.5
     onReleased: button.opacity = 1
     }
    }
    
    
    MenuScene-----> SceneBase {
      id: menuScene
    
    
      // signal indicating that the selectLevelScene should be displayed
      signal selectLevelPressed
      // signal indicating that the creditsScene should be displayed
      signal creditsPressed
    
    
      // background
      Rectangle {
        anchors.fill: parent.gameWindowAnchorItem
        color: "#47688e"
      }
    
    
      // the "logo"
      Text {
        anchors.horizontalCenter: parent.horizontalCenter
        y: 30
        font.pixelSize: 30
        color: "#e9e9e9"
        text: "MultiSceneMultiLevel"
      }
    
    
      // menu
      Column {
        anchors.centerIn: parent
        spacing: 10
        MenuButton {
          text: "Levels"
          onClicked: selectLevelPressed()
        }
    
    
    
    
      }
    
    
    }
    SceneBase---->
    
    Scene {
     id: sceneBase
    
     // by default, set the opacity to 0 - this is changed from the main.qml with PropertyChanges
     opacity: 0
     // we set the visible property to false if opacity is 0 because the renderer skips invisible items, this is an performance improvement
     visible: opacity > 0
     // if the scene is invisible, we disable it. In Qt 5, components are also enabled if they are invisible. This means any MouseArea in the Scene would still be active even we hide the Scene, since we do not want this to happen, we disable the Scene (and therefore also its children) if it is hidden
     enabled: visible
    
     // every change in opacity will be done with an animation
     Behavior on opacity {
     NumberAnimation {property: "opacity"; easing.type: Easing.InOutQuad}
     }
    
    }
    
    
    SelectScene----->
    SceneBase {
      id: selectLevelScene
    
    
      // signal indicating that a level has been selected
      signal levelPressed(string selectedLevel)
    
    
      // background
      Rectangle {
        anchors.fill: parent.gameWindowAnchorItem
        color: "#ece468"
      }
    
    
      // 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: selectLevelScene.gameWindowAnchorItem.right
        anchors.rightMargin: 10
        anchors.top: selectLevelScene.gameWindowAnchorItem.top
        anchors.topMargin: 10
        onClicked: backButtonPressed()
      }
    
    
      // levels to be selected
      Grid {
        anchors.centerIn: parent
        spacing: 10
        columns: 5
        MenuButton {
          text: "1"
          width: 50
          height: 50
          onClicked: {
            levelPressed("main.qml")
          }
        }
    
    
      }
    }
    main.qml------>  
    GameWindow {
        id: gameWindow
        screenHeight: 768
        screenWidth: 1024
    //definición de la pantalla de juego como pantalla completa.
    
    
    
    
    
    
    
    
        EntityManager{
            id:entityManager
            entityContainer: window
        }
    MenuScene {
              id: menuScene
              // listen to the button signals of the scene and change the state according to it
              onSelectLevelPressed: window.state = "selectLevel"
              onCreditsPressed: window.state = "credits"
              // the menu scene is our start scene, so if back is pressed there we ask the user if he wants to quit the application
              onBackButtonPressed: {
                nativeUtils.displayMessageBox(qsTr("Really quit the game?"), "", 2);
              }
              // listen to the return value of the MessageBox
              Connections {
                target: nativeUtils
                onMessageBoxFinished: {
                  // only quit, if the activeScene is menuScene - the messageBox might also get opened from other scenes in your code
                  if(accepted && window.activeScene === menuScene)
                    Qt.quit()
                }
              }
            }
    
    
            // scene for selecting levels
            SelectScene {
              id: selectLevelScene
              onLevelPressed: {
                // selectedLevel is the parameter of the levelPressed signal
                gameScene.setLevel(selectedLevel)
                window.state = "game"
    
    
              }
              onBackButtonPressed: window.state = "menu"
            }
    
    
            // credits scene
    
    
    
    
            // game scene to play a level
            GameScene {
              id: gameScene
              onBackButtonPressed: window.state = "selectLevel"
            }
    
    
            // 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: "game"
                PropertyChanges {target: gameScene; opacity: 1}
                PropertyChanges {target: window; activeScene: gameScene}
              }
            ]
    
    
    
    
    
    
    
    
    
    
    }
    
    Thank you for the help!
    
    
    #16193

    Patricia

    I forgot also put Level.qml file—–>

    Item {
    // this will be displayed in the GameScene
    property string levelName
    // this is emitted whenever the rectangle has been tapped successfully, the GameScene will listen to this signal and increase the score
    signal rectanglePressed
    }

    #16194

    Patricia

    Also, in main.qml file I have a part that puts

    Level{..

    ..

    ..}

    where inside there are things I want to do my program

    #16196

    Günther
    Felgo Team

    Hi Patricia,

    the activeScene property is only available for the GameWindow type. It is used to automatically set the QML Item Focus to the active scene when switching scenes. Your GameWindow uses the id: gameWindow, but in the code for onMessageBoxFinished you are referring to window.activeScene. Please make sure that you use the correct id for GameWindow when addressing it from somewhere else within your code.

    For a general guide about multiple scenes and levels, you can have a look at our multiple scenes and levels tutorial.

    Best,
    Günther

    #16273

    Patricia

    Hi, I was checkeing the “multiple scenes and levels tutorial” now when I press level1 it’s in on my game but I have 3 problems.. I hope you can solve them.

    1. The first one is that I get a error in main.qml in the line where put:  onMessageBoxFinished it say (invalid property name) (I changed the things that you say me)
    2. I also get this error in GameScene.qml: qrc:/GameScene.qml:72:3: QML Connections: Cannot assign to non-existent property “onRectanglePressed” I dont know why because I font have any phrase like that.
    3. And the last one is: when I press the level1, the program go in but the dimension of my game is too big. I dont know why because I dont change the dimension that I had before to divide my game in teh differents scenes. Maybe you can help me.

    I’m going to put my code again, meybe for you is easier.

    main.qml——>

    import QtQuick 2.6

    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import QtQml 2.2
    import Felgo 3.0
    import QtMultimedia 5.0
    
    
    
    
    
    
    
    
    GameWindow {
        id: window
        screenHeight: 768
        screenWidth: 1024
    ButtonVPlay {
            text: "Salir"
            onClicked: gameWindow.close()
    
    
      }
    
    
      AudioManager{
          id:audioManager
    
    
      }
    
    
      BackgroundMusic{
          id: backgroundMusic
          source: "sound/backgroundAudio.wav"
          autoPlay: true
          loops: Audio.Infinite
          volume:0.1
      }
    
    
      //poder mutear el sonido de fondo
    
    
      AudioButton{
          id:audioButton
          anchors.top: window.top
          anchors.right: window.right
          state: "stopState"
          function stopMusic(){
              audioButton.state = "playState"
              backgroundMusic.stop()
              console.log("stopState")
              opacity= 0.5
          }
    
    
          function playMusic(){
              audioButton.state = "stopState"
              backgroundMusic.play()
              console.log("playState")
              opacity = 1
          }
    
    
          onClicked: {
              audioButton.state === "stopState" ?
                          audioButton.stopMusic() : audioButton.playMusic()
          }
      }
    
    
    
    
            MenuScene {
              id: menuScene
              // listen to the button signals of the scene and change the state according to it
              onSelectLevelPressed: window.state = "selectLevel"
              onBackButtonPressed: {
                nativeUtils.displayMessageBox(qsTr("Really quit the game?"), "", 2);
              }
              // listen to the return value of the MessageBox
              Connections {
                target: nativeUtils
                onMessageBoxFinished: {
                  // only quit, if the activeScene is menuScene - the messageBox might also get opened from other scenes in your code
                  if(accepted && window.activeScene === menuScene)
                    Qt.quit()
                }
              }
            }
    
    
            // scene for selecting levels
            SelectScene {
              id: selectLevelScene
              onLevelPressed: {
                // selectedLevel is the parameter of the levelPressed signal
                gameScene.setLevel(selectedLevel)
                window.state = "game"
    
    
              }
              onBackButtonPressed: window.state = "menu"
            }
            // game scene to play a level
            GameScene {
              id: gameScene
              onBackButtonPressed: window.state = "selectLevel"
            }
    
    
            // 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: "game"
                PropertyChanges {target: gameScene; opacity: 1}
                PropertyChanges {target: window; activeScene: gameScene}
              }
            ]
    }
    GameScene----->
    import Felgo 3.0
    import QtQuick 2.0
    
    
     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
      // score
      property int score: 0
      // countdown shown at level start
      property int countdown: 0
      // flag indicating if game is running
      property bool gameRunning: countdown == 0
    
    
      // set the name of the current level, this will cause the Loader to load the corresponding level
      function setLevel(fileName) {
        activeLevelFileName = fileName
      }
    
    
      // background
      Rectangle {
        anchors.fill: parent.gameWindowAnchorItem
        color: "#dd94da"
      }
    
    
      // 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.right: gameScene.gameWindowAnchorItem.right
        anchors.rightMargin: 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
          // restarts the countdown
          countdown = 3
        }
      }
    
    
      // we connect the gameScene to the loaded level
      Connections {
        // only connect if a level is loaded, to prevent errors
        target: activeLevel !== undefined ? activeLevel : null
    
    
      }
    
    
      // name of the current level
      Text {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: gameScene.gameWindowAnchorItem.top
        anchors.topMargin: 30
        color: "white"
        font.pixelSize: 40
        text: score
      }
    
    
      // text displaying either the countdown or "tap!"
      Text {
        anchors.centerIn: parent
        color: "white"
        font.pixelSize: countdown > 0 ? 160 : 18
        text: countdown > 0 ? countdown : "tap!"
      }
    
    
      // if the countdown is greater than 0, this timer is triggered every second, decreasing the countdown (until it hits 0 again)
      Timer {
        repeat: true
        running: countdown > 0
        onTriggered: {
          countdown--
        }
      }
    }
    
    
    Level------------->
    import Felgo 3.0
    import QtQuick 2.0
    
    
    
    
    Item {
      // this will be displayed in the GameScene
      property string levelName
    
    
    }
    MenuButton--->
    import QtQuick 2.0
    
    
    Rectangle {
      id: button
      // this will be the default size, it is same size as the contained text + some padding
      width: buttonText.width+ paddingHorizontal*2
      height: buttonText.height+ paddingVertical*2
    
    
      color: "#e9e9e9"
      // round edges
      radius: 10
    
    
      // the horizontal margin from the Text element to the Rectangle at both the left and the right side.
      property int paddingHorizontal: 10
      // the vertical margin from the Text element to the Rectangle at both the top and the bottom side.
      property int paddingVertical: 5
    
    
      // access the text of the Text component
      property alias text: buttonText.text
    
    
      // this handler is called when the button is clicked.
      signal clicked
    
    
      Text {
        id: buttonText
        anchors.centerIn: parent
        font.pixelSize: 18
        color: "black"
      }
    
    
      MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        onClicked: button.clicked()
        onPressed: button.opacity = 0.5
        onReleased: button.opacity = 1
      }
    }
    MenuScene---->
    
    import QtQuick 2.0
    SceneBase {
      id: menuScene
    
    
      // signal indicating that the selectLevelScene should be displayed
      signal selectLevelPressed
    //  signal selectSound
    
    
      // background
      Rectangle {
        anchors.fill: parent.gameWindowAnchorItem
        Image{
            source: "img/background.png"
        }
      }
    
    
      Text {
        anchors.horizontalCenter: parent.horizontalCenter
        y: 30
        font.pixelSize: 30
        color: "#e9e9e9"
        text: "Bird Game"
      }
    
    
      // menu
      Column {
        anchors.centerIn: parent
        spacing: 10
        MenuButton {
          text: "Niveles"
          onClicked: selectLevelPressed()
        }
      }
    SceneBase----->
    import Felgo 3.0
    import QtQuick 2.0
    
    
    Scene {
      id: sceneBase
    
    
      // by default, set the opacity to 0 - this is changed from the main.qml with PropertyChanges
      opacity: 0
      // we set the visible property to false if opacity is 0 because the renderer skips invisible items, this is an performance improvement
      visible: opacity > 0
      // if the scene is invisible, we disable it. In Qt 5, components are also enabled if they are invisible. This means any MouseArea in the Scene would still be active even we hide the Scene, since we do not want this to happen, we disable the Scene (and therefore also its children) if it is hidden
      enabled: visible
    
    
      // every change in opacity will be done with an animation
      Behavior on opacity {
        NumberAnimation {property: "opacity"; easing.type: Easing.InOutQuad}
      }
    }
    
    
    
    
    }
    SelectScene----->
    import Felgo 3.0
    import QtQuick 2.0
    
    
    SceneBase {
      id: selectLevelScene
    
    
      // signal indicating that a level has been selected
      signal levelPressed(string selectedLevel)
    
    
      // background
      Rectangle {
        anchors.fill: parent.gameWindowAnchorItem
        color: "#ece468"
      }
    
    
      // 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: selectLevelScene.gameWindowAnchorItem.right
        anchors.rightMargin: 10
        anchors.top: selectLevelScene.gameWindowAnchorItem.top
        anchors.topMargin: 10
        onClicked: backButtonPressed()
      }
    
    
      // levels to be selected
      Grid {
        anchors.centerIn: parent
        spacing: 10
        columns: 5
        MenuButton {
          text: "1"
          width: 50
          height: 50
          onClicked: {
            levelPressed("Level1.qml")
          }
        }
    
    
      }
    }
    
    
    Thanks for everything and sorry for the long commentary
    
    

     

    #16318

    Günther
    Felgo Team

    Hi Patricia,

    I followed up regarding your issues via mail!

    Best,
    Günther from Felgo

Viewing 6 posts - 1 through 6 (of 6 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