MainScene.qml Example File

demos/ChickenOutbreak2/qml/MainScene.qml
 import QtQuick 2.0
 import Felgo 3.0

 // is shown at game start and shows the maximum highscore and a button for starting the game
 SceneBase {
   id: mainScene

   // only if this is set to true, the exit dialog should quit the app
   property bool exitDialogShown: false
   property bool vplayLinkShown: false

   onBackButtonPressed: {
     audioManager.play(audioManager.idMENUCLICK)
     exitDialogShown = true
     // instead of immediately shutting down the app, ask the user if he really wants to exit the app with a native dialog
     nativeUtils.displayMessageBox(qsTr("Really quit the game?"), "", 2);
   }

   Connections {
     // nativeUtils should only be connected, when this is the active scene
     target: window.activeScene === mainScene ? nativeUtils : null
     onMessageBoxFinished: {
       audioManager.play(audioManager.idMENUCLICK)
       console.debug("the user confirmed the Ok/Cancel dialog with:", accepted)
       if(accepted && exitDialogShown) {
         entityManager.removeAllPooledEntities()
         Qt.quit()
       } else if(accepted && vplayLinkShown) {
         flurry.logEvent("MainScene.Show.VPlayWeb")
         nativeUtils.openUrl("https://felgo.com/showcases/?utm_medium=game&utm_source=chicken_outbreak2&utm_campaign=chicken_outbreak2#chicken_outbreak2");
       }

       // set it to false again
       exitDialogShown = false
       vplayLinkShown = false
     }
   }

   MultiResolutionImage {
     source: "../assets/background-wood2.png"
     // Warning - Do not use anchoring but defined positioning for MultiResolutionImages when used in Scene directly used after heavy loading (e.g. NetworkService). Framedelay in anchoring positioning with about 2-3 Frames.
     //anchors.centerIn: parent
     x: parent.gameWindowAnchorItem.x
     y: parent.gameWindowAnchorItem.y
   }

   MainSceneDecoration {}

   Image {
     source: "../assets/sign-chickenoutbreak2.png"
     anchors.horizontalCenter: parent.horizontalCenter
     y: -118
     width: 300
     height: 270
     z: 20
   }

   property int signRotationPrefix: accelerometer.active ? (accelerometer.reading.x >= 0 ? 1 : -1) : 0
   property real signRotation: accelerometer.active ? (Math.abs(accelerometer.reading.x) > 1 ? (accelerometer.reading.x - signRotationPrefix) * 2 : 0) : 0

 //  Image {
 //    id: gameCenterButton
 //    y:115
 //    z:19
 //    anchors.right: gameNetworkSign.left
 //    anchors.leftMargin: 10
 //    rotation: signRotation

 //    source: "../assets/sign_small.png"
 //    // the image size is bigger (for hd2 image), so only a single image no multiresimage can be used
 //    // this scene is not performance sensitive anyway!
 //    fillMode: Image.PreserveAspectFit
 //    height: 80

 //    // this button opens the gamecenter leaderboards - only show it if the gamecenter is available (so iOS only)
 //    // still show the Game Center button, as on iOS it is also very popular!
 //    // on iOS VPGN and GameCenter both can be used together
 //    visible: gameCenter.authenticated

 //    Image {
 //      source: "../assets/game_center.png"
 //      anchors.horizontalCenter: parent.horizontalCenter
 //      anchors.bottom: parent.bottom
 //      anchors.bottomMargin: 10
 //      width: 30
 //      height: 30
 //    }

 //    MouseArea {
 //      anchors.fill: parent
 //      onClicked: {
 //        parent.scale = 1.0
 //        audioManager.play(audioManager.idMENUCLICK)
 //          flurry.logEvent("GameCenter.Show")
 //          gameCenter.showLeaderboard();
 //      }

 //      onPressed: {
 //        parent.scale = 0.85
 //      }
 //      onReleased: {
 //        parent.scale = 1.0
 //      }
 //      onCanceled: {
 //        parent.scale = 1.0
 //      }
 //    }
 //  }

 //  Image {
 //    id: gameNetworkButton
 //    y:115
 //    z:19
 //    anchors.left: gameNetworkSign.right
 //    anchors.rightMargin: 10
 //    rotation: signRotation

 //    source: "../assets/sign_small.png"
 //    // the image size is bigger (for hd2 image), so only a single image no multiresimage can be used
 //    // this scene is not performance sensitive anyway!
 //    fillMode: Image.PreserveAspectFit
 //    height: 80

 //    // this button opens the gamecenter leaderboards - only show it if the gamecenter is available (so iOS only)
 //    // still show the Game Center button, as on iOS it is also very popular!
 //    // on iOS VPGN and GameCenter both can be used together
 //    visible: gameCenter.authenticated

 //    Image {
 //      source: "../assets/vpgn.png"
 //      anchors.horizontalCenter: parent.horizontalCenter
 //      anchors.bottom: parent.bottom
 //      anchors.bottomMargin: 10
 //      width: 30
 //      height: 30
 //    }

 //    MouseArea {
 //      anchors.fill: parent
 //      onClicked: {
 //        parent.scale = 1.0
 //        audioManager.play(audioManager.idMENUCLICK)
 //        window.state = "gameNetwork"
 //        // this would not need to be called, then no request is sent when showing it every time
 //        gameNetwork.showLeaderboard()
 //      }

 //      onPressed: {
 //        parent.scale = 0.85
 //      }
 //      onReleased: {
 //        parent.scale = 1.0
 //      }
 //      onCanceled: {
 //        parent.scale = 1.0
 //      }
 //    }
 //  }

   Image {
     id: gameNetworkSign
     source: "../assets/sign.png"
     anchors.horizontalCenter: parent.horizontalCenter
     width: 150
     height: 120
     mirror: true
     y:80
     z:19
     rotation: signRotation

     Item {
       y: 82
       anchors.horizontalCenter: parent.horizontalCenter
       width:  highscoreText.width+scoreImage.width+highscoreTextScore.width
       rotation: 2

       MenuText {
         id: highscoreText
         text: qsTr("Highscore:")
         font.pixelSize: 20
       }
       Image {
         id: scoreImage
         source: "../assets/gamecenter.png"
         y: 1
         width: 20
         height: 20
         anchors.left: highscoreText.right
         anchors.leftMargin: 1
       }

       MenuText {
         id: highscoreTextScore
         text: ""+ maximumHighscore
         font.pixelSize: 20
         anchors.left: scoreImage.right
         anchors.leftMargin: 5
       }
     }
     MouseArea {
       anchors.bottom: parent.bottom
       width: parent.width
       height: parent.height/2
       onClicked: {
         parent.scale = 1.0
         audioManager.play(audioManager.idMENUCLICK)
         window.state = "gameNetwork"
         // this would not need to be called, then no request is sent when showing it every time
         gameNetwork.showLeaderboard()
       }

       onPressed: {
         parent.scale = 0.85
       }
       onReleased: {
         parent.scale = 1.0
       }
       onCanceled: {
         parent.scale = 1.0
       }
     }
   }

   Image {
     source: "../assets/sign.png"
     anchors.horizontalCenter: parent.horizontalCenter
     width: 100
     height: 85
     y:158
     z:18
     rotation: signRotation
     MenuText {
       id: shopShield
       anchors.left: parent.left
       anchors.leftMargin: 20
       y: 55
       text: qsTr("Shop ")
       font.pixelSize: 20
       rotation: -2
     }

     CoinDisplay {
       y: 55
       anchors.left: shopShield.right
       rotation: -2
     }

     MouseArea {
       anchors.bottom: parent.bottom
       width: parent.width
       height: parent.height/2
       onClicked: {
         parent.scale = 1.0
         audioManager.play(audioManager.idMENUCLICK)
         window.state = "shop"
       }

       onPressed: {
         parent.scale = 0.85
       }
       onReleased: {
         parent.scale = 1.0
       }
       onCanceled: {
         parent.scale = 1.0
       }
     }
   }

   MenuButton {
     text: qsTr("Play")
     onClicked: {
       audioManager.play(audioManager.idMENUCLICK)
       if(firstAppStart) window.state = "tutorial"
       else window.state = "game"
     }
     anchors.horizontalCenter: parent.horizontalCenter
     textSize: 27
     width: 170 * 0.8
     height: 60 * 0.8
     y: 290
   }

   Column {
     anchors.horizontalCenter: parent.horizontalCenter
     y: 345
     spacing: 5

     MenuButton {
       text: qsTr("Credits")
       anchors.horizontalCenter: parent.horizontalCenter

       width: 170 * 0.6
       height: 60 * 0.6

       onClicked: {
         audioManager.play(audioManager.idMENUCLICK)
         window.state = "credits";
       }
     }
     Item {
       width: 90
       height: 60 * 0.6
       anchors.horizontalCenter: parent.horizontalCenter
       MusicButton {
       }

       SoundButton {
         x: 50
       }
     }

     Item {
       width: 90
       height: 60 * 0.6
       anchors.horizontalCenter: parent.horizontalCenter

       SocialMediaButton {
         id: facebook
         source: "../assets/facebook.png"
         onClicked: {
           audioManager.play(audioManager.idMENUCLICK)
           nativeUtils.openUrl("https://apps.facebook.com/chicken_outbreak_two")
          }
       }

       SocialMediaButton {
         id: twitter
         x: 50
         source: "../assets/twitter.png"
         onClicked: {
           audioManager.play(audioManager.idMENUCLICK)
           nativeUtils.openUrl("https://twitter.com/ChickenOutbreak")
         }
       }
     }
   }
   DailyBonus {
     id: dailyBonus
     anchors.horizontalCenter: parent.horizontalCenter
     y: 248
   }

   HourlyBonus {
     id: hourlyBonus
     anchors.horizontalCenter: parent.horizontalCenter
     y: 248
   }

   Image {
     id: logo
     anchors.right: mainScene.gameWindowAnchorItem.right
     anchors.rightMargin: 10
     anchors.bottom: mainScene.gameWindowAnchorItem.bottom
     anchors.bottomMargin: 10
     source: "../assets/img/felgo-logo.png"
     // the image size is bigger (for hd2 image), so only a single image no multiresimage can be used
     // this scene is not performance sensitive anyway!
     fillMode: Image.PreserveAspectFit
     height: 55

     MouseArea {
       anchors.fill: parent
       onClicked: {
         audioManager.play(audioManager.idMENUCLICK)
         vplayLinkShown = true
         flurry.logEvent("MainScene.ShowDialog.VPlayWeb")
         nativeUtils.displayMessageBox(qsTr("Felgo"), qsTr("This game is built with Felgo. The source code is available in the free Felgo SDK - so you can build your own Chicken Outbreak 2 in minutes! Visit Felgo.net now?"), 2)
       }
     }

     SequentialAnimation {
       running: true
       loops: -1
       NumberAnimation { target: logo; property: "opacity"; to: 0.1; duration: 1200 }
       NumberAnimation { target: logo; property: "opacity"; to: 1; duration: 1200 }
     }
   }

   function enterScene() {
     opacity = 1
     var today = new Date()
     var todayTruncated = new Date(today.getTime()-today.getHours()*3600000-today.getMinutes()*60000-today.getSeconds()*1000-today.getMilliseconds())
     console.debug("Today truncated is " + todayTruncated.getTime().toString())
     var storedLastLoginMS = settings.getValue("lastLogin")
     console.debug("Read from database " + storedLastLoginMS)
     if(!storedLastLoginMS) {
       settings.setValue("lastLogin",todayTruncated.getTime().toString())
       dailyBonus.giveBonus()
       console.debug("Set lastLogin to" + settings.getValue("lastLogin"))
     } else {
       if(todayTruncated.getTime().toString() > storedLastLoginMS) {
         console.debug("Last login was a day ago, give player bonus")
         settings.setValue("lastLogin",todayTruncated.getTime().toString())
         dailyBonus.giveBonus()
       }
     }
     lockTimer.restart()
   }

   // use a timer to lock the screen for a second, that users can not press buttons before the scene is loaded. E.g. when pressing back in store and it is pressed a few times, the press would also have affect on the twitter/facebook buttons.
   Timer {
     id: lockTimer
     interval: 500
     onTriggered: {
       lockArea.enabled = false
     }
   }

   MouseArea {
     id: lockArea
     anchors.fill: parent
     enabled: true
     onClicked: {

     }
   }
 }

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