<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>samegame.js Example File | Qt Quick 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtquick-index.html">Qt Quick</a></td><td ><a href="qtquick-tutorials-samegame-samegame2-example.html">QML Advanced Tutorial 2 - Populating the Game Canvas</a></td><td >samegame.js Example File</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">samegame.js Example File</h1> <span class="subtitle">tutorials/samegame/samegame2/samegame.js</span> <!-- $$$tutorials/samegame/samegame2/samegame.js-description --> <div class="descr"> <a name="details"></a> <pre class="js"> 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; } </pre> </div> <!-- @@@tutorials/samegame/samegame2/samegame.js --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>