/**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.gravity = 1.5; self.bounceFactor = 0.7; self._move_migrated = function () { self.x += self.velocityX; self.y += self.velocityY; self.velocityY += self.gravity; self.rotation += self.velocityX * 0.005; if (self.x - self.width / 2 < 0) { self.velocityX *= -0.7; self.x = self.width / 2; } if (self.x + self.width / 2 > 2048) { self.velocityX *= -0.7; self.x = 2048 - self.width / 2; } }; self.bounce = function (boot) { var dx = self.x - boot.x; var dy = self.y - boot.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < boot.realWidth / 2 + self.width / 2) { var angle = Math.atan2(dy, dx); var sin = Math.sin(angle); var cos = Math.cos(angle); var bootSpeed = Math.sqrt(boot.velocityX * boot.velocityX + boot.velocityY * boot.velocityY); var ballSpeed = Math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY); var totalSpeed = (bootSpeed + ballSpeed) * self.bounceFactor; var directionX = cos * totalSpeed; var directionY = sin * totalSpeed; var scoreIncrement = Math.max(0, Math.round(totalSpeed) - 20); self.parent.score += scoreIncrement; LK.setScore(self.parent.score); self.velocityX = directionX; self.velocityY = directionY; var overlap = boot.realWidth / 2 + self.width / 2 - distance; self.x += 2 * overlap * cos; self.y += 2 * overlap * sin; } }; }); var Boot = Container.expand(function () { var self = Container.call(this); var bootGraphics = self.attachAsset('boot', { anchorX: 0.5, anchorY: 0.5 }); self.realWidth = bootGraphics.width * 0.7; self.velocityX = 0; self.velocityY = 0; self.previousX = 0; self.previousY = 0; self.calculateVelocity = function () { self.velocityX = self.x - self.previousX; self.velocityY = self.y - self.previousY; self.previousX = self.x; self.previousY = self.y; var targetRotation = -Math.atan2(self.velocityY, 0) / 2 + Math.PI / 9; self.rotation += (targetRotation - self.rotation) * 0.1; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x36759c }); /**** * Game Code ****/ var stadiumBackground = game.attachAsset('stadium', { anchorX: 0.5, anchorY: 0.5 }); stadiumBackground.x = 2048 / 2; stadiumBackground.y = 2732 / 2; stadiumBackground.alpha = .8; game.on('move', function (x, y, obj) { var pos = game.toLocal(obj.global); boot.x = pos.x; boot.y = pos.y; }); var ball = game.addChild(new Ball()); ball.x = 2048 / 2; ball.y = 2732 / 4; var boot = game.addChild(new Boot()); boot.x = 2048 / 2; boot.y = 2732 / 2; boot.visible = true; var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(.5, 0); LK.gui.top.addChild(scoreTxt); var tutorialTxt = new Text2('Kick harder for more points', { size: 100, fill: 0xFFFFFF, dropShadow: true, dropShadowColor: '#000000', dropShadowBlur: 4, dropShadowAngle: Math.PI / 6, dropShadowDistance: 6 }); tutorialTxt.anchor.set(.5, 1); LK.gui.bottom.addChild(tutorialTxt); var isGameOver = false; var isGameStarted = false; game.score = 0; game.on('down', function (x, y, obj) { var pos = game.toLocal(obj.global); boot.x = pos.x; boot.y = pos.y; boot.visible = true; }); LK.on('tick', function () { boot.calculateVelocity(); if (!isGameStarted) { var dx = boot.x - ball.x; var dy = boot.y - ball.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < boot.realWidth / 2 + ball.width / 2) { ball.bounce(boot); isGameStarted = true; } } else { ball._move_migrated(); var dx = boot.x - ball.x; var dy = boot.y - ball.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < boot.realWidth / 2 + ball.width / 2) { ball.bounce(boot); } } if (ball.y > 2732 - ball.height / 2) { isGameOver = true; } if (isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } LK.setScore(game.score); scoreTxt.setText(LK.getScore()); });
===================================================================
--- original.js
+++ change.js
@@ -1,27 +1,7 @@
/****
* Classes
-****/
-var Boot = Container.expand(function () {
- var self = Container.call(this);
- var bootGraphics = self.attachAsset('boot', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.realWidth = bootGraphics.width * 0.7;
- self.velocityX = 0;
- self.velocityY = 0;
- self.previousX = 0;
- self.previousY = 0;
- self.calculateVelocity = function () {
- self.velocityX = self.x - self.previousX;
- self.velocityY = self.y - self.previousY;
- self.previousX = self.x;
- self.previousY = self.y;
- var targetRotation = -Math.atan2(self.velocityY, 0) / 2 + Math.PI / 9;
- self.rotation += (targetRotation - self.rotation) * 0.1;
- };
-});
+****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
@@ -30,9 +10,9 @@
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 1.5;
self.bounceFactor = 0.7;
- self.move = function () {
+ self._move_migrated = function () {
self.x += self.velocityX;
self.y += self.velocityY;
self.velocityY += self.gravity;
self.rotation += self.velocityX * 0.005;
@@ -68,28 +48,48 @@
self.y += 2 * overlap * sin;
}
};
});
+var Boot = Container.expand(function () {
+ var self = Container.call(this);
+ var bootGraphics = self.attachAsset('boot', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.realWidth = bootGraphics.width * 0.7;
+ self.velocityX = 0;
+ self.velocityY = 0;
+ self.previousX = 0;
+ self.previousY = 0;
+ self.calculateVelocity = function () {
+ self.velocityX = self.x - self.previousX;
+ self.velocityY = self.y - self.previousY;
+ self.previousX = self.x;
+ self.previousY = self.y;
+ var targetRotation = -Math.atan2(self.velocityY, 0) / 2 + Math.PI / 9;
+ self.rotation += (targetRotation - self.rotation) * 0.1;
+ };
+});
/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x36759c
});
/****
* Game Code
-****/
+****/
var stadiumBackground = game.attachAsset('stadium', {
anchorX: 0.5,
anchorY: 0.5
});
stadiumBackground.x = 2048 / 2;
stadiumBackground.y = 2732 / 2;
stadiumBackground.alpha = .8;
-game.on('move', function (obj) {
- var pos = obj.event.getLocalPosition(game);
+game.on('move', function (x, y, obj) {
+ var pos = game.toLocal(obj.global);
boot.x = pos.x;
boot.y = pos.y;
});
var ball = game.addChild(new Ball());
@@ -100,15 +100,15 @@
boot.y = 2732 / 2;
boot.visible = true;
var scoreTxt = new Text2('0', {
size: 150,
- fill: "#ffffff"
+ fill: 0xFFFFFF
});
scoreTxt.anchor.set(.5, 0);
-LK.gui.topCenter.addChild(scoreTxt);
+LK.gui.top.addChild(scoreTxt);
var tutorialTxt = new Text2('Kick harder for more points', {
size: 100,
- fill: "#ffffff",
+ fill: 0xFFFFFF,
dropShadow: true,
dropShadowColor: '#000000',
dropShadowBlur: 4,
dropShadowAngle: Math.PI / 6,
@@ -118,10 +118,10 @@
LK.gui.bottom.addChild(tutorialTxt);
var isGameOver = false;
var isGameStarted = false;
game.score = 0;
-game.on('down', function (obj) {
- var pos = obj.event.getLocalPosition(game);
+game.on('down', function (x, y, obj) {
+ var pos = game.toLocal(obj.global);
boot.x = pos.x;
boot.y = pos.y;
boot.visible = true;
});
@@ -135,9 +135,9 @@
ball.bounce(boot);
isGameStarted = true;
}
} else {
- ball.move();
+ ball._move_migrated();
var dx = boot.x - ball.x;
var dy = boot.y - ball.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < boot.realWidth / 2 + ball.width / 2) {