===================================================================
--- original.js
+++ change.js
@@ -1,7 +1,13 @@
+/****
+* Classes
+****/
var OverhangPlate = Container.expand(function (width, direction, color) {
var self = Container.call(this);
- var plateGraphics = self.createAsset('plate', 'Overhang Plate Graphics', .5, .5);
+ var plateGraphics = self.attachAsset('plate', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
plateGraphics.width = width;
plateGraphics.tint = color;
self.speedX = direction === 'right' ? 5 : -5;
self.speedY = -20;
@@ -13,53 +19,15 @@
self.speedX *= 0.99;
self.rotation += self.rotationSpeed;
};
});
-var Plate = Container.expand(function (targetWidth) {
+var Brick = Container.expand(function (targetWidth) {
var self = Container.call(this);
- var plateGraphics = self.createAsset('plate', 'Plate Graphics', .5, .5);
- plateGraphics.width = targetWidth;
- self.colorIndex = 0;
- self.hsvToRgb = function (h, s, v) {
- var r, g, b;
- var i = Math.floor(h * 6);
- var f = h * 6 - i;
- var p = v * (1 - s);
- var q = v * (1 - f * s);
- var t = v * (1 - (1 - f) * s);
- switch (i % 6) {
- case 0:
- (r = v, g = t, b = p);
- break;
- case 1:
- (r = q, g = v, b = p);
- break;
- case 2:
- (r = p, g = v, b = t);
- break;
- case 3:
- (r = p, g = q, b = v);
- break;
- case 4:
- (r = t, g = p, b = v);
- break;
- case 5:
- (r = v, g = p, b = q);
- break;
- }
- return (r * 255 << 16) + (g * 255 << 8) + b * 255;
- };
- self.updateColor = function () {
- var h = self.colorIndex / 32;
- var s = 1;
- var v = 1;
- self.color = self.hsvToRgb(h, s, v);
- plateGraphics.tint = self.color;
- self.colorIndex = (self.colorIndex + 1) % 32;
- var bgColor = self.hsvToRgb(h, s, v / 3);
- LK.stageContainer.setBackgroundColor(bgColor);
- };
- self.updateColor();
+ var brickGraphics = self.attachAsset('brick', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ brickGraphics.width = targetWidth;
self.speed = 22.5;
self.move = function () {
self.x -= self.speed;
};
@@ -69,87 +37,87 @@
self.slide = function () {
self.x = 2048 + self.width / 2;
};
});
-var Game = Container.expand(function () {
- var self = Container.call(this);
- var scoreTxt = new Text2('0', {
- size: 150,
- fill: '#ffffff',
- font: 'Impact'
- });
- scoreTxt.anchor.set(.5, 0);
- LK.gui.topCenter.addChild(scoreTxt);
- var plateContainer = new Container();
- plateContainer.targetY = 0;
- self.addChild(plateContainer);
- var currentPlateWidth = 1000;
- var plates = [];
- var plateColorIndex = 0;
- var firstPlate = new Plate(currentPlateWidth);
- firstPlate.x = 2048 / 2;
- firstPlate.y = 2732 - firstPlate.height / 2;
- firstPlate.speed = 0;
- firstPlate.colorIndex = plateColorIndex;
- firstPlate.updateColor();
- plates.push(firstPlate);
- plateContainer.addChild(firstPlate);
- var newPlate = new Plate(currentPlateWidth);
- newPlate.slide();
- newPlate.y = 2732 - plates.length * newPlate.height - newPlate.height / 2;
- plateColorIndex++;
- newPlate.colorIndex = plateColorIndex;
- newPlate.updateColor();
- plates.push(newPlate);
- plateContainer.addChild(newPlate);
- stage.on('down', function (obj) {
- var topPlate = plates[plates.length - 2];
- if (newPlate.x < topPlate.x + topPlate.width && newPlate.x + newPlate.width > topPlate.x) {
- var overhang = newPlate.x + newPlate.width / 2 - (topPlate.x + topPlate.width / 2);
- var overhangSide = overhang > 0 ? 'right' : 'left';
- overhang = Math.abs(overhang);
- newPlate.stack();
- currentPlateWidth -= overhang;
- var overhangPlate = new OverhangPlate(overhang, overhangSide, newPlate.color);
- overhangPlate.x = overhangSide === 'right' ? newPlate.x + newPlate.width / 2 : newPlate.x - newPlate.width / 2;
- overhangPlate.y = newPlate.y;
- plateContainer.addChildAt(overhangPlate, 0);
- newPlate.width -= overhang;
- if (overhangSide === 'right') {
- newPlate.x -= overhang / 2;
- overhangPlate.x -= overhang / 2;
- } else {
- newPlate.x += overhang / 2;
- overhangPlate.x += overhang / 2;
- }
- newPlate = new Plate(currentPlateWidth);
- newPlate.slide();
- newPlate.y = 2732 - plates.length * newPlate.height - newPlate.height / 2;
- plateColorIndex++;
- newPlate.colorIndex = plateColorIndex;
- newPlate.updateColor();
- plates.push(newPlate);
- plateContainer.addChild(newPlate);
- if (plates.length > 17) {
- plateContainer.targetY += newPlate.height;
- }
- scoreTxt.setText(plates.length - 2);
+
+/****
+* Initialize Game
+****/
+var game = new LK.Game({
+ backgroundColor: 0x000000
+});
+
+/****
+* Game Code
+****/
+var scoreTxt = new Text2('0', {
+ size: 150,
+ fill: '#ffffff',
+ font: 'Impact'
+});
+scoreTxt.anchor.set(.5, 0);
+LK.gui.topCenter.addChild(scoreTxt);
+var plateContainer = new Container();
+plateContainer.targetY = 0;
+game.addChild(plateContainer);
+var currentPlateWidth = 1000;
+var plates = [];
+var plateColorIndex = 0;
+var firstBrick = new Brick(currentPlateWidth);
+firstBrick.x = 2048 / 2;
+firstBrick.y = 2732 - firstBrick.height / 2;
+firstBrick.speed = 0;
+plates.push(firstBrick);
+plateContainer.addChild(firstBrick);
+var newBrick = new Brick(currentPlateWidth);
+newBrick.slide();
+newBrick.y = 2732 - plates.length * newBrick.height - newBrick.height / 2;
+plates.push(newBrick);
+plateContainer.addChild(newBrick);
+game.on('down', function (obj) {
+ var topBrick = plates[plates.length - 2];
+ if (newBrick.x < topBrick.x + topBrick.width && newBrick.x + newBrick.width > topBrick.x) {
+ var overhang = newBrick.x + newBrick.width / 2 - (topBrick.x + topBrick.width / 2);
+ var overhangSide = overhang > 0 ? 'right' : 'left';
+ overhang = Math.abs(overhang);
+ newBrick.stack();
+ currentPlateWidth -= overhang;
+ var overhangBrick = new OverhangPlate(overhang, overhangSide, newBrick.color);
+ overhangBrick.x = overhangSide === 'right' ? newBrick.x + newBrick.width / 2 : newBrick.x - newBrick.width / 2;
+ overhangBrick.y = newBrick.y;
+ plateContainer.addChildAt(overhangBrick, 0);
+ newBrick.width -= overhang;
+ if (overhangSide === 'right') {
+ newBrick.x -= overhang / 2;
+ overhangBrick.x -= overhang / 2;
} else {
- LK.showGameOver();
+ newBrick.x += overhang / 2;
+ overhangBrick.x += overhang / 2;
}
- });
- LK.on('tick', function () {
- for (var i = 0; i < plateContainer.children.length; i++) {
- var plate = plateContainer.children[i];
- plate.move();
- if (plate instanceof OverhangPlate && (plate.x < -plate.width / 2 || plate.x > 2048 + plate.width / 2 || plate.y > 2732 + plate.height / 2)) {
- plate.destroy();
- }
- if (plate instanceof Plate && plate.x < -plate.width / 2) {
- LK.showGameOver();
- }
+ newBrick = new Brick(currentPlateWidth);
+ newBrick.slide();
+ newBrick.y = 2732 - plates.length * newBrick.height - newBrick.height / 2;
+ plates.push(newBrick);
+ plateContainer.addChild(newBrick);
+ if (plates.length > 17) {
+ plateContainer.targetY += newBrick.height;
}
- if (plateContainer.y !== plateContainer.targetY) {
- plateContainer.y += (plateContainer.targetY - plateContainer.y) * 0.1;
- }
- });
+ scoreTxt.setText(plates.length - 2);
+ } else {
+ LK.showGameOver();
+ }
});
+LK.on('tick', function () {
+ for (var i = 0; i < plateContainer.children.length; i++) {
+ var plate = plateContainer.children[i];
+ plate.move();
+ if (plate instanceof OverhangPlate && (plate.x < -plate.width / 2 || plate.x > 2048 + plate.width / 2 || plate.y > 2732 + plate.height / 2)) {
+ plate.destroy();
+ }
+ if (plate instanceof Plate && plate.x < -plate.width / 2) {
+ LK.showGameOver();
+ }
+ }
+ if (plateContainer.y !== plateContainer.targetY) {
+ plateContainer.y += (plateContainer.targetY - plateContainer.y) * 0.1;
+ }
+});
\ No newline at end of file