Code edit (7 edits merged)
Please save this source code
User prompt
in the handleButtons function, add a copy of the button clicked to the latest cakelayer added.
Code edit (8 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: button.parent.numChildren is not a function' in or related to this line: 'console.log('button.parent.children.length - 1: ' + button.parent.numChildren() - 1);' Line Number: 358
Code edit (1 edits merged)
Please save this source code
User prompt
show a scorelabel center top
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Error: The supplied index is out of bounds' in or related to this line: 'button.parent.setChildIndex(button, button.parent.getChildIndex(latestCakeLayer) + 1);' Line Number: 356
User prompt
in handlebuttons, also move the pressed button to a layer above the latest cake layer.
Code edit (3 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: game.getChildren is not a function' in or related to this line: 'button.zIndex = game.getChildren().length;' Line Number: 350
Code edit (1 edits merged)
Please save this source code
User prompt
let all buttons call handlebuttons function in their on down handler.
User prompt
implement the handebuttons function according to the comments inside it.
Code edit (1 edits merged)
Please save this source code
Initial prompt
Copy Layer Cake Panic
/**** * Classes ****/ var BlueberryButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('blueberryButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); // CakeLayer class var CakeLayer = Container.expand(function () { var self = Container.call(this); var layerGraphic = self.attachAsset('cakeLayer', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.startY = self.y; self.targetY = 0; self.tweenStartTime = LK.ticks; self.tweening = false; self.inPlace = false; self.move = function () { if (!self.tweening && !self.inPlace) { self.tweening = true; self.startY = self.y; self.targetY = game.height - 300 - currentCakeHeight - self.height / 2; self.tweenStartTime = LK.ticks; } var tweenDuration = 60; // Duration of the tween in ticks var timeElapsed = LK.ticks - self.tweenStartTime; if (timeElapsed < tweenDuration) { //self.y = easeOutElastic(timeElapsed, self.startY, self.targetY - self.startY, tweenDuration); self.y = easeInExpo(timeElapsed, self.startY, self.targetY - self.startY, tweenDuration); //self.y = easeOutElastic(timeElapsed, self.startY, self.targetY - self.startY, tweenDuration); } else { self.y = self.targetY; self.tweening = false; self.inPlace = true; if (!self.shaken && typeof shakeScreen === 'function') { shakeScreen(300); self.shaken = true; showOptions(); } //currentCakeHeight = currentCakeHeight + self.height; //console.log('currentCakeHeight is now:' + currentCakeHeight); } }; }); // Filling class var Filling = Container.expand(function (type) { var self = Container.call(this); var fillingGraphic = self.attachAsset(type === 'good' ? 'goodFilling' : 'badFilling', { anchorX: 0.5, anchorY: 0.5 }); self.type = type; self.speed = 5; self.move = function () { self.y += self.speed; }; }); var FishButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('fishButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var FrogButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('frogButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var MangoButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('mangoButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var MouseButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('mouseButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var NutellaButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('nutellaButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var PineappleButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('pineappleButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var RaspberryButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('raspberryButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); var StrawberryButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('strawberryButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); // Table class var Table = Container.expand(function () { var self = Container.call(this); var tableGraphic = self.attachAsset('table', { anchorX: 0.5, anchorY: 0.5 }); }); var WhippedCreamButton = Container.expand(function () { var self = Container.call(this); var buttonGraphic = self.attachAsset('whippedCreamButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function (obj) { handleButtons(self); }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Init game with sky blue background }); /**** * Game Code ****/ function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } function showOptions() { goodFill.forEach(function (i) { i.y = 4000; }); badFill.forEach(function (i) { i.y = 4000; }); var t = []; for (i = 0; i < 10; i++) { var g = goodFill[Math.floor(Math.random() * goodFill.length)]; if (!t.includes(g) && t.length < 2) { t.push(g); } } console.log(t); var b = badFill[Math.floor(Math.random() * badFill.length)]; t.push(b); shuffleArray(t); t[0].x = game.width / 2 - 400; t[1].x = game.width / 2; t[2].x = game.width / 2 + 400; for (var j = 0; j < t.length; j++) { t[j].y = 2550; } } ; // Add background image function shakeScreen(intensity, shakes) { var shakeCount = 0; var maxShakes = shakes || 10; var shakeAmount = 10; var shakeInterval = LK.setInterval(function () { game.x += Math.random() * shakeAmount / 2 - shakeAmount / 4; game.y += Math.random() * shakeAmount - shakeAmount / 2; shakeCount++; if (shakeCount >= maxShakes) { LK.clearInterval(shakeInterval); game.x = 0; game.y = 0; } }, 5); } var background = game.addChild(LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5 })); background.x = game.width / 2; background.y = game.height / 2; // Initialize important asset arrays // Define the assets for the cake layers and fillings function easeInExpo(t, b, c, d) { //d *= 15; return t == 0 ? b : c * Math.pow(2, 10 * (t / d - 1)) + b; } function easeOutElastic(t, b, c, d) { //d *= 15; var s = 1.70158; var p = 0; var a = c; if (t == 0) { return b; } if ((t /= d) == 1) { return b + c; } if (!p) { p = d * .3; } if (a < Math.abs(c)) { a = c; var s = p / 4; } else { var s = p / (2 * Math.PI) * Math.asin(c / a); } return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b; } var cakeLayers = []; var fillings = []; var currentCakeHeight = 0; // Create a draggable cake base var cakeBase = game.addChild(new CakeLayer()); cakeBase.x = game.width / 2; cakeBase.y = game.height - cakeBase.height / 2; // Handle dragging the cake base var dragNode = null; game.on('down', function (obj) { dragNode = cakeBase; }); game.on('move', function (obj) { if (dragNode) { var pos = obj.event.getLocalPosition(game); dragNode.x = pos.x; } }); game.on('up', function (obj) { dragNode = null; }); // Create and position the table at the bottom of the screen var table = game.addChild(new Table()); table.x = game.width / 2 - 35; table.y = game.height - table.height / 2 + 1320; table.scale.x = 3; table.scale.y = 2; // Add Mango button to the game var mangoButton = game.addChild(new MangoButton()); mangoButton.x = game.width / 2 + 800; mangoButton.y = game.height - 800; var frogButton = game.addChild(new FrogButton()); frogButton.x = game.width / 2 + 400; frogButton.y = game.height - 150; var fishButton = game.addChild(new FishButton()); fishButton.x = game.width / 2 + 800; fishButton.y = game.height - 1000; var mouseButton = game.addChild(new MouseButton()); mouseButton.x = game.width / 2 - 800; mouseButton.y = game.height - 800; var pineappleButton = game.addChild(new PineappleButton()); pineappleButton.x = game.width / 2; pineappleButton.y = game.height - 150; // Add whipped cream button to the game var whippedCreamButton = game.addChild(new WhippedCreamButton()); whippedCreamButton.x = game.width / 2 - 400; whippedCreamButton.y = game.height - 150; var nutellaButton = game.addChild(new NutellaButton()); nutellaButton.x = game.width / 2 - 800; nutellaButton.y = game.height - 150; var blueberryButton = game.addChild(new BlueberryButton()); blueberryButton.x = game.width / 2 + 800; blueberryButton.y = game.height - 150; var raspberryButton = game.addChild(new RaspberryButton()); raspberryButton.x = game.width / 2 - 800; raspberryButton.y = game.height - 150 - 400; var strawberryButton = game.addChild(new StrawberryButton()); strawberryButton.x = game.width / 2 + 800; strawberryButton.y = game.height - 150 - 400; var goodFill = [mangoButton, raspberryButton, nutellaButton, whippedCreamButton, pineappleButton, blueberryButton, strawberryButton]; var badFill = [mouseButton, fishButton, frogButton]; goodFill.forEach(function (i) { i.bonus = 1; i.y = 4000; }); badFill.forEach(function (i) { i.bonus = -1; i.y = 4000; }); function handleButtons(button) { button.interactive = false; //button.zIndex = 1000; //game.children.length - 1; button.y = game.height - currentCakeHeight - button.height / 2 - 400; LK.setScore(LK.getScore() + button.bonus); // Move the pressed button to a layer above the latest cake layer if (cakeLayers.length > 0) { var latestCakeLayer = cakeLayers[cakeLayers.length - 1]; if (button.parent.getChildIndex(latestCakeLayer) < button.parent.children.length - 1) { button.parent.setChildIndex(button, button.parent.getChildIndex(latestCakeLayer)); } } } ; var scoreLabel = new Text2('Score: 0', { size: 100, fill: "#ffffff", align: "center" }); scoreLabel.x = game.width / 2; scoreLabel.y = 50; scoreLabel.anchor.set(0.5, 0); LK.gui.top.addChild(scoreLabel); // Game tick event LK.on('tick', function () { scoreLabel.setText('Score: ' + LK.getScore()); // Move cake layers for (var i = cakeLayers.length - 1; i >= 0; i--) { cakeLayers[i].move(); // Remove off-screen cake layers /*if (cakeLayers[i].y > game.height + cakeLayers[i].height) { cakeLayers[i].destroy(); cakeLayers.splice(i, 1); }*/ } // Spawn cake layers and fillings if (LK.ticks % 180 === 0 && currentCakeHeight < 1200) { var newLayer = new CakeLayer(currentCakeHeight); newLayer.x = game.width / 2; newLayer.y = -newLayer.height / 2; cakeLayers.push(newLayer); game.addChild(newLayer); currentCakeHeight += currentCakeHeight == 0 ? 1 : 150; } });
===================================================================
--- original.js
+++ change.js
@@ -172,21 +172,8 @@
/****
* Game Code
****/
-var scoreLabel = new Text2('Score: 0', {
- size: 100,
- fill: "#ffffff",
- align: "center"
-});
-scoreLabel.x = game.width / 2;
-scoreLabel.y = 50;
-scoreLabel.anchor.set(0.5, 0);
-LK.gui.top.addChild(scoreLabel);
-// Update score label in the game tick event
-LK.on('tick', function () {
- scoreLabel.setText('Score: ' + LK.getScore());
-});
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
@@ -352,10 +339,20 @@
}
}
}
;
+var scoreLabel = new Text2('Score: 0', {
+ size: 100,
+ fill: "#ffffff",
+ align: "center"
+});
+scoreLabel.x = game.width / 2;
+scoreLabel.y = 50;
+scoreLabel.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreLabel);
// Game tick event
LK.on('tick', function () {
+ scoreLabel.setText('Score: ' + LK.getScore());
// Move cake layers
for (var i = cakeLayers.length - 1; i >= 0; i--) {
cakeLayers[i].move();
// Remove off-screen cake layers
a dark wooden kitchen table with a serving plate.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A bakery wall with shelves full of cakes, toppings, berries. Rich game illustration style.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A swirl of delicious whipped cream. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A small pile of delicious blueberries. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A few delicious red raspberries, cleaned.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Lush, mature strawberries, cleaned and slices and ready for use in bakery or for decoration.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A little green frog. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A cute little goldfish. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A golden star.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A pile of pieces of milk chocolate. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.