Code edit (7 edits merged)
Please save this source code
User prompt
Please fix the bug: 'self.children.remove is not a function' in or related to this line: 'self.children.remove(0);' Line Number: 212
Code edit (14 edits merged)
Please save this source code
User prompt
Please fix the bug: '[object Object]addChildAt: The index [object Object] supplied is out of bounds 0' in or related to this line: 'self.addChildAt(moneyText, {}, 0);' Line Number: 210
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Render the caught fish on the cast screen.
Code edit (7 edits merged)
Please save this source code
User prompt
Render the caught fish on the cast screen.
Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'size is not defined' in or related to this line: 'var buttonText = new Text2(self.text, {' Line Number: 43
Code edit (1 edits merged)
Please save this source code
Code edit (4 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'x')' in or related to this line: 'var localPos = game.toLocal(obj.position);' Line Number: 201
User prompt
Hide the `tabBackground` when you tap anywhere else that is not on the `tabLayer`, I recommend using `game.down`
Code edit (14 edits merged)
Please save this source code
User prompt
Hide the `tabBackground` when you tap anywhere else that is not on the `tabLayer`, I recommend using `game.down`
User prompt
Hide the `tabBackground` when you don't tap on `tabLayer`
Code edit (1 edits merged)
Please save this source code
Code edit (6 edits merged)
Please save this source code
User prompt
Please fix the bug: 'papyrus is not defined' in or related to this line: 'papyrus;' Line Number: 134
Code edit (1 edits merged)
Please save this source code
Code edit (5 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, font: "Arial Black", stroke: 0x000000, strokeThickness: textSize / 25 }); 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.x = self.margin + Math.random() * (2048 - self.margin * 2); self.y = self.margin + Math.random() * (2732 - self.margin * 2); self.update = function () { // 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.formatTime = function (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; }; self.stringFish = function (fish) { if (fish.rate.toFixed(2) == 0) { var rate = "frame"; } else { var rate = formatTime(fish.rate, 2); } return formatMoney(fish.value) + " every " + rate; }; 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 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 gameStatus = new GameStatus(); var tabLayout = {}; var fishTypes = ['blueFish', 'greenFish', 'pinkFish', 'redFish']; function addFish(fish) { console.log(gameStatus); gameStatus.fishes.push(fish); fishLayer.addChild(fish); } 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; } var castLayer = new Container(); castLayer.visible = false; castLayer.attachAsset('castBackground', { alpha: 0.5 }); var keepButton = new Button(function () { addFish(gameStatus.caughtFish); castLayer.visible = false; gameStatus.caughtFish = undefined; if (gameStatus.fishDisplay) { gameStatus.fishDisplay.destroy(); gameStatus.fishDisplay = undefined; } if (gameStatus.fishInfoText) { gameStatus.fishInfoText.destroy(); gameStatus.fishInfoText = undefined; } }, { text: "Keep", width: 818, height: 335 }); keepButton.x = 2048 / 2 - 818 / 2; keepButton.y = 2732 / 2 + 500; castLayer.addChild(keepButton); game.addChild(castLayer); function cast() { if (gameStatus.caughtFish === undefined) { gameStatus.caughtFish = randomFish(gameStatus.fishValue, gameStatus.fishRate); } // Display the cast screen castLayer.visible = true; hideTab(); // Display the caught fish if (gameStatus.fishDisplay) { gameStatus.fishDisplay.destroy(); } gameStatus.fishDisplay = new Container(); var caughtFishGraphics = gameStatus.fishDisplay.attachAsset(gameStatus.caughtFish.fishType, { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); gameStatus.fishDisplay.x = 2048 / 2; gameStatus.fishDisplay.y = 2732 / 2 - 200; castLayer.addChild(gameStatus.fishDisplay); // Display fish value if (gameStatus.fishInfoText) { gameStatus.fishInfoText.destroy(); } gameStatus.fishInfoText = new Text2('Value: ' + gameStatus.formatMoney(gameStatus.caughtFish.value), { size: 80, fill: 0xFFFFFF, stroke: 0x000000, strokeThickness: 4 }); gameStatus.fishInfoText.anchor.set(0.5, 0.5); gameStatus.fishInfoText.x = 2048 / 2; gameStatus.fishInfoText.y = 2732 / 2 + 100; castLayer.addChild(gameStatus.fishInfoText); // var sellButton = document.createElement("button") // canvas.innerHTML = "" // keepButton.innerText = "Keep" // if (fishesAtCapacity()) // keepButton.disabled = true // keepButton.onclick = function(){ // game.bait -= 1 // if (name.value.length!=0) // game.caughtFish.name = name.value // addFish(game.caughtFish) // document.getElementById("cast").click() // refresh = true // game.caughtFish = undefined // } // sellButton.innerText = "Sell" // sellButton.onclick = function(){ // game.bait -= 1 // pointSet(game.points+game.caughtFish.value) // document.getElementById("cast").click() // refresh = true // game.caughtFish = undefined // } } function hideTab() { tabBackground.visible = false; 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') { console.log(fishes); } 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()); 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(tabLayer); game.addChild(castLayer); game.down = function (x, y, obj) { if (tabBackground.x > x) { hideTab(); } };
===================================================================
--- original.js
+++ change.js
@@ -37,15 +37,18 @@
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;
- if (self.onPress) {
- self.onPress(self);
- }
};
self.up = function (x, y, obj) {
self.isPressed = false;
background.visible = true;
@@ -116,8 +119,64 @@
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.formatTime = function (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;
+ };
+ self.stringFish = function (fish) {
+ if (fish.rate.toFixed(2) == 0) {
+ var rate = "frame";
+ } else {
+ var rate = formatTime(fish.rate, 2);
+ }
+ return formatMoney(fish.value) + " every " + rate;
+ };
+ return self;
+});
/****
* Initialize Game
****/
@@ -127,16 +186,151 @@
/****
* Game Code
****/
+function countNumber(number) {
+ return Math.floor(Math.log10(Math.abs(number))) + 1;
+}
+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 gameStatus = new GameStatus();
var tabLayout = {};
+var fishTypes = ['blueFish', 'greenFish', 'pinkFish', 'redFish'];
+function addFish(fish) {
+ console.log(gameStatus);
+ gameStatus.fishes.push(fish);
+ fishLayer.addChild(fish);
+}
+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;
+}
+var castLayer = new Container();
+castLayer.visible = false;
+castLayer.attachAsset('castBackground', {
+ alpha: 0.5
+});
+var keepButton = new Button(function () {
+ addFish(gameStatus.caughtFish);
+ castLayer.visible = false;
+ gameStatus.caughtFish = undefined;
+ if (gameStatus.fishDisplay) {
+ gameStatus.fishDisplay.destroy();
+ gameStatus.fishDisplay = undefined;
+ }
+ if (gameStatus.fishInfoText) {
+ gameStatus.fishInfoText.destroy();
+ gameStatus.fishInfoText = undefined;
+ }
+}, {
+ text: "Keep",
+ width: 818,
+ height: 335
+});
+keepButton.x = 2048 / 2 - 818 / 2;
+keepButton.y = 2732 / 2 + 500;
+castLayer.addChild(keepButton);
+game.addChild(castLayer);
+function cast() {
+ if (gameStatus.caughtFish === undefined) {
+ gameStatus.caughtFish = randomFish(gameStatus.fishValue, gameStatus.fishRate);
+ }
+ // Display the cast screen
+ castLayer.visible = true;
+ hideTab();
+ // Display the caught fish
+ if (gameStatus.fishDisplay) {
+ gameStatus.fishDisplay.destroy();
+ }
+ gameStatus.fishDisplay = new Container();
+ var caughtFishGraphics = gameStatus.fishDisplay.attachAsset(gameStatus.caughtFish.fishType, {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 2,
+ scaleY: 2
+ });
+ gameStatus.fishDisplay.x = 2048 / 2;
+ gameStatus.fishDisplay.y = 2732 / 2 - 200;
+ castLayer.addChild(gameStatus.fishDisplay);
+ // Display fish value
+ if (gameStatus.fishInfoText) {
+ gameStatus.fishInfoText.destroy();
+ }
+ gameStatus.fishInfoText = new Text2('Value: ' + gameStatus.formatMoney(gameStatus.caughtFish.value), {
+ size: 80,
+ fill: 0xFFFFFF,
+ stroke: 0x000000,
+ strokeThickness: 4
+ });
+ gameStatus.fishInfoText.anchor.set(0.5, 0.5);
+ gameStatus.fishInfoText.x = 2048 / 2;
+ gameStatus.fishInfoText.y = 2732 / 2 + 100;
+ castLayer.addChild(gameStatus.fishInfoText);
+ // var sellButton = document.createElement("button")
+ // canvas.innerHTML = ""
+ // keepButton.innerText = "Keep"
+ // if (fishesAtCapacity())
+ // keepButton.disabled = true
+ // keepButton.onclick = function(){
+ // game.bait -= 1
+ // if (name.value.length!=0)
+ // game.caughtFish.name = name.value
+ // addFish(game.caughtFish)
+ // document.getElementById("cast").click()
+ // refresh = true
+ // game.caughtFish = undefined
+ // }
+ // sellButton.innerText = "Sell"
+ // sellButton.onclick = function(){
+ // game.bait -= 1
+ // pointSet(game.points+game.caughtFish.value)
+ // document.getElementById("cast").click()
+ // refresh = true
+ // game.caughtFish = undefined
+ // }
+}
function hideTab() {
tabBackground.visible = false;
tabLayout[currTab].visible = false;
currTab = null;
}
var currTab;
function tabPressed(button) {
+ if (castLayer.visible) {
+ return false;
+ }
tabBackground.visible = true;
if (currTab == button.id) {
return;
}
@@ -144,21 +338,27 @@
tabLayout[currTab].visible = false;
}
currTab = button.id;
tabLayout[button.id].visible = true;
+ if (currTab == 'fishes') {
+ console.log(fishes);
+ }
+ return true;
}
var tabLayer = new Container();
var tabBackground = new Container();
var tabElements = new Container();
-var castButton = new Button(null, {
+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());
tabLayer.addChild(tabBackground);
tabLayer.addChild(tabElements);
tabBackground.x = 2048 - 976;
tabBackground.y = 25;
@@ -202,24 +402,12 @@
tabLayer.addChild(shopTab);
var fishLayer = new Container();
game.addChild(fishLayer);
var fishes = [];
-var fishTypes = ['blueFish', 'greenFish', 'pinkFish', 'redFish'];
-var addFishButton = new Button(function () {
- var randomType = fishTypes[Math.floor(Math.random() * fishTypes.length)];
- var newFish = new Fish(randomType);
- fishes.push(newFish);
- fishLayer.addChild(newFish);
-}, {
- size: 300,
- text: "Add Fish"
-});
-addFishButton.x = 50;
-addFishButton.y = 2732 - 325;
game.attachAsset('waterBackground', {});
game.addChild(fishLayer);
-game.addChild(addFishButton);
game.addChild(tabLayer);
+game.addChild(castLayer);
game.down = function (x, y, obj) {
if (tabBackground.x > x) {
hideTab();
}