User prompt
Switch to Another way to chain `drawSegment` calls without using a callback system is to use a game loop or a tick function that is called at a regular interval by the game engine. Since the LK game engine operates at 60 frames per second, you can leverage the `LK.on('tick', function() {...})` event to schedule and control the drawing of segments.
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Timeout.tick error: Cannot read properties of null (reading 'x')' in this line: 'var lastSegmentEndX = lastSegment.x + Math.cos(lastSegment.rotation) * lastSegment.width;' Line Number: 195
Code edit (1 edits merged)
Please save this source code
User prompt
(!lastSegment || typeof lastSegment.rotation !== 'number') blocks first segment creation
User prompt
Fix rotation is Nan starting from segment index 4. 1. Verify that the `lastSegment` object has been properly initialized with all necessary properties before attempting to calculate its rotation. 2. Check the values of `deltaY` and `deltaX` used in the rotation calculation to ensure they are both valid numbers and that `deltaX` is not zero, as `Math.atan2(0, 0)` would result in an undefined rotation. 3. Trace back to where `deltaY` and `deltaX` are derived from, ensuring that the points or coordinates used in their calculation are correctly set and represent the actual positions of the segments.
Code edit (2 edits merged)
Please save this source code
User prompt
fix pointA.X is still Nan after passing the center
User prompt
fix pointA.X is sometimes Nan
Code edit (7 edits merged)
Please save this source code
User prompt
lastSegmentEndX should take rotation into account
Code edit (1 edits merged)
Please save this source code
User prompt
Fix position of pointA & pointB in drawNextSegment when moveSegments = true : new segments pointA x exeeds previous segment end
User prompt
position of pointA & pointB in drawNextSegment are wrong when moveSegments = true, maybe the slideAmount should be cummulative
User prompt
remove useless code like initSlide
User prompt
remove everything related to ValueIndicator. clean the code
User prompt
remove ValueIndicator without altering segments algorithm
Code edit (1 edits merged)
Please save this source code
User prompt
when moveSegments is true, take into account the slide in drawsegment
User prompt
Ensure the linkage between segments. adjuste to account for the distance that the existing segments have already moved to the left. This would ensure that the new segment is connected to the end of the last segment after it has slid, rather than where it was before sliding.
User prompt
Fix new segments not visible when moveSegments is true
User prompt
maintain the linkage between segments, the starting position of the new segment (`x1`, `y1`) need to be adjusted to account for the distance that the existing segments have already moved to the left. This would ensure that the new segment is connected to the end of the last segment after it has slid, rather than where it was before sliding.
Code edit (2 edits merged)
Please save this source code
User prompt
when moveSegments = true continuously add new stockValuesHistory & graphPoints values
User prompt
continuously add new stockValuesHistory & graphPoints values
===================================================================
--- original.js
+++ change.js
@@ -19,83 +19,8 @@
var segmentGraphics = self.createAsset('segment', 'Graph Segment', 0, 0.5);
// Function to slide the segment to the left
});
// 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.initSlide = function (distance) {
- self.distanceToSlide = distance;
- self.startX = self.x;
- };
- self.updateSlide = function () {
- if (moveSegments) {
- self.x -= slideAmount;
- self.distanceToSlide -= slideAmount;
- if (self.distanceToSlide <= 0) {
- self.distanceToSlide = null;
- }
- }
- };
- 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; // 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 = 0; //11000; // 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 = 1; // 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
- var nextX = self.x;
- console.log('ValueIndicator moving to next position: ' + stockValuesHistory[historyIndex], nextX, 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
- }
- };
- animateY();
- historyIndex++;
- LK.setTimeout(function () {
- updateYPosition();
- }, 1000); // Update position every second and add a new segment
- }
- };
- 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
@@ -154,38 +79,21 @@
// Add 10 other random stock values
for (var i = 0; i < 20; i++) {
stockValuesHistory.push(Math.floor(Math.random() * 100) + 1);
}
-// Instantiate 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 = 0;
-var y = 1366;
-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 = Math.min(segmentWidth * i, game.width / 2); // with limit
+var x, y;
+// Compute the coordinates for the graph based on stockValuesHistory
+for (var i = 0; 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
});
}
-graphPoints.push({
- x: game.width,
- y: y
-});
function addNewStockValues() {
if (moveSegments) {
var newStockValue = Math.floor(Math.random() * 100) + 1;
stockValuesHistory.push(newStockValue);
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.