From 623b28b04540128ec71487a7d64a8a75a9cba2c4 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 30 Nov 2018 21:31:53 +0100 Subject: [PATCH] added functions that change the states instead of directly change the state --- Calculator.qml | 54 ++++++++++--------- CalculatorGameOverPage.qml | 10 ---- ...rMainPage.qml => CalculatorRunningPage.qml | 2 +- CalculatorStartPage.qml | 2 +- qml.qrc | 2 +- 5 files changed, 31 insertions(+), 39 deletions(-) rename CalculatorMainPage.qml => CalculatorRunningPage.qml (98%) diff --git a/Calculator.qml b/Calculator.qml index fe22c59..81e1019 100644 --- a/Calculator.qml +++ b/Calculator.qml @@ -19,7 +19,7 @@ Item { signal pageOpened() onPageOpened: { - calculator.state = "starting" + calculator.start() } states: [ @@ -35,11 +35,11 @@ Item { name: "running" PropertyChanges { target: calculatorStack - currPage: calculatorMainPageComp + currPage: calculatorRunningPageComp } }, State { - name: "ending" + name: "gameOver" PropertyChanges { target: calculatorStack currPage: calculatorGameOverPageComp @@ -68,8 +68,8 @@ Item { } Component { - id: calculatorMainPageComp - CalculatorMainPage { + id: calculatorRunningPageComp + CalculatorRunningPage { } } @@ -106,6 +106,29 @@ Item { ---------functions--------- -------------------------*/ + function start() { + calculator.reset() + calculator.state = "starting" + } + + function run() { + calculator.nextNumber() + calculator.state = "running" + } + + function gameOver() { + calculator.state = "gameOver" + } + + function reset() { + calculator.sum = 0 + calculator.actualNumCount = 0 + calculator.endPageLabelText = "You Lose!" + calculator.endPageVisibility = false + calculator.lastNum = 0 + calculator.nextNum = 0 + } + function nextNumber(){ var randNum = 0 var range = calculator.max - calculator.min @@ -118,21 +141,6 @@ Item { actualNumCount += 1 } - function start(min, max) { - calculator.reset() - calculator.nextNumber() - calculator.state = "running" - } - - function reset() { - calculator.sum = 0 - calculator.actualNumCount = 0 - calculator.endPageLabelText = "You Lose!" - calculator.endPageVisibility = false - calculator.lastNum = 0 - calculator.nextNum = 0 - } - function checkSum(sumInputText) { if (sumInputText !== "") { if (calculator.sum === parseInt(sumInputText)) { @@ -141,10 +149,4 @@ Item { calculator.endPageVisibility = true } } - - function setState(state) { - if (state === "starting") { - } - calculator.state = state - } } diff --git a/CalculatorGameOverPage.qml b/CalculatorGameOverPage.qml index 3206edc..89e1942 100644 --- a/CalculatorGameOverPage.qml +++ b/CalculatorGameOverPage.qml @@ -89,16 +89,6 @@ Page { height: root.width * 0.2 width: height text: "start" - onClicked: { - calculator.state = "starting" - } - } - - RoundButton { - id: restart - height: root.width * 0.2 - width: height - text: "restart" onClicked: { calculator.start() } diff --git a/CalculatorMainPage.qml b/CalculatorRunningPage.qml similarity index 98% rename from CalculatorMainPage.qml rename to CalculatorRunningPage.qml index 2bdeb3e..02a1de0 100644 --- a/CalculatorMainPage.qml +++ b/CalculatorRunningPage.qml @@ -96,7 +96,7 @@ Page { tick.start() } else { - calculator.state = "ending" + calculator.state = "gameOver" } } diff --git a/CalculatorStartPage.qml b/CalculatorStartPage.qml index 4b00ee9..8ed92cd 100644 --- a/CalculatorStartPage.qml +++ b/CalculatorStartPage.qml @@ -118,7 +118,7 @@ Page { } function start() { root.updateVars() - calculator.start() + calculator.run() } function updateVars() { diff --git a/qml.qrc b/qml.qrc index 253605d..94dc434 100644 --- a/qml.qrc +++ b/qml.qrc @@ -3,7 +3,7 @@ main.qml CalenderPage.qml StartPage.qml - CalculatorMainPage.qml + CalculatorRunningPage.qml CalculatorStartPage.qml CalculatorGameOverPage.qml Calculator.qml