added functions that change the states instead of directly change the state

This commit is contained in:
Max 2018-11-30 21:31:53 +01:00
parent 534832f546
commit 623b28b045
5 changed files with 31 additions and 39 deletions

View file

@ -19,7 +19,7 @@ Item {
signal pageOpened() signal pageOpened()
onPageOpened: { onPageOpened: {
calculator.state = "starting" calculator.start()
} }
states: [ states: [
@ -35,11 +35,11 @@ Item {
name: "running" name: "running"
PropertyChanges { PropertyChanges {
target: calculatorStack target: calculatorStack
currPage: calculatorMainPageComp currPage: calculatorRunningPageComp
} }
}, },
State { State {
name: "ending" name: "gameOver"
PropertyChanges { PropertyChanges {
target: calculatorStack target: calculatorStack
currPage: calculatorGameOverPageComp currPage: calculatorGameOverPageComp
@ -68,8 +68,8 @@ Item {
} }
Component { Component {
id: calculatorMainPageComp id: calculatorRunningPageComp
CalculatorMainPage { CalculatorRunningPage {
} }
} }
@ -106,6 +106,29 @@ Item {
---------functions--------- ---------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(){ function nextNumber(){
var randNum = 0 var randNum = 0
var range = calculator.max - calculator.min var range = calculator.max - calculator.min
@ -118,21 +141,6 @@ Item {
actualNumCount += 1 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) { function checkSum(sumInputText) {
if (sumInputText !== "") { if (sumInputText !== "") {
if (calculator.sum === parseInt(sumInputText)) { if (calculator.sum === parseInt(sumInputText)) {
@ -141,10 +149,4 @@ Item {
calculator.endPageVisibility = true calculator.endPageVisibility = true
} }
} }
function setState(state) {
if (state === "starting") {
}
calculator.state = state
}
} }

View file

@ -89,16 +89,6 @@ Page {
height: root.width * 0.2 height: root.width * 0.2
width: height width: height
text: "start" text: "start"
onClicked: {
calculator.state = "starting"
}
}
RoundButton {
id: restart
height: root.width * 0.2
width: height
text: "restart"
onClicked: { onClicked: {
calculator.start() calculator.start()
} }

View file

@ -96,7 +96,7 @@ Page {
tick.start() tick.start()
} }
else { else {
calculator.state = "ending" calculator.state = "gameOver"
} }
} }

View file

@ -118,7 +118,7 @@ Page {
} }
function start() { function start() {
root.updateVars() root.updateVars()
calculator.start() calculator.run()
} }
function updateVars() { function updateVars() {

View file

@ -3,7 +3,7 @@
<file>main.qml</file> <file>main.qml</file>
<file>CalenderPage.qml</file> <file>CalenderPage.qml</file>
<file>StartPage.qml</file> <file>StartPage.qml</file>
<file>CalculatorMainPage.qml</file> <file>CalculatorRunningPage.qml</file>
<file>CalculatorStartPage.qml</file> <file>CalculatorStartPage.qml</file>
<file>CalculatorGameOverPage.qml</file> <file>CalculatorGameOverPage.qml</file>
<file>Calculator.qml</file> <file>Calculator.qml</file>