2
\$\begingroup\$

I'm working on my final project, and it is a game made with Java Script in code.org's Game Lab. The code worked fine, and it was not slow at all, but as I worked on it, it became much slower! The character starts lagging and it eventually stops moving at all.

I have tried optimizing it by combining the tasks of a few functions into one function that does it all (the motion of the character) and it seemed to help, but not for long. It worked at first with the createPlatform() function, but now it is just making the code slower as well. The goal of the createPlatform() function is for its parameter to be the number of screens that I want the platform to extend to. It did work, but now it seems to be making the game unplayable. Here is the link to try it out! Any help would be greatly appreciated!

Here is the code:

var max_X = 200;
var min_X = 50;
var min_Y = 330;
var platforms = [];
var centered = false;
var sprite = createSprite(200, 250);
sprite.setAnimation("alienBlue_2");
//sprite.scale = 0.15;


function draw() {
 startGame(2, "gray", "groundBrickPlain.png_1", 4);
 drawSprites();
}

function startGame(levelGravity, backG, platformType, platSize) {
  //var Currentlevel = level;
  // Set the initial velocity and gravity
  sprite.velocityY = 0;
  var gravity = levelGravity;
  // Clear the screen
  background(backG);
  createPlatform(platSize, platformType);

  // Apply gravity
  sprite.velocityY = sprite.velocityY + gravity;

  
  move();
  
  if ((keyDown("tab") && keyDown("right")) && centered){
    chageWorldSpeed(7);         
  }
  
  if (sprite.y >= min_Y) {
    console.log("You died.");
  }
  // Prevent the sprite from falling through the platform
  //dontFall(312, 0);

  // Make the sprite jump
  //jump(-12, "alienBlue_jump_1");
  
  var brickN1 = createSprite(200, 200);
  brickN1.setAnimation("brickNormal");
}

function chageWorldSpeed(speed) {
  for (var p = 0; p < platforms.length; p++) {
      platforms[p].x -= speed;
    }
}
function createPlatform(numScreens, platType) {
  var startX = 0;
  var startY = 380;
  var spacing = 40;
  var numBricks = numScreens* 400;
  
  for (var i = 0; i < numBricks; i += spacing) {
    var platform = createSprite(startX + i, startY);
    platform.setAnimation(platType);
    platform.scale = 0.0333333333333333333333333333;
    platforms.push(platform);
  }
}
function move() {
  // Make the sprite move
  if (keyDown("left")) {
    sprite.velocityX = -5;
    sprite.setAnimation("alienBlue_walk_1_copy_1");
    for (var i = 0; i < platforms.length; i++) {
      platforms[i].x += 5;
    }
  } else if (keyDown("right")) {
    sprite.velocityX = 5;
    sprite.setAnimation("alienBlue_walk_1");
    for (var t = 0; t < platforms.length; t++) {
      platforms[t].x -= 5;
    }
  }else if(keyDown("down")) {
    squat("alienBlue_duck_1");
  }else {
    sprite.velocityX = 0;
    sprite.setAnimation("alienBlue_2");
  }
  if(sprite.x == max_X && keyDown("right")) {
    sprite.velocityX = 0;
    centered = true;
  }
  if (sprite.x == min_X && keyDown("left")) {
    var spriteX = 5;
    sprite.velocityX = 0;
    for (var h = 0; h < platforms.length; h++) {
      platforms[h].x -= spriteX;
  }
  }
  
  var jumped = false;
  for (var m = 0; m < platforms.length; m++) {
    if (keyDown("space")|| (keyDown("up")) && sprite.isTouching(platforms[m])) {
      sprite.setAnimation("alienBlue_jump_1");
      sprite.velocityY = -12;
      jumped = true;
      break;
    }
  }
  if (!jumped) {
        sprite.setAnimation("alienBlue_2"); // Reset animation if no jump occurs
    }
    
  for (var v = 0; v < platforms.length; v++) {
    if (sprite.velocityY > 0 && sprite.isTouching(platforms[v])){
      sprite.velocityY = 0;
      sprite.y = 312;
    }
  }
}
function squat(anim) {
  sprite.setAnimation(anim);
}
function dontFall(yVal, yVelocity) {
  for (var v = 0; v < platforms.length; v++) {
    if (sprite.velocityY > 0 && sprite.isTouching(platforms[v])){
      sprite.velocityY = yVelocity;
      sprite.y = yVal;
    }
  }
}
function blockLeft(xVal) {
  var spriteX = xVal;
  sprite.velocityX = 0;
  for (var h = 0; h < platforms.length; h++) {
    platforms[h].x -= spriteX;
  }
}
\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

If you move each game object individually after the player presses a button your game will become slower and slower.

The fundamental flaw in your code is that you have nested for loops everywhere. Be aware that when one for loops calls another for loop you get a massive slowdown.

Your key press detection should only change the variables for position and speed of the player.

There should be only one function that redraws the world depending on the variables that were changed.

You shouldn't redraw EVERY object when the player presses a key! You can just move the player. If you want to prevent the player moving offscreen you can move the whole world (in a container) in the opposite direction.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.