User prompt
in moveSegmentsLeft use the global array of segments to move them
User prompt
store a global array of segments. add them in drawSegment()
User prompt
Fix Bug: 'TypeError: segment.slide is not a function' in this line: 'segment.slide();' Line Number: 189
User prompt
instead of moving the segments "externally" in moveSegmentsLeft, use a global boolean "moveSegments" defaulting to false. when `(segment.x + segment.width / 2 >= game.width / 2)` instead of calling moveSegmentsLeft, set moveSegments to true. Also add a slide() function in segments to make them move left when moveSegments is true
User prompt
the move of segments moveSegmentsLeft should be continous like the one of the indicator, currently their move is jerky !
Code edit (1 edits merged)
Please save this source code
User prompt
store a global array of segments. add them in drawSegment(). use them in moveSegmentsLeft to move them in place of `game.children.filter(function (child) {return child instanceof GraphSegment;})`
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Timeout.tick error: GraphSegment is not defined' in this line: 'return child instanceof GraphSegment;' Line Number: 219
User prompt
bug : in moveSegmentsLeft : var segments = game.children.filter(function (child) { return child.assetId === 'segment';}); returns 0 segments !
Code edit (4 edits merged)
Please save this source code
User prompt
bug : moveSegmentsLeft is called but the segments doesn't move to the left
Code edit (3 edits merged)
Please save this source code
User prompt
now when the a segment reaches the center (game.width / 2) make all the segments move to the left
Code edit (3 edits merged)
Please save this source code
User prompt
Fix Bug: 'Timeout.tick error: nextX is not defined' in this line: 'console.log('ValueIndicator moving to next position: ' + stockValuesHistory[historyIndex], nextX, 1366 - targetY);' Line Number: 41
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Timeout.tick error: startGame is not defined' in this line: 'startGame();' Line Number: 252
User prompt
call startGame after 1 sec
Code edit (2 edits merged)
Please save this source code
User prompt
in drawSegment in updateWidth, with should only change when running is true
User prompt
add a global boolean "running". default false. The indicator and graph updates should occur only when running is true. Do not change anything else
Code edit (4 edits merged)
Please save this source code
User prompt
Fix ValueIndicator not moving after start
/****
* Classes
****/
// ValueIndicator class to represent the current value of the stock on the graph
var ValueIndicator = Container.expand(function () {
var self = Container.call(this);
var indicatorGraphics = self.createAsset('valueIndicator', 'Current Stock Value Indicator', 0.5, 0.5);
self.updatePosition = function (price) {
if (!running) {
return;
}
// Position ValueIndicator based on the stock price
self.x = 0; // Start from the left side
self.y = 2732 / 2; // Start from the left side
var targetX = game.width / 2; // Center horizontally
var targetY = game.height / 2; // Center vertically
// Custom tween function to animate the ValueIndicator and update SpecialGraphSegment
var startTime = Date.now();
var duration = 3000; // Duration in milliseconds
var startX = self.x;
var endX = targetX;
var startY = self.y;
var endY = targetY;
var animateX = function animateX() {
var endTime = startTime + duration;
var currentTime = Date.now();
var timeRemaining = endTime - currentTime;
var nextX = startX + (endX - startX) * (1 - timeRemaining / duration);
var nextY = startY + (endY - startY) * (1 - timeRemaining / duration);
self.x = nextX;
if (currentTime < endTime) {
LK.setTimeout(animateX, 16);
}
};
animateX();
// Function to animate the ValueIndicator based on stockValuesHistory
var historyIndex = 0; // Start with the first value in the history
var updateYPosition = function updateYPosition() {
if (historyIndex < stockValuesHistory.length) {
var targetY = game.height / 2 - stockValuesHistory[historyIndex] * 10; // Scale the stock value for display
console.log('ValueIndicator moving to next position: ' + stockValuesHistory[historyIndex], 1366 - targetY);
var startY = self.y;
var duration = 1000; // Duration in milliseconds for Y movement
var startTime = Date.now();
var animateY = function animateY() {
var currentTime = Date.now();
var timeElapsed = currentTime - startTime;
var newY = startY + (targetY - startY) * (timeElapsed / duration);
self.y = newY;
if (timeElapsed < duration) {
LK.setTimeout(animateY, 16);
} else {
self.y = targetY; // Ensure final position is set
historyIndex++;
if (historyIndex < stockValuesHistory.length) {
LK.setTimeout(updateYPosition, 1000); // Continue updating position
}
}
};
animateY();
}
};
updateYPosition();
};
});
// Player class to represent the player's portfolio
var Player = Container.expand(function () {
var self = Container.call(this);
self.balance = 100; // Start with $100
self.stocks = {}; // Object to hold stocks and quantities
self.buyStock = function (stock, quantity) {
var cost = stock.getPrice() * quantity;
if (self.balance >= cost) {
self.balance -= cost;
if (!self.stocks[stock]) {
self.stocks[stock] = 0;
}
self.stocks[stock] += quantity;
}
};
self.sellStock = function (stock, quantity) {
if (self.stocks[stock] && self.stocks[stock] >= quantity) {
self.balance += stock.getPrice() * quantity;
self.stocks[stock] -= quantity;
if (self.stocks[stock] === 0) {
delete self.stocks[stock];
}
}
};
self.getBalance = function () {
return self.balance;
};
self.getPortfolio = function () {
return self.stocks;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
// No need to set backgroundColor since we are adding a background asset
});
/****
* Game Code
****/
// Create and add background asset
var background = game.addChild(LK.getAsset('background', 'Game background', 0.5, 0.5));
background.x = 1024; // Center x position
background.y = 2732 - background.height / 2; // Center y position
// Initialize game elements
var player = new Player();
var stocks = [];
// Initialize stock values history with the first value of $10
var running = false;
var stockValuesHistory = [10];
// Add 10 other random stock values
for (var i = 0; i < 10; i++) {
stockValuesHistory.push(Math.floor(Math.random() * 100) + 1);
}
// Instantiate ValueIndicator
var valueIndicator = new ValueIndicator();
game.addChild(valueIndicator);
// Compute the coordinates for ValueIndicator based on stockValuesHistory
var graphPoints = [];
var graphWidth = game.width;
var segmentWidth = graphWidth / stockValuesHistory.length;
// Set the first graphPoints to match the start coordinates of valueIndicator
var x = valueIndicator.x;
var y = valueIndicator.y;
console.log('valueIndicator start at', {
x: Math.round(x),
y: Math.round(1366 - y)
});
graphPoints.push({
x: x,
y: y
});
// Compute the remaining coordinates
for (var i = 1; i < stockValuesHistory.length; i++) {
x = segmentWidth * i;
y = game.height / 2 - stockValuesHistory[i] * 10; // Scale the stock value for display
graphPoints.push({
x: x,
y: y
});
}
// Draw a sample square using drawGraph
var stockDisplay = []; // Array to hold stock display elements
var balanceText = new Text2('Balance: $' + player.getBalance(), {
size: 50,
fill: "#ffffff"
});
// Instantiate ValueIndicator
var valueIndicator = new ValueIndicator();
balanceText.anchor.set(0.5, 0);
LK.gui.top.addChild(balanceText);
// Update the player's balance display
function updateBalanceDisplay() {
balanceText.setText('Balance: $' + player.getBalance().toFixed(2));
}
// Function to handle buying stocks
function buyStock(stock) {
player.buyStock(stock, 1); // Buy 1 stock for simplicity
updateBalanceDisplay();
}
// Function to handle selling stocks
function sellStock(stock) {
player.sellStock(stock, 1); // Sell 1 stock for simplicity
updateBalanceDisplay();
}
// Main game loop
LK.on('tick', function () {
// Check for game over conditions (e.g., player runs out of money)
if (player.getBalance() <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
function drawSegment(gameRef, x1, y1, x2, y2, delay, callback) {
var segment = gameRef.addChild(LK.getAsset('segment', 'Graph Segment', 0, 0.5));
segment.x = x1;
segment.y = y1;
segment.width = 1;
segment.height = 5;
var deltaX = x2 - x1;
var deltaY = y2 - y1;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
segment.rotation = Math.atan2(deltaY, deltaX);
var startTime = Date.now();
var endTime = startTime + delay * 1000;
var updateWidth = function updateWidth() {
var currentTime = Date.now();
var timeElapsed = currentTime - startTime;
var newWidth = Math.min(timeElapsed / (delay * 1000) * distance, distance);
segment.width = newWidth;
if (currentTime < endTime) {
LK.setTimeout(updateWidth, 16);
} else {
if (typeof callback === 'function') {
callback();
}
}
};
updateWidth();
}
function drawGraph(gameRef, points) {
if (!running) {
return;
}
console.log('Starting to draw graph');
if (points.length < 2) {
console.log('Not enough points to draw a graph segment.');
return;
} // Need at least two points to draw a segment
function drawNextSegment(index) {
if (index < points.length - 1) {
var pointA = points[index];
var pointB = points[index + 1];
console.log('Drawing segment from', {
x: Math.round(pointA.x),
y: Math.round(1366 - pointA.y)
}, 'to', {
x: Math.round(pointB.x),
y: Math.round(1366 - pointB.y)
});
drawSegment(gameRef, pointA.x, pointA.y, pointB.x, pointB.y, 1, function () {
/*console.log('Segment drawn from', {
x: Math.round(pointA.x),
y: Math.round(pointA.y)
}, 'to', {
x: Math.round(pointB.x),
y: Math.round(pointB.y)
});*/
drawNextSegment(index + 1);
});
} else {
console.log('Finished drawing graph');
}
}
drawNextSegment(0);
}
function startGame() {
console.log('Starting the game...');
running = true;
// Add ValueIndicator to the game and update its position
valueIndicator.updatePosition(100); // Example stock price
// Pass the series of points to drawGraph
drawGraph(game, graphPoints);
}
LK.setTimeout(startGame, 1000);
A Technical dark background. Nothing just a gradiant of colors from black to dark blue. Theme : stock market. background
A modern clean empty rectangular button without borders. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
without shadow
a basic empty ui popup with a black background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.