/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = LK.getAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(ballGraphics);
self.speed = {
x: 20,
y: -20
};
self._move_migrated = function () {
self.x += self.speed.x;
self.y += self.speed.y;
if (self.x < 0 || self.x > 2048) {
self.speed.x *= -1;
}
};
});
var Brick = Container.expand(function (i) {
var self = Container.call(this);
var brickGraphics = LK.getAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
var colors = [0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x4B0082, 0x8B00FF];
brickGraphics.tint = colors[i % colors.length];
self.addChild(brickGraphics);
});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = LK.getAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(paddleGraphics);
self.speed = 5;
self._move_migrated = function (pos) {
self.x = pos.x;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
game.setBackgroundColor(0x000040);
var ball = game.addChild(new Ball());
var paddle = game.addChild(new Paddle());
var bricks = [];
var score = 0;
var scoreText = new Text2('0', {
size: 150,
fill: '#ffffff'
});
scoreText.anchor.set(.5, 0);
LK.gui.top.addChild(scoreText);
ball.x = 2048 / 2;
ball.y = 2732 / 2;
paddle.x = 2048 / 2;
paddle.y = 2732 - paddle.height;
var brickRows = 10;
var brickColumns = 5;
var brickAreaWidth = brickRows * new Brick().width;
var brickAreaStartX = (2048 - brickAreaWidth) / 2;
for (var i = 0; i < brickRows; i++) {
for (var j = 0; j < brickColumns; j++) {
var brick = game.addChild(new Brick(i + j));
brick.x = brickAreaStartX + i * brick.width + brick.width / 2;
brick.y = j * brick.height + brick.height / 2 + 4 * brick.height;
bricks.push(brick);
}
}
LK.on('tick', function () {
ball._move_migrated();
if (ball.intersects(paddle) && ball.speed.y > 0) {
var hitPos = (ball.x - paddle.x) / paddle.width;
ball.speed.x = hitPos * 20;
ball.speed.y *= -1;
}
if (ball.y < 0) {
ball.speed.y *= -1;
}
for (var i = 0; i < bricks.length; i++) {
if (ball.intersects(bricks[i])) {
ball.speed.y *= -1;
bricks[i].destroy();
bricks.splice(i, 1);
i--;
score++;
LK.setScore(score);
scoreText.setText(score.toString());
}
}
if (ball.y > 2732 || bricks.length === 0) {
LK.showGameOver();
}
});
game.on('move', function (x, y, obj) {
var pos = game.toLocal(obj.global);
paddle._move_migrated(pos);
}); ===================================================================
--- original.js
+++ change.js
@@ -1,7 +1,7 @@
/****
* Classes
-****/
+****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = LK.getAsset('ball', {
anchorX: 0.5,
@@ -11,49 +11,49 @@
self.speed = {
x: 20,
y: -20
};
- self.move = function () {
+ self._move_migrated = function () {
self.x += self.speed.x;
self.y += self.speed.y;
if (self.x < 0 || self.x > 2048) {
self.speed.x *= -1;
}
};
});
+var Brick = Container.expand(function (i) {
+ var self = Container.call(this);
+ var brickGraphics = LK.getAsset('brick', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var colors = [0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x4B0082, 0x8B00FF];
+ brickGraphics.tint = colors[i % colors.length];
+ self.addChild(brickGraphics);
+});
var Paddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = LK.getAsset('paddle', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(paddleGraphics);
self.speed = 5;
- self.move = function (pos) {
+ self._move_migrated = function (pos) {
self.x = pos.x;
};
});
-var Brick = Container.expand(function (i) {
- var self = Container.call(this);
- var brickGraphics = LK.getAsset('brick', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- var colors = [0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x4B0082, 0x8B00FF];
- brickGraphics.tint = colors[i % colors.length];
- self.addChild(brickGraphics);
-});
/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
-****/
+****/
game.setBackgroundColor(0x000040);
var ball = game.addChild(new Ball());
var paddle = game.addChild(new Paddle());
var bricks = [];
@@ -62,9 +62,9 @@
size: 150,
fill: '#ffffff'
});
scoreText.anchor.set(.5, 0);
-LK.gui.topCenter.addChild(scoreText);
+LK.gui.top.addChild(scoreText);
ball.x = 2048 / 2;
ball.y = 2732 / 2;
paddle.x = 2048 / 2;
paddle.y = 2732 - paddle.height;
@@ -80,9 +80,9 @@
bricks.push(brick);
}
}
LK.on('tick', function () {
- ball.move();
+ ball._move_migrated();
if (ball.intersects(paddle) && ball.speed.y > 0) {
var hitPos = (ball.x - paddle.x) / paddle.width;
ball.speed.x = hitPos * 20;
ball.speed.y *= -1;
@@ -104,8 +104,8 @@
if (ball.y > 2732 || bricks.length === 0) {
LK.showGameOver();
}
});
-game.on('move', function (obj) {
- var pos = obj.event.getLocalPosition(game);
- paddle.move(pos);
+game.on('move', function (x, y, obj) {
+ var pos = game.toLocal(obj.global);
+ paddle._move_migrated(pos);
});
\ No newline at end of file