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!