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

Juicy Squash - Match-3 Game

 import Felgo 4.0
 import QtQuick 2.0
 import "scenes"

 GameWindow {
   id: gameWindow

   // You get free licenseKeys from https://felgo.com/licenseKey
   // With a licenseKey you can:
   //  * Publish your games & apps for the app stores
   //  * Remove the Felgo Splash Screen or set a custom one (available with the Pro Licenses)
   //  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
   //licenseKey: "<generate one from https://felgo.com/licenseKey>"

   // the size of the Window can be changed at runtime by pressing Ctrl (or Cmd on Mac) + the number keys 1-8
   // the content of the logical scene size (480x320 for landscape mode by default) gets scaled to the window size based on the scaleMode
   // you can set this size to any resolution you would like your project to start with, most of the times the one of your main target device
   // this resolution is for iPhone 4 & iPhone 4S
   screenWidth: 640
   screenHeight: 960

   // custom font loading of ttf fonts
   FontLoader {
     id: gameFont
     source: Qt.resolvedUrl("../assets/fonts/akaDylan Plain.ttf")
   }

   // add google analytics plugin
   GoogleAnalytics {
     id: gAnalytics
   }

   // loading screen
   Rectangle {
     id: loadScreen
     width: gameWindow.width
     height: gameWindow.height
     color: "white"
     z: 1

     opacity: 0
     enabled: opacity == 1 ? true : false
     visible: opacity > 0 ? true : false

     // signal when load screen is fully visible
     signal fullyVisible()

     // background and text
     Text {
       // set font
       font.family: gameFont.name
       font.pixelSize: gameWindow.width / 640 * 24
       color: "red"
       text: "Loading ..."
       anchors.centerIn: parent
     }

     // animate loading screen
     Behavior on opacity {
       PropertyAnimation {
         duration: 300
         onRunningChanged: {
           if(!running && opacity == 1)
             loadScreen.fullyVisible()
         }
       }
     }
   }

   // add spashscreen scene (first scene to show)
   SplashScreenScene {
     id: splashScene
     onSplashScreenFinished: gameWindow.state = "game" // show game screen after splash screen
   }

   // use loader to load game-scene when necessary
   Loader {
     id: gameSceneLoader
     onLoaded: loadScreen.opacity = 0
   }

   // use loader to load gamenetwork-scene when necessary
   Loader {
     id: gameNetworkSceneLoader
     onLoaded: loadScreen.opacity = 0
   }

   // set start state
   state: "splash"

   states: [
     State {
       name: "splash"
       PropertyChanges {target: splashScene; opacity: 1}
       PropertyChanges {target: gameWindow; activeScene: splashScene}
     },
     State {
       name: "gameNetwork"
       StateChangeScript {
         script: {
           showGameNetworkScene()
           gAnalytics.logScreen("GameNetwork")
         }
       }
     },
     State {
       name: "game"
       StateChangeScript {
         script: {
           showGameScene()
           gAnalytics.logScreen("Game")
         }
       }
     }
   ]

   // show game scene
   function showGameScene() {
     // if game scene not loaded -> load first
     if(gameSceneLoader.item === null) {
       gameSceneLoader.loaded.connect(showGameScene)
       loadGameScene()
       return
     }

     // scene is definitely loaded at this point
     // show game scene
     gameWindow.activeScene = gameSceneLoader.item
     gameSceneLoader.item.opacity = 1

     // hide gamenetwork scene
     if(gameNetworkSceneLoader.item !== null)
       gameNetworkSceneLoader.item.opacity = 0
   }

   // show gamenetwork scene
   function showGameNetworkScene() {
     // if gamenetwork scene not loaded -> load first
     if(gameNetworkSceneLoader.item === null) {
       gameNetworkSceneLoader.loaded.connect(showGameNetworkScene)
       loadGameNetworkScene()
       return
     }

     // scene is definitely loaded at this point
     // show gamenetwork scene
     gameWindow.activeScene = gameNetworkSceneLoader.item
     gameNetworkSceneLoader.item.opacity = 1

     // hide game scene
     if(gameSceneLoader.item !== null)
       gameSceneLoader.item.opacity = 0
   }

   // show loading screen and start loading game scene
   function loadGameScene() {
     loadScreen.opacity = 1
     loadScreen.fullyVisible.connect(fetchAndInitializeGameScene)
   }

   // show loading screen and start loading gamenetwork scene
   function loadGameNetworkScene() {
     loadScreen.opacity = 1
     loadScreen.fullyVisible.connect(fetchAndInitializeGameNetworkScene)
   }

   // fetch game scene from qml and connect signals
   function fetchAndInitializeGameScene() {
     gameSceneLoader.source = "scenes/GameScene.qml"
     gameSceneLoader.item.highscoreClicked.connect(onHighscoreClicked)
     gameSceneLoader.item.reportScore.connect(onReportScore)

     loadScreen.fullyVisible.disconnect(fetchAndInitializeGameScene)
   }

   // fetch gamenetwork scene from qml and connect signals
   function fetchAndInitializeGameNetworkScene() {
     gameNetworkSceneLoader.source = "scenes/GameNetworkScene.qml"
     gameNetworkSceneLoader.item.backButtonPressed.connect(onBackButtonPressed)

     loadScreen.fullyVisible.disconnect(fetchAndInitializeGameNetworkScene)
   }

   // report score event
   function onReportScore(score) {
     if(gameNetworkSceneLoader.item === null) {
       // if not loaded -> load gamenetwork -> then report score again
       gameNetworkSceneLoader.loaded.connect(function() { onReportScore(score)} )
       loadGameNetworkScene()
       return
     }

     gameNetworkSceneLoader.item.gameNetwork.reportScore(score)
   }

   // switch state events
   function onHighscoreClicked() {
     gameWindow.state = "gameNetwork"
   }

   function onBackButtonPressed() {
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded