diff options
author | Hugo Parente Lima <hugo.pl@gmail.com> | 2011-02-25 19:11:25 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:54:00 -0300 |
commit | 610fc659e802c05e14ec4e905f06a19b5306ce2a (patch) | |
tree | aa3b48047c06e78dad419ee5607a4a1017cdfd00 /doc/tutorials/qmladvancedtutorial/samegame/samegame2/samegame.js | |
parent | a7263b23e001b5281119edd3c5d9f49513503afb (diff) |
Added some QML tutorials to the documentation.
Diffstat (limited to 'doc/tutorials/qmladvancedtutorial/samegame/samegame2/samegame.js')
-rw-r--r-- | doc/tutorials/qmladvancedtutorial/samegame/samegame2/samegame.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/doc/tutorials/qmladvancedtutorial/samegame/samegame2/samegame.js b/doc/tutorials/qmladvancedtutorial/samegame/samegame2/samegame.js new file mode 100644 index 000000000..c749dc17b --- /dev/null +++ b/doc/tutorials/qmladvancedtutorial/samegame/samegame2/samegame.js @@ -0,0 +1,63 @@ +//![0] +var blockSize = 40; +var maxColumn = 10; +var maxRow = 15; +var maxIndex = maxColumn * maxRow; +var board = new Array(maxIndex); +var component; + +//Index function used instead of a 2D array +function index(column, row) { + return column + (row * maxColumn); +} + +function startNewGame() { + //Delete blocks from previous game + for (var i = 0; i < maxIndex; i++) { + if (board[i] != null) + board[i].destroy(); + } + + //Calculate board size + maxColumn = Math.floor(background.width / blockSize); + maxRow = Math.floor(background.height / blockSize); + maxIndex = maxRow * maxColumn; + + //Initialize Board + board = new Array(maxIndex); + for (var column = 0; column < maxColumn; column++) { + for (var row = 0; row < maxRow; row++) { + board[index(column, row)] = null; + createBlock(column, row); + } + } +} + +function createBlock(column, row) { + if (component == null) + component = Qt.createComponent("Block.qml"); + + // Note that if Block.qml was not a local file, component.status would be + // Loading and we should wait for the component's statusChanged() signal to + // know when the file is downloaded and ready before calling createObject(). + if (component.status == Component.Ready) { + var dynamicObject = component.createObject(background); + if (dynamicObject == null) { + console.log("error creating block"); + console.log(component.errorString()); + return false; + } + dynamicObject.x = column * blockSize; + dynamicObject.y = row * blockSize; + dynamicObject.width = blockSize; + dynamicObject.height = blockSize; + board[index(column, row)] = dynamicObject; + } else { + console.log("error loading block component"); + console.log(component.errorString()); + return false; + } + return true; +} +//![0] + |