/****
* Classes
****/
// BadFallingObject class: represents a clickable object that decreases score
var BadFallingObject = Container.expand(function () {
var self = Container.call(this);
// Attach a red ellipse asset, centered
var obj = self.attachAsset('badFallingEllipse', {
anchorX: 0.5,
anchorY: 0.5
});
// Direction: 0=down, 1=up, 2=left, 3=right
self.direction = 0;
self.speed = 8 + Math.random() * 8;
// Track last positions for off-screen detection
self.lastX = self.x;
self.lastY = self.y;
// Handle tap/click on the object
self.down = function (x, y, objEvent) {
if (!self._destroyed) {
score -= 1;
if (score < 0) score = 0;
updateScore();
self.destroy();
}
};
// Called every frame
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
// Randomly accelerate or decelerate speed a little, but clamp to min/max
if (Math.random() < 0.15) {
// 15% chance per frame to change speed
var delta = (Math.random() - 0.5) * 1.2; // -0.6 to +0.6
self.speed += delta;
if (self.speed < 3) self.speed = 3;
if (self.speed > 20) self.speed = 20;
}
// Randomly change direction with a small chance
if (Math.random() < 0.03) {
// Pick a new direction different from current
var newDir = Math.floor(Math.random() * 4);
if (newDir !== self.direction) {
self.direction = newDir;
}
}
if (self.direction === 0) {
// down
self.y += self.speed;
if (self.lastY <= 2732 && self.y > 2732) {
self.destroy();
}
} else if (self.direction === 1) {
// up
self.y -= self.speed;
if (self.lastY >= -100 && self.y < -100) {
self.destroy();
}
} else if (self.direction === 2) {
// left
self.x -= self.speed;
if (self.lastX >= -100 && self.x < -100) {
self.destroy();
}
} else if (self.direction === 3) {
// right
self.x += self.speed;
if (self.lastX <= 2048 + 100 && self.x > 2048 + 100) {
self.destroy();
}
}
};
return self;
});
// FallingObject class: represents a clickable object moving in any direction
var FallingObject = Container.expand(function () {
var self = Container.call(this);
// Attach a simple ellipse asset, centered
var obj = self.attachAsset('fallingEllipse', {
anchorX: 0.5,
anchorY: 0.5
});
// Direction: 0=down, 1=up, 2=left, 3=right
self.direction = 0;
self.speed = 8 + Math.random() * 8;
// Track last positions for off-screen detection
self.lastX = self.x;
self.lastY = self.y;
// Handle tap/click on the object
self.down = function (x, y, objEvent) {
if (!self._destroyed) {
score += 1;
updateScore();
self.destroy();
}
};
// Called every frame
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
// Randomly accelerate or decelerate speed a little, but clamp to min/max
if (Math.random() < 0.15) {
// 15% chance per frame to change speed
var delta = (Math.random() - 0.5) * 1.2; // -0.6 to +0.6
self.speed += delta;
if (self.speed < 3) self.speed = 3;
if (self.speed > 20) self.speed = 20;
}
// Randomly change direction with a small chance
if (Math.random() < 0.03) {
// Pick a new direction different from current
var newDir = Math.floor(Math.random() * 4);
if (newDir !== self.direction) {
self.direction = newDir;
}
}
if (self.direction === 0) {
// down
self.y += self.speed;
if (self.lastY <= 2732 && self.y > 2732) {
self.destroy();
}
} else if (self.direction === 1) {
// up
self.y -= self.speed;
if (self.lastY >= -100 && self.y < -100) {
self.destroy();
}
} else if (self.direction === 2) {
// left
self.x -= self.speed;
if (self.lastX >= -100 && self.x < -100) {
self.destroy();
}
} else if (self.direction === 3) {
// right
self.x += self.speed;
if (self.lastX <= 2048 + 100 && self.x > 2048 + 100) {
self.destroy();
}
}
};
return self;
});
/****
* Initialize Game
****/
// Asset for falling objects (ellipse, visually distinct)
// No custom assets needed for this minimal tap game. The score will be displayed using Text2.
// No plugins needed for this minimal tap game.
// No custom classes needed for this minimal tap game.
var game = new LK.Game({
backgroundColor: 0x000000 // Black background for contrast
});
/****
* Game Code
****/
// Set a visually appealing background color (optional, can be changed)
game.setBackgroundColor(0x1a1a1a);
// Create the score text, large and centered at the top
var scoreTxt = new Text2('0', {
size: 200,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Center horizontally, top edge
LK.gui.top.addChild(scoreTxt);
// Initialize score variable
var score = 0;
// Function to update score display
function updateScore() {
scoreTxt.setText(score);
}
// Handle tap/click/touch anywhere on the game area
game.down = function (x, y, obj) {
// Ignore taps in the top-left 100x100 area (reserved for menu)
if (x < 100 && y < 100) return;
score += 1;
updateScore();
};
// Optionally, update the score at start
updateScore();
// Array to keep track of falling objects
var fallingObjects = [];
// Spawn a new falling object from a random direction
function spawnFallingObject() {
// 25% chance to spawn a bad object, 75% normal
var isBad = Math.random() < 0.25;
var obj;
if (isBad) {
obj = new BadFallingObject();
} else {
obj = new FallingObject();
}
// Pick a random direction: 0=down, 1=up, 2=left, 3=right
var dir = Math.floor(Math.random() * 4);
obj.direction = dir;
// Set initial position based on direction
if (dir === 0) {
// down (from top)
obj.x = 120 + Math.random() * (2048 - 240);
obj.y = -100;
} else if (dir === 1) {
// up (from bottom)
obj.x = 120 + Math.random() * (2048 - 240);
obj.y = 2732 + 100;
} else if (dir === 2) {
// left (from right)
obj.x = 2048 + 100;
obj.y = 120 + Math.random() * (2732 - 240);
} else if (dir === 3) {
// right (from left)
obj.x = -100;
obj.y = 120 + Math.random() * (2732 - 240);
}
fallingObjects.push(obj);
game.addChild(obj);
}
// Game update: move objects, remove destroyed ones, spawn new ones
game.update = function () {
// Update all falling objects
for (var i = fallingObjects.length - 1; i >= 0; i--) {
var obj = fallingObjects[i];
if (obj._destroyed) {
fallingObjects.splice(i, 1);
continue;
}
if (typeof obj.update === "function") obj.update();
}
// Spawn a new object every 40 frames (~1.5 per second)
if (LK.ticks % 40 === 0) {
spawnFallingObject();
}
};
// Remove tap-to-score-anywhere, now only objects are clickable
game.down = function (x, y, obj) {
// Do nothing here; objects handle their own tap
};
// Asset for falling objects (ellipse, visually distinct) /****
* Classes
****/
// BadFallingObject class: represents a clickable object that decreases score
var BadFallingObject = Container.expand(function () {
var self = Container.call(this);
// Attach a red ellipse asset, centered
var obj = self.attachAsset('badFallingEllipse', {
anchorX: 0.5,
anchorY: 0.5
});
// Direction: 0=down, 1=up, 2=left, 3=right
self.direction = 0;
self.speed = 8 + Math.random() * 8;
// Track last positions for off-screen detection
self.lastX = self.x;
self.lastY = self.y;
// Handle tap/click on the object
self.down = function (x, y, objEvent) {
if (!self._destroyed) {
score -= 1;
if (score < 0) score = 0;
updateScore();
self.destroy();
}
};
// Called every frame
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
// Randomly accelerate or decelerate speed a little, but clamp to min/max
if (Math.random() < 0.15) {
// 15% chance per frame to change speed
var delta = (Math.random() - 0.5) * 1.2; // -0.6 to +0.6
self.speed += delta;
if (self.speed < 3) self.speed = 3;
if (self.speed > 20) self.speed = 20;
}
// Randomly change direction with a small chance
if (Math.random() < 0.03) {
// Pick a new direction different from current
var newDir = Math.floor(Math.random() * 4);
if (newDir !== self.direction) {
self.direction = newDir;
}
}
if (self.direction === 0) {
// down
self.y += self.speed;
if (self.lastY <= 2732 && self.y > 2732) {
self.destroy();
}
} else if (self.direction === 1) {
// up
self.y -= self.speed;
if (self.lastY >= -100 && self.y < -100) {
self.destroy();
}
} else if (self.direction === 2) {
// left
self.x -= self.speed;
if (self.lastX >= -100 && self.x < -100) {
self.destroy();
}
} else if (self.direction === 3) {
// right
self.x += self.speed;
if (self.lastX <= 2048 + 100 && self.x > 2048 + 100) {
self.destroy();
}
}
};
return self;
});
// FallingObject class: represents a clickable object moving in any direction
var FallingObject = Container.expand(function () {
var self = Container.call(this);
// Attach a simple ellipse asset, centered
var obj = self.attachAsset('fallingEllipse', {
anchorX: 0.5,
anchorY: 0.5
});
// Direction: 0=down, 1=up, 2=left, 3=right
self.direction = 0;
self.speed = 8 + Math.random() * 8;
// Track last positions for off-screen detection
self.lastX = self.x;
self.lastY = self.y;
// Handle tap/click on the object
self.down = function (x, y, objEvent) {
if (!self._destroyed) {
score += 1;
updateScore();
self.destroy();
}
};
// Called every frame
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
// Randomly accelerate or decelerate speed a little, but clamp to min/max
if (Math.random() < 0.15) {
// 15% chance per frame to change speed
var delta = (Math.random() - 0.5) * 1.2; // -0.6 to +0.6
self.speed += delta;
if (self.speed < 3) self.speed = 3;
if (self.speed > 20) self.speed = 20;
}
// Randomly change direction with a small chance
if (Math.random() < 0.03) {
// Pick a new direction different from current
var newDir = Math.floor(Math.random() * 4);
if (newDir !== self.direction) {
self.direction = newDir;
}
}
if (self.direction === 0) {
// down
self.y += self.speed;
if (self.lastY <= 2732 && self.y > 2732) {
self.destroy();
}
} else if (self.direction === 1) {
// up
self.y -= self.speed;
if (self.lastY >= -100 && self.y < -100) {
self.destroy();
}
} else if (self.direction === 2) {
// left
self.x -= self.speed;
if (self.lastX >= -100 && self.x < -100) {
self.destroy();
}
} else if (self.direction === 3) {
// right
self.x += self.speed;
if (self.lastX <= 2048 + 100 && self.x > 2048 + 100) {
self.destroy();
}
}
};
return self;
});
/****
* Initialize Game
****/
// Asset for falling objects (ellipse, visually distinct)
// No custom assets needed for this minimal tap game. The score will be displayed using Text2.
// No plugins needed for this minimal tap game.
// No custom classes needed for this minimal tap game.
var game = new LK.Game({
backgroundColor: 0x000000 // Black background for contrast
});
/****
* Game Code
****/
// Set a visually appealing background color (optional, can be changed)
game.setBackgroundColor(0x1a1a1a);
// Create the score text, large and centered at the top
var scoreTxt = new Text2('0', {
size: 200,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Center horizontally, top edge
LK.gui.top.addChild(scoreTxt);
// Initialize score variable
var score = 0;
// Function to update score display
function updateScore() {
scoreTxt.setText(score);
}
// Handle tap/click/touch anywhere on the game area
game.down = function (x, y, obj) {
// Ignore taps in the top-left 100x100 area (reserved for menu)
if (x < 100 && y < 100) return;
score += 1;
updateScore();
};
// Optionally, update the score at start
updateScore();
// Array to keep track of falling objects
var fallingObjects = [];
// Spawn a new falling object from a random direction
function spawnFallingObject() {
// 25% chance to spawn a bad object, 75% normal
var isBad = Math.random() < 0.25;
var obj;
if (isBad) {
obj = new BadFallingObject();
} else {
obj = new FallingObject();
}
// Pick a random direction: 0=down, 1=up, 2=left, 3=right
var dir = Math.floor(Math.random() * 4);
obj.direction = dir;
// Set initial position based on direction
if (dir === 0) {
// down (from top)
obj.x = 120 + Math.random() * (2048 - 240);
obj.y = -100;
} else if (dir === 1) {
// up (from bottom)
obj.x = 120 + Math.random() * (2048 - 240);
obj.y = 2732 + 100;
} else if (dir === 2) {
// left (from right)
obj.x = 2048 + 100;
obj.y = 120 + Math.random() * (2732 - 240);
} else if (dir === 3) {
// right (from left)
obj.x = -100;
obj.y = 120 + Math.random() * (2732 - 240);
}
fallingObjects.push(obj);
game.addChild(obj);
}
// Game update: move objects, remove destroyed ones, spawn new ones
game.update = function () {
// Update all falling objects
for (var i = fallingObjects.length - 1; i >= 0; i--) {
var obj = fallingObjects[i];
if (obj._destroyed) {
fallingObjects.splice(i, 1);
continue;
}
if (typeof obj.update === "function") obj.update();
}
// Spawn a new object every 40 frames (~1.5 per second)
if (LK.ticks % 40 === 0) {
spawnFallingObject();
}
};
// Remove tap-to-score-anywhere, now only objects are clickable
game.down = function (x, y, obj) {
// Do nothing here; objects handle their own tap
};
// Asset for falling objects (ellipse, visually distinct)