summaryrefslogtreecommitdiffstats
path: root/examples/declarative/tutorials/samegame/samegame2/samegame.js
diff options
context:
space:
mode:
Diffstat (limited to 'examples/declarative/tutorials/samegame/samegame2/samegame.js')
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/samegame.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.js b/examples/declarative/tutorials/samegame/samegame2/samegame.js
new file mode 100644
index 0000000..2bf68b5
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/samegame.js
@@ -0,0 +1,61 @@
+//![0]
+//Note that X/Y referred to here are in game coordinates
+var maxX = 10;//Nums are for tileSize 40
+var maxY = 15;
+var tileSize = 40;
+var maxIndex = maxX*maxY;
+var board = new Array(maxIndex);
+var tileSrc = "Block.qml";
+var component;
+
+//Index function used instead of a 2D array
+function index(xIdx,yIdx) {
+ return xIdx + (yIdx * maxX);
+}
+
+function initBoard()
+{
+ //Calculate board size
+ maxX = Math.floor(background.width/tileSize);
+ maxY = Math.floor(background.height/tileSize);
+ maxIndex = maxY*maxX;
+
+ //Initialize Board
+ board = new Array(maxIndex);
+ for(xIdx=0; xIdx<maxX; xIdx++){
+ for(yIdx=0; yIdx<maxY; yIdx++){
+ board[index(xIdx,yIdx)] = null;
+ createBlock(xIdx,yIdx);
+ }
+ }
+}
+
+function createBlock(xIdx,yIdx){
+ if(component==null)
+ component = createComponent(tileSrc);
+
+ // Note that we don't wait for the component to become ready. This will
+ // only work if the block QML is a local file. Otherwise the component will
+ // not be ready immediately. There is a statusChanged signal on the
+ // component you could use if you want to wait to load remote files.
+ if(component.isReady){
+ dynamicObject = component.createObject();
+ if(dynamicObject == null){
+ print("error creating block");
+ print(component.errorsString());
+ return false;
+ }
+ dynamicObject.parent = background;
+ dynamicObject.x = xIdx*tileSize;
+ dynamicObject.y = yIdx*tileSize;
+ dynamicObject.width = tileSize;
+ dynamicObject.height = tileSize;
+ board[index(xIdx,yIdx)] = dynamicObject;
+ }else{//isError or isLoading
+ print("error loading block component");
+ print(component.errorsString());
+ return false;
+ }
+ return true;
+}
+//![0]