Code edit (1 edits merged)
Please save this source code
User prompt
Display the baitTimer under the bait display, but only display it when bait is not at 20
User prompt
Every second, decrease `baitTimer`, when it reaches 0, increase `bait` by one, and set `baitTimer` to 300. Don't decrease `baitTimer` when bait is at 20. Use LK.setInterval, but do not clear the timer
Code edit (13 edits merged)
Please save this source code
User prompt
Updated the bait counter and disable the cast button when out of bait
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (15 edits merged)
Please save this source code
User prompt
the fishIcon get stuck on the screen when sell, fix that
User prompt
Please fix the bug: 'Uncaught Error: getChildAt: Supplied index 2 does not exist in the child list, or the supplied DisplayObject must be a child of the caller' in or related to this line: 'tabLayout.fishes.removeChildAt(2);' Line Number: 433
User prompt
the fishIcon get stuck on the screen when sell, fix that
Code edit (1 edits merged)
Please save this source code
Code edit (7 edits merged)
Please save this source code
User prompt
properly refresh the list redrawing the list rather than pressing the fishes tab
User prompt
Add sell button for each fish in the fishes tab
Code edit (9 edits merged)
Please save this source code
User prompt
Add a interval timer when a fish get added.
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Replace the 'Your fishes' with the capacity of the fishes, draw the corresponding fish and add a separator asset for each fish.
Code edit (10 edits merged)
Please save this source code
User prompt
Display the `gameStatus.fishes` fish, rate and value when the fishes tab is shown.
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ var Button = Container.expand(function (onPress, config) { var self = Container.call(this); self.onPress = onPress; self.isPressed = false; self.id = config.id; if (config.size) { var buttonWidth = config.size; var buttonHeight = config.size; } else { var buttonWidth = config.width; var buttonHeight = config.height; } var textSize = Math.max(buttonWidth, buttonHeight); var background = self.attachAsset('buttonBackground', { width: buttonWidth, height: buttonHeight }); var backgroundPressed = self.attachAsset('buttonBackgroundPressed', { visible: false, width: buttonWidth, height: buttonHeight }); self.text = config.text; var buttonText = new Text2(self.text, { size: textSize / 5, fill: 0xFFFFFF, weight: 700, stroke: 0x000000, strokeThickness: 20 }); buttonText.anchor.set(0.5, 0.5); buttonText.x = buttonWidth / 2; buttonText.y = buttonHeight / 2; self.addChild(buttonText); self.down = function (x, y, obj) { if (self.onPress) { // If onPress return false, then don't press down the button if (!self.onPress(self)) { return; } } self.isPressed = true; background.visible = false; backgroundPressed.visible = true; currentButton = self; }; self.up = function (x, y, obj) { self.isPressed = false; background.visible = true; backgroundPressed.visible = false; }; return self; }); var Fish = Container.expand(function (fishType) { var self = Container.call(this); // Store fish type self.fishType = fishType || 'blueFish'; // Create fish visual var fishGraphics = self.attachAsset(self.fishType, { anchorX: 0.5, anchorY: 0.5 }); // Swimming properties self.speed = 2 + Math.random() * 3; // Random speed between 2-5 self.direction = Math.random() * Math.PI * 2; // Random initial direction self.turnSpeed = 0.02 + Math.random() * 0.03; // How fast fish can turn self.targetDirection = self.direction; self.wobbleAmount = 0.1 + Math.random() * 0.2; // Slight wobble for natural movement self.wobbleSpeed = 0.05 + Math.random() * 0.05; self.wobbleOffset = Math.random() * Math.PI * 2; // Boundaries with margin self.margin = 100; // Initialize position self.randomPosition = function () { self.x = self.margin + Math.random() * (2048 - self.margin * 2); self.y = self.margin + Math.random() * (2732 - self.margin * 2); }; self.format = function () { if (self.rate.toFixed(2) == 0) { var rate = "frame"; } else { var rate = formatTime(self.rate, 2); } return formatMoney(self.value) + " every " + rate; }; self.destroy = function () { if (self.incomeTimer) { LK.clearInterval(self.incomeTimer); self.incomeTimer = null; } Container.prototype.destroy.call(self); }; self.update = function () { if (!self.hasAI) { return; } // Add wobble to movement var wobble = Math.sin(LK.ticks * self.wobbleSpeed + self.wobbleOffset) * self.wobbleAmount; // Gradually turn towards target direction var directionDiff = self.targetDirection - self.direction; // Normalize angle difference to -PI to PI while (directionDiff > Math.PI) { directionDiff -= Math.PI * 2; } while (directionDiff < -Math.PI) { directionDiff += Math.PI * 2; } self.direction += directionDiff * self.turnSpeed; // Move fish self.x += Math.cos(self.direction + wobble) * self.speed; self.y += Math.sin(self.direction + wobble) * self.speed; // Face the direction of movement fishGraphics.rotation = self.direction; // Check boundaries and change direction var turnAwayForce = 0.1; var boundaryDistance = 200; if (self.x < self.margin + boundaryDistance) { self.targetDirection = 0; // Turn right } else if (self.x > 2048 - self.margin - boundaryDistance) { self.targetDirection = Math.PI; // Turn left } if (self.y < self.margin + boundaryDistance) { self.targetDirection = Math.PI / 2; // Turn down } else if (self.y > 2732 - self.margin - boundaryDistance - 200) { self.targetDirection = -Math.PI / 2; // Turn up } // Occasionally change direction randomly if (Math.random() < 0.005) { // 0.5% chance per frame self.targetDirection = Math.random() * Math.PI * 2; } // Keep fish within bounds (hard limit) self.x = Math.max(self.margin, Math.min(2048 - self.margin, self.x)); self.y = Math.max(self.margin, Math.min(2732 - self.margin, self.y)); }; return self; }); var GameStatus = Container.expand(function () { var self = Container.call(this); self.money = 0; self.fishes = []; self.bait = 20; self.baitTimer = 300; self.fishValue = 1; self.fishMax = 3; self.fishRate = 3.6e+6; self.fishesAtCapacity = function () { return self.fishes.length >= self.fishMax; }; self.formatMoney = function () { if (self.money === Infinity) { return "$Infinity"; } var str = "$"; if (self.money >= 1000) { suffixes = ["K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "De"]; var digits = countNumber(self.money); var sf = suffixes[Math.floor((digits - 1) / 3) - 1]; if (sf === undefined) { str += String(self.money.toExponential(2)); } else { str += (self.money / Math.pow(10, Math.floor((digits - 1) / 3) * 3)).toFixed(2) + sf; } } else { str += self.money.toFixed(2); } return str; }; self.updateMoney = function () { if (self.children.length > 0) { self.removeChildAt(0); } var moneyText = new Text2(self.formatMoney(), { size: 125, fill: 0xC7FFC7, weight: 750 }); moneyText.y = 50; moneyText.x = 50; self.addChildAt(moneyText, 0); }; self.updateMoney(); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x83C7FF }); /**** * Game Code ****/ function countNumber(number) { return Math.floor(Math.log10(Math.abs(number))) + 1; } function formatTime(time, point) { time = time / 1000; var str = ""; if (time > 3600) { str += String((time / 3600).toFixed(point)) + " hour"; } else if (time > 60) { str += String((time / 60).toFixed(point)) + " minute"; } else { str += String(time.toFixed(point)) + " second"; } if (time != 1) { str += "s"; } return str; } function formatMoney(money) { if (money === Infinity) { return "$Infinity"; } var str = "$"; if (money >= 1000) { var suffixes = ["K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "De"]; var digits = countNumber(money); var sf = suffixes[Math.floor((digits - 1) / 3) - 1]; if (sf === undefined) { str += String(money.toExponential(2)); } else { str += (money / Math.pow(10, Math.floor((digits - 1) / 3) * 3)).toFixed(2) + sf; } } else { str += money.toFixed(2); } return str; } function normalRandom(min, max, skew) { skew = skew | 1; var u = 0, v = 0; while (u === 0) { u = Math.random(); } while (v === 0) { v = Math.random(); } var num = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v); num = num / 10.0 + 0.5; if (num > 1 || num < 0) { num = normalRandom(min, max, skew); } else { num = Math.pow(num, skew); num *= max - min; num += min; } return num; } var shine; var shine2; var gameStatus = new GameStatus(); var tabLayout = {}; var fishTypes = ['blueFish', 'greenFish', 'pinkFish', 'redFish']; function addFish(fish) { gameStatus.caughtFish.scaleX = 1; gameStatus.caughtFish.scaleY = 1; fish.randomPosition(); gameStatus.fishes.push(fish); fishLayer.addChild(fish); // Add interval timer for this fish fish.incomeTimer = LK.setInterval(function () { gameStatus.money += fish.value; gameStatus.updateMoney(); }, fish.rate); } function randomFish(value, rate) { if (value === undefined) { var r = 0; var v = 0; } else { var r = normalRandom(rate - 900 * 1000, rate + 900 * 1000); var v = Number(normalRandom(value / 2, value * 2).toFixed(2)); if (r < 0) { r = 0; } } var randomType = fishTypes[Math.floor(Math.random() * fishTypes.length)]; var fish = new Fish(randomType); fish.value = v; fish.rate = r; return fish; } // Initialise the cast screen var castLayer = new Container(); castLayer.visible = false; castLayer.attachAsset('castBackground', { alpha: 0.5 }); var text = new Text2("You caught a fish!", { size: 150, fill: 0xFFFFFF, weight: 700, stroke: 0x000000, strokeThickness: 20 }); text.x = 2048 / 2 - text.width / 2; text.y = 2732 / 2 - 900; castLayer.addChild(text); var keepButton = new Button(function () { game.removeChild(gameStatus.caughtFish); gameStatus.caughtFish.hasAI = true; addFish(gameStatus.caughtFish); shine.destroy(); shine2.destroy(); fishText.destroy(); fishText = undefined; castLayer.visible = false; gameStatus.caughtFish = undefined; }, { text: "Keep", width: 818, height: 335 }); keepButton.x = 2048 / 2 - 818 - 50; keepButton.y = 2732 / 2 + 900; castLayer.addChild(keepButton); var fishText; var sellButton = new Button(function () { game.removeChild(gameStatus.caughtFish); shine.destroy(); shine2.destroy(); fishText.destroy(); fishText = undefined; castLayer.visible = false; gameStatus.money += gameStatus.caughtFish.value; gameStatus.updateMoney(); gameStatus.caughtFish = undefined; }, { text: "Sell", width: 818, height: 335 }); sellButton.x = 2048 / 2 + 50; sellButton.y = 2732 / 2 + 900; castLayer.addChild(sellButton); var passiveIncome = new Text2("Passive Income:", { size: 125, fill: 0xFFFFFF, weight: 700, stroke: 0x000000, strokeThickness: 18 }); passiveIncome.x = 2048 / 2; passiveIncome.y = 2732 / 2 - 800 + text.height; passiveIncome.anchor.set(0.5, 0); castLayer.addChild(passiveIncome); game.addChild(castLayer); function cast() { if (gameStatus.caughtFish === undefined) { gameStatus.caughtFish = randomFish(gameStatus.fishValue, gameStatus.fishRate); } shine = LK.getAsset('shine', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.8, scaleY: 1.8, alpha: 0.7 }); shine.x = 2048 / 2; shine.y = 2732 / 2 + 300; shine.rotation = 0; castLayer.addChild(shine); shine2 = LK.getAsset('shine', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5, alpha: 0.5 }); shine2.x = 2048 / 2; shine2.y = 2732 / 2 + 300; shine2.rotation = 0; castLayer.addChild(shine2); // Render fish on cast screen gameStatus.caughtFish.x = 2048 / 2; gameStatus.caughtFish.y = 2732 / 2 + 300; gameStatus.caughtFish.scaleX = 2; gameStatus.caughtFish.scaleY = 2; gameStatus.caughtFish.hasAI = false; castLayer.addChild(gameStatus.caughtFish); fishText = new Text2(gameStatus.caughtFish.format(), { size: 125, fill: 0xC7FFC7, weight: 750 }); fishText.x = 2048 / 2; fishText.y = 2732 / 2 - 790 + passiveIncome.height + text.height; fishText.anchor.set(0.5, 0); castLayer.addChild(fishText); // Display the cast screen castLayer.visible = true; hideTab(); } function hideTab() { tabBackground.visible = false; if (currTab) { tabLayout[currTab].visible = false; currTab = null; } } var currTab; function tabPressed(button) { if (castLayer.visible) { return false; } tabBackground.visible = true; if (currTab == button.id) { return; } if (currTab) { tabLayout[currTab].visible = false; } currTab = button.id; tabLayout[button.id].visible = true; if (currTab == 'fishes') { var cap = gameStatus.fishes.length + '/' + gameStatus.fishMax; capText.setText(cap); // Display each fish var yOffset = 230; for (var i = 0; i < gameStatus.fishes.length; i++) { var fish = gameStatus.fishes[i]; // Fish icon var fishIcon = LK.getAsset(fish.fishType, {}); fishIcon.scale.set(0.75); fishIcon.x = 100; fishIcon.y = yOffset; tabLayout.fishes.addChild(fishIcon); // Fish info text var fishInfo = new Text2(fish.format(), { size: 60, fill: 0xAAAAFF, weight: 600 }); fishInfo.anchor.set(0, 0.5); fishInfo.x = 100; fishInfo.y = yOffset + fishIcon.height + 22; tabLayout.fishes.addChild(fishInfo); yOffset += fishIcon.height + fishInfo.height + 25; // Add separator if (i < gameStatus.fishes.length - 1) { var separator = LK.getAsset('separator', {}); separator.x = 85; separator.y = yOffset; tabLayout.fishes.addChild(separator); yOffset += separator.height + 20; } } } return true; } var tabLayer = new Container(); var tabBackground = new Container(); var tabElements = new Container(); var castButton = new Button(cast, { width: 818, height: 335, text: "Cast" }); castButton.x = (946 - 818) / 2; castButton.y = 75; castButton.visible = false; tabLayout.cast = tabElements.addChild(castButton); tabLayout.fishes = tabElements.addChild(new Container()); tabLayout.shop = tabElements.addChild(new Container()); var fishesHeader = new Text2('Capacity: ', { size: 100, fill: 0xFFFFFF, weight: 700, strokeThickness: 14 }); fishesHeader.x = 100; fishesHeader.y = 70; tabLayout.fishes.addChild(fishesHeader); var capText = new Text2("", { size: 100, fill: 0xFFFFFF, weight: 700 }); capText.x = 100 + fishesHeader.width; capText.y = 85; tabLayout.fishes.addChild(capText); tabLayout.fishes.visible = false; tabLayer.addChild(tabBackground); tabLayer.addChild(tabElements); tabBackground.x = 2048 - 976; tabBackground.y = 25; tabElements.x = 2048 - 976; tabElements.y = 25; tabBackground.visible = false; tabBackground.attachAsset('tabBackgroundTL', {}); tabBackground.attachAsset('tabBackgroundBL', { y: 1155 }); tabBackground.attachAsset('tabBackgroundBR', { x: 473, y: 1155 }); tabBackground.attachAsset('tabBackgroundTR', { x: 473 }); var castTab = new Button(tabPressed, { size: 300, text: "cast", id: "cast" }); castTab.x = 2048 - 325 * 3; castTab.y = 2732 - 325; tabLayer.addChild(castTab); var fishesTab = new Button(tabPressed, { size: 300, text: "fishes", id: "fishes" }); fishesTab.x = 2048 - 325 * 2; fishesTab.y = 2732 - 325; tabLayer.addChild(fishesTab); var shopTab = new Button(tabPressed, { size: 300, text: "shop", id: "shop" }); shopTab.x = 2048 - 325; shopTab.y = 2732 - 325; tabLayer.addChild(shopTab); var fishLayer = new Container(); game.addChild(fishLayer); var fishes = []; game.attachAsset('waterBackground', {}); game.addChild(fishLayer); game.addChild(gameStatus); game.addChild(tabLayer); game.addChild(castLayer); game.update = function () { // Rotate shine clockwise if visible if (shine) { shine.rotation += 0.01; // Rotate clockwise } if (shine2) { shine2.rotation -= 0.0075; // Rotate anti-clockwise, slower } }; game.down = function (x, y, obj) { if (tabBackground.x > x) { hideTab(); } };
===================================================================
--- original.js
+++ change.js
@@ -85,8 +85,15 @@
var rate = formatTime(self.rate, 2);
}
return formatMoney(self.value) + " every " + rate;
};
+ self.destroy = function () {
+ if (self.incomeTimer) {
+ LK.clearInterval(self.incomeTimer);
+ self.incomeTimer = null;
+ }
+ Container.prototype.destroy.call(self);
+ };
self.update = function () {
if (!self.hasAI) {
return;
}
@@ -257,8 +264,13 @@
gameStatus.caughtFish.scaleY = 1;
fish.randomPosition();
gameStatus.fishes.push(fish);
fishLayer.addChild(fish);
+ // Add interval timer for this fish
+ fish.incomeTimer = LK.setInterval(function () {
+ gameStatus.money += fish.value;
+ gameStatus.updateMoney();
+ }, fish.rate);
}
function randomFish(value, rate) {
if (value === undefined) {
var r = 0;
@@ -411,9 +423,9 @@
if (currTab == 'fishes') {
var cap = gameStatus.fishes.length + '/' + gameStatus.fishMax;
capText.setText(cap);
// Display each fish
- var yOffset = 270;
+ var yOffset = 230;
for (var i = 0; i < gameStatus.fishes.length; i++) {
var fish = gameStatus.fishes[i];
// Fish icon
var fishIcon = LK.getAsset(fish.fishType, {});
@@ -428,11 +440,11 @@
weight: 600
});
fishInfo.anchor.set(0, 0.5);
fishInfo.x = 100;
- fishInfo.y = yOffset + fishIcon.height + 20;
+ fishInfo.y = yOffset + fishIcon.height + 22;
tabLayout.fishes.addChild(fishInfo);
- yOffset += fishIcon.height + fishInfo.height + 40;
+ yOffset += fishIcon.height + fishInfo.height + 25;
// Add separator
if (i < gameStatus.fishes.length - 1) {
var separator = LK.getAsset('separator', {});
separator.x = 85;
@@ -464,17 +476,17 @@
weight: 700,
strokeThickness: 14
});
fishesHeader.x = 100;
-fishesHeader.y = 100;
+fishesHeader.y = 70;
tabLayout.fishes.addChild(fishesHeader);
var capText = new Text2("", {
size: 100,
fill: 0xFFFFFF,
weight: 700
});
capText.x = 100 + fishesHeader.width;
-capText.y = 115;
+capText.y = 85;
tabLayout.fishes.addChild(capText);
tabLayout.fishes.visible = false;
tabLayer.addChild(tabBackground);
tabLayer.addChild(tabElements);