samegame.js Example Filedeclarative/tutorials/samegame/samegame2/samegame.jsvar 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; } © 2008-2011 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. Privacy Policy Licensees holding valid Qt Commercial licenses may use this document in accordance with the Qt Commercial License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Nokia. Alternatively, this document may be used under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. |