User prompt
Instead of modifying scorepersec directly, let's create get and set functions for scorepersec and have the upgrades class use those
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'scorePerSec')' in this line: 'game.scorePerSec += self.effectValue;' Line Number: 50
Code edit (2 edits merged)
Please save this source code
User prompt
scorepersecond is not being affected by upgrades
User prompt
output important local variables for debugging
User prompt
Upgrade should modify trhe scorepersec variable
User prompt
move updating the balance of the bank from the persec value to the main LK on tick in the Game class instead of in the upgrade class
User prompt
It seems the add to persec upgrade isn't working
Code edit (6 edits merged)
Please save this source code
User prompt
Fix Bug: 'Timeout.tick error: scoreTxt is not defined' in this line: 'scoreTxt.setText(LK.getScore().toString());' Line Number: 53
Code edit (6 edits merged)
Please save this source code
User prompt
Snowflakes should spawn within 50 px horizontally of the center of the cloud
Code edit (1 edits merged)
Please save this source code
Code edit (9 edits merged)
Please save this source code
User prompt
It's no longer bouncing though
Code edit (5 edits merged)
Please save this source code
User prompt
We need to offset the center of the cloud sway to be centered in the window
Code edit (4 edits merged)
Please save this source code
User prompt
Sway width shoudl determine how far the cloud moves left right, while the speed how fast (with higher speed meaning faster moving)
Code edit (1 edits merged)
Please save this source code
User prompt
Let's make the width and speed of the left right cloud sway into parameters
Code edit (1 edits merged)
Please save this source code
User prompt
That slowed it down, but now it's going way too far up and down.
User prompt
Hmm, it's still bounching up and down way too quickly
User prompt
Ok, great. Now let's slow down the vertical motion a lot, and shift the starting point to be centered horizontally in the window
var Upgrade = Container.expand(function (cost, effectType, effectMode, effectValue, available) { var self = Container.call(this); self.available = available || false; self.purchased = false; var self = Container.call(this); self.available = false; self.purchased = false; var upgradeGraphics = self.createAsset('upgrade', 'Upgrade Graphics', .5, .5); var costText = new Text2('Cost: ' + (cost < 10000 ? Math.round(cost) : cost.toPrecision(2)), { size: 35, fill: '#ffffff', anchor: { x: 0.5, y: 0.5 } }); costText.y = -35; costText.x = -costText.width / 2; self.addChild(costText); var effectDescription = effectType === 'click' ? 'Per Click: ' : 'Per Second: '; effectDescription += effectMode === 'add' ? '+' : 'x'; effectDescription += effectValue !== undefined ? effectValue.toString() : '0'; var effectText = new Text2(effectDescription, { size: 35, fill: '#ffffff', anchor: { x: 0.5, y: 0.5 } }); effectText.y = 35; effectText.x = -effectText.width / 2; self.addChild(effectText); self.cost = cost; self.effectType = effectType; self.effectMode = effectMode; self.effectValue = effectValue; self.purchased = false; self.on('down', function () { if (!self.purchased && LK.getScore() >= self.cost) { LK.setScore(LK.getScore() - self.cost); if (self.effectType === 'click') { if (self.effectMode === 'add') { cloud.clickValue += self.effectValue; } else { cloud.clickValue *= self.effectValue; } } else { if (self.effectMode === 'add') { self.parent.setScorePerSec(self.parent.getScorePerSec() + self.effectValue); } else if (self.effectMode === 'multiply') { game.setScorePerSec(game.getScorePerSec() * self.effectValue); } } self.purchased = true; self.removeChild(self); self.parent.children.forEach(function (upgrade, index) { if (upgrade.purchased) { self.parent.removeChild(upgrade); } else { upgrade.y -= 150; } }); } }); }); var TabButton = Container.expand(function (label, onClick) { var self = Container.call(this); var buttonGraphics = self.createAsset('tabButton', 'Tab Button Graphics', 0, 0.5); buttonGraphics.tint = 0x777777; var buttonText = new Text2(label, { size: 50, fill: '#ffffff', anchor: { x: 0.5, y: 0.5 }, align: 'center' }); self.addChild(buttonText); self.on('down', function () { onClick(); }); }); var TabSystem = Container.expand(function () { var self = Container.call(this); self.tabs = {}; self.currentTab = ''; self.addTab = function (name, content) { self.tabs[name] = content; content.visible = false; self.addChild(content); }; self.switchTab = function (name) { if (self.tabs[self.currentTab]) { self.tabs[self.currentTab].visible = false; self.tabs[self.currentTab].children.forEach(function (child) { child.visible = false; }); } self.currentTab = name; self.tabs[name].visible = true; self.tabs[name].children.forEach(function (child) { child.visible = true; if (child instanceof TabButton) { child.children[0].tint = 0xFFFFFF; } }); Object.values(self.tabs).forEach(function (content) { if (content !== self.tabs[name]) { content.children.forEach(function (child) { if (child instanceof TabButton) { child.children[0].tint = 0x777777; } }); } }); }; }); var Snowflake = Container.expand(function () { var self = Container.call(this); var snowflakeGraphics = self.createAsset('snowflake', 'Snowflake Graphics', .5, .5); self.rotationSpeed = (Math.random() - 0.5) * 0.02; self.swayMagnitude = Math.random() * 10; self.swaySpeed = (Math.random() - 0.5) * 0.03; self.sizeFactor = Math.random() * 0.75 + 0.25; self.scale.set(self.sizeFactor); self.swayMagnitude = self.sizeFactor * 5; self.swayOffset = Math.random() * Math.PI * 2; self.update = function () { self.y += 0.5 + Math.random(); self.rotation += self.rotationSpeed; self.x += Math.sin(LK.ticks * self.swaySpeed * 2 + self.swayOffset) * (self.swayMagnitude * 0.25); }; self.collect = function () { LK.setScore(Math.round(LK.getScore() + 1)); scoreTxt.setText(LK.getScore().toString()); self.destroy(); }; }); var Cloud = Container.expand(function () { var self = Container.call(this); self.visible = true; var cloudGraphics = self.createAsset('cloud', 'Cloud Graphics', .5, .5); cloudGraphics.alpha = 1; self.spawnRate = 1; self.clickValue = 1; self.bounceSpeed = 0.02; self.bounceHeight = 15; self.bounceOffset = 0; self.swayWidth = 300; self.swaySpeed = .003; self.update = function () { self.bounceOffset += self.bounceSpeed; self.y = 2732 / 2 + Math.sin(self.bounceOffset) * self.bounceHeight; self.x = 1024 + Math.sin(LK.ticks * self.swaySpeed) * self.swayWidth; }; self.spawnSnowflake = function () { var snowflake = new Snowflake(); var cloudWidth = cloudGraphics.width; snowflake.x = this.x + (Math.random() - 0.5) * 800; snowflake.y = self.y + cloudGraphics.height / 2 + Math.sin(self.bounceOffset) * self.bounceHeight; snowflake.rotation = Math.random() * Math.PI * 2; return snowflake; }; }); var Game = Container.expand(function () { var self = Container.call(this); self._scorePerSec = 0; self.getScorePerSec = function () { return self._scorePerSec; }; self.setScorePerSec = function (value) { self._scorePerSec = value; }; LK.stageContainer.setBackgroundColor(0x000000); var tabSystem = new TabSystem(); var upgradesTab = new Container(); var upgradesConfig = [{ cost: 10, effectType: 'perSecond', effectMode: 'add', effectValue: 1 }, { cost: 25, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }, { cost: 100, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }, { cost: 500, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }, { cost: 1000, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }, { cost: 2500, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }, { cost: 10000, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }, { cost: 20000, effectType: 'perSecond', effectMode: 'multiply', effectValue: 2 }]; var columns = 3; var upgradeWidth = 400; var upgradeHeight = 200; var upgradeSpacing = 200; var upgradePadding = 200; var startX = 0 - upgradeWidth / 2; var startY = 800; upgradesConfig.forEach(function (config, index) { var upgrade = upgradesTab.addChild(new Upgrade(config.cost, config.effectType, config.effectMode, config.effectValue)); var column = index % columns + 1; var row = Math.floor(index / columns); upgrade.x = startX + column * (upgradeWidth + upgradeSpacing); upgrade.y = startY + row * (upgradeHeight + upgradePadding); }); var scoreTxt; function initializeScoreText() { scoreTxt = new Text2('', { size: 150, fill: '#ffffff', anchor: { x: 0.5, y: 0.5 } }); scoreTxt.x = -scoreTxt.width / 2; LK.gui.topCenter.addChild(scoreTxt); scoreTxt.y = LK.gui.topCenter.height / 2; } initializeScoreText(); var cloudTabButton = new TabButton('Cloud', function () { tabSystem.switchTab('Cloud'); }); var upgradesTabButton = new TabButton('Upgrades', function () { tabSystem.switchTab('Upgrades'); }); cloudTabButton.x = -cloudTabButton.width; cloudTabButton.y = scoreTxt.y + scoreTxt.height + 50; upgradesTabButton.x = 10; upgradesTabButton.y = cloudTabButton.y; LK.gui.topCenter.addChild(cloudTabButton); LK.gui.topCenter.addChild(upgradesTabButton); self.addChild(tabSystem); var snowflakes = []; var cloudTab = new Container(); var cloud = cloudTab.addChild(new Cloud()); cloud.x = 2048 / 2; cloud.y = 2732 / 2; tabSystem.addTab('Upgrades', upgradesTab); tabSystem.addTab('Cloud', cloudTab); tabSystem.switchTab('Cloud'); cloud.on('down', function (obj) { for (var i = 0; i < cloud.spawnRate; i++) { var snowflake = cloud.spawnSnowflake(); snowflakes.push(snowflake); self.addChild(snowflake); } LK.setScore(Math.round(LK.getScore() + cloud.clickValue)); scoreTxt.setText(Math.round(LK.getScore()).toString()); }); var tickCount = 0; LK.on('tick', function () { console.log('Score per second:', self.getScorePerSec(), 'Current score:', LK.getScore()); LK.setScore(Math.round(LK.getScore() + self.getScorePerSec())); scoreTxt.setText(LK.getScore().toString()); upgradesTab.children.forEach(function (upgrade) { if (upgrade.purchased && upgrade.effectType === 'perSecond') { if (upgrade.effectMode === 'add') { LK.setScore(Math.round(LK.getScore() + upgrade.effectValue)); } else if (upgrade.effectMode === 'multiply') { LK.setScore(Math.round(LK.getScore() * upgrade.effectValue)); } scoreTxt.setText(LK.getScore().toString()); } }); cloud.update(); for (var i = snowflakes.length - 1; i >= 0; i--) { snowflakes[i].update(); if (snowflakes[i].y > 2732 + snowflakes[i].height) { snowflakes[i].destroy(); snowflakes.splice(i, 1); } } scoreTxt.setText(LK.getScore().toString()); }); });
===================================================================
--- original.js
+++ change.js
@@ -46,11 +46,11 @@
cloud.clickValue *= self.effectValue;
}
} else {
if (self.effectMode === 'add') {
- self.parent.scorePerSec += self.effectValue;
+ self.parent.setScorePerSec(self.parent.getScorePerSec() + self.effectValue);
} else if (self.effectMode === 'multiply') {
- game.scorePerSec *= self.effectValue;
+ game.setScorePerSec(game.getScorePerSec() * self.effectValue);
}
}
self.purchased = true;
self.removeChild(self);
@@ -165,9 +165,15 @@
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
- self.scorePerSec = 0;
+ self._scorePerSec = 0;
+ self.getScorePerSec = function () {
+ return self._scorePerSec;
+ };
+ self.setScorePerSec = function (value) {
+ self._scorePerSec = value;
+ };
LK.stageContainer.setBackgroundColor(0x000000);
var tabSystem = new TabSystem();
var upgradesTab = new Container();
var upgradesConfig = [{
@@ -271,10 +277,10 @@
scoreTxt.setText(Math.round(LK.getScore()).toString());
});
var tickCount = 0;
LK.on('tick', function () {
- console.log('Score per second:', self.scorePerSec, 'Current score:', LK.getScore());
- LK.setScore(Math.round(LK.getScore() + self.scorePerSec));
+ console.log('Score per second:', self.getScorePerSec(), 'Current score:', LK.getScore());
+ LK.setScore(Math.round(LK.getScore() + self.getScorePerSec()));
scoreTxt.setText(LK.getScore().toString());
upgradesTab.children.forEach(function (upgrade) {
if (upgrade.purchased && upgrade.effectType === 'perSecond') {
if (upgrade.effectMode === 'add') {