/****
* Classes
****/
// Crop class
var Crop = Container.expand(function () {
var self = Container.call(this);
var cropGraphics = self.attachAsset('crop', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Crop's behavior will be defined here
};
});
// The assets will be automatically created and loaded by the LK engine
// Farmer class
var Farmer = Container.expand(function () {
var self = Container.call(this);
var farmerGraphics = self.attachAsset('farmer', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Farmer's behavior will be defined here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize farmer
var farmer = game.addChild(new Farmer());
farmer.x = 2048 / 2; // Position farmer at the center of the screen
farmer.y = 2732 / 2;
// Initialize crops array
var crops = [];
// Game update function
game.update = function () {
// Randomly generate crops
if (LK.ticks % 60 == 0) {
// Every second
var newCrop = new Crop();
newCrop.x = Math.random() * 2048; // Random position in the x-axis
newCrop.y = 0; // Start at the top of the screen
crops.push(newCrop);
game.addChild(newCrop);
}
// Check for collisions between the farmer and the crops
for (var i = crops.length - 1; i >= 0; i--) {
if (farmer.intersects(crops[i])) {
// If a collision is detected, remove the crop
crops[i].destroy();
crops.splice(i, 1);
// Increase the score
LK.setScore(LK.getScore() + 1);
}
}
};
// Handle touch events
game.down = function (x, y, obj) {
// Move the farmer to the touch position
farmer.x = x;
farmer.y = y;
};
game.move = function (x, y, obj) {
// Move the farmer to the touch position
farmer.x = x;
farmer.y = y;
};