/**** * Classes ****/ var Dinosaur = Container.expand(function () { var self = Container.call(this); var dinosaurGraphics = self.attachAsset('dinosaur_image', { anchorX: 0.5, anchorY: 0.5 }); self.jumpSpeed = 0; self.isJumping = false; self.update = function () { if (self.isJumping) { self.y += self.jumpSpeed; self.jumpSpeed += 1; if (self.y >= 2732 / 2) { self.y = 2732 / 2; self.isJumping = false; } } else { // No gravity when the dinosaur is not jumping if (self.y > 2732 / 2) { self.y = 2732 / 2; } } }; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.jumpSpeed = -30; } }; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle_image', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.x += self.speed; }; }); /**** * Initialize Game ****/ //<Write imports for supported plugins here> var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var dinosaur = game.addChild(new Dinosaur()); dinosaur.x = 2048 / 4; dinosaur.y = 2732 / 2; var obstacles = []; for (var i = 0; i < 5; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = 2048 + i * 500; obstacle.y = 2732 / 2; obstacles.push(obstacle); } game.down = function (x, y, obj) { dinosaur.jump(); dragNode = dinosaur; }; game.move = function (x, y, obj) { if (dragNode) { dragNode.jump(); } }; //{new_code} game.up = function (x, y, obj) { dragNode = null; dinosaur.isJumping = false; }; //{new_code} var dragNode = null; game.update = function () { dinosaur.update(); for (var i = 0; i < obstacles.length; i++) { obstacles[i].update(); if (obstacles[i].x < -100) { obstacles[i].x = 2048 + Math.random() * 500; } if (dinosaur.intersects(obstacles[i])) { LK.showGameOver(); } } };
/****
* Classes
****/
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinosaurGraphics = self.attachAsset('dinosaur_image', {
anchorX: 0.5,
anchorY: 0.5
});
self.jumpSpeed = 0;
self.isJumping = false;
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += 1;
if (self.y >= 2732 / 2) {
self.y = 2732 / 2;
self.isJumping = false;
}
} else {
// No gravity when the dinosaur is not jumping
if (self.y > 2732 / 2) {
self.y = 2732 / 2;
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.jumpSpeed = -30;
}
};
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle_image', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.x += self.speed;
};
});
/****
* Initialize Game
****/
//<Write imports for supported plugins here>
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var dinosaur = game.addChild(new Dinosaur());
dinosaur.x = 2048 / 4;
dinosaur.y = 2732 / 2;
var obstacles = [];
for (var i = 0; i < 5; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = 2048 + i * 500;
obstacle.y = 2732 / 2;
obstacles.push(obstacle);
}
game.down = function (x, y, obj) {
dinosaur.jump();
dragNode = dinosaur;
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.jump();
}
}; //{new_code}
game.up = function (x, y, obj) {
dragNode = null;
dinosaur.isJumping = false;
}; //{new_code}
var dragNode = null;
game.update = function () {
dinosaur.update();
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
if (obstacles[i].x < -100) {
obstacles[i].x = 2048 + Math.random() * 500;
}
if (dinosaur.intersects(obstacles[i])) {
LK.showGameOver();
}
}
};