CSD Unit 3 Lesson 24

Link to the project or level: [Game Lab - Code.org]
What I expect to happen: [Line 53 catch the isTouching boolean and act accordingly]
What actually happens: [The second iteration gives an error ERROR: Line: 53: TypeError: Cannot read properties of null (reading ‘x’). ]
What I’ve tried: [I stepped through using the debugger. I put a console.log for both the sprites coordinates, they have non-null values, so not sure what is going on here ]

Found the source of the bug. But could use help on why that is an issue.
Commenting the burger.setCollider(“circle”); on line 17 removes the error.
Reading on why and where setCollider could be causing this; could use help on that.

you have 2 issues one of them is you haven’t fulfilled the parameters for the collider .setCollider(“circle”, offsetX, offsetY, radius) should be the parameters you fill in.
the second issue happens when you set your eagle Y velocity to infinity durring the first frame of your program since it tries to calculate if it’s touching another sprite by using the velocity it’ll obviously not return a number since Infinity and NaN are not apart of the number class exclusively, this also explains the double music start as well from your program I’ve revised your code to be more safe instead of using World.frameRate directly we’ll be using delta

var twins = createSprite(330,234);
twins.setAnimation("twintowers.png_1");
var burning = createSprite(80,285);
burning.scale = 1.4;
burning.setAnimation("burningbuilding_1.png_1");
var eagle = createSprite(200, 100);
eagle.setAnimation("eagle_right");
eagle.scale = 0.3;
var missile1 = createSprite(-50,randomNumber(0,400));
var missile2 = createSprite(randomNumber(0,400),-50);
var missile3 = createSprite(450,randomNumber(0,400));
var missile4 = createSprite(randomNumber(0,400),400);
var score = 0;
var burger = createSprite(randomNumber(100,300),randomNumber(100,300));
burger.setAnimation("american_hamburger_1");
burger.scale = 0.2;
burger.setCollider("circle");
burger.debug = true;
missile1.setAnimation("missile.png_1");
missile2.setAnimation("missile.png_1");
missile3.setAnimation("missile.png_1");
missile4.setAnimation("missile.png_1");
missile2.rotation = 90;
missile3.rotation = 180;
missile4.rotation = 270;
missile1.scale = 0.75;
missile2.scale = 0.75;
missile3.scale = 0.75;
missile4.scale = 0.75;
missile1.velocityX = 5;
missile2.velocityY = 5;
missile3.velocityX = -5;
missile4.velocityY = -5;
var gravity = 0;
var songPlaying = false;
var songProgress = 0;
var jumpCooldown = 0;

function draw() {
  var delta = World.frameRate || 1;
  background("lightblue");
  text("Score: " + score,320,25);
  if (eagle.y >= 500) {
    eagle.velocityY = 0;
    gravity = 0;
    eagle.y = 352.5;
  } else {
    gravity = gravity+(2/delta);
    eagle.velocityY = eagle.velocityY + gravity;
  }
  if (songPlaying === false) {
    playSound("USA-Anthem-but-with-explosions-and-bald-eagle-screeches.mp3");
    songPlaying = true;
  }
  if (eagle.isTouching(burger)) {
    score = score+1;
    burger.x = randomNumber(0,400);
    burger.y = randomNumber(0,400);
  }
  jumpCooldown = jumpCooldown-1/delta;
  songProgress = songProgress+(1/delta);
  if (songProgress >= 77) {
    songProgress = 0;
    songPlaying = false;
  }
  if (keyWentDown("up")||keyWentDown("w")) {
    if (jumpCooldown <= 0) {
      jumpCooldown = 0.3;
      gravity=0;
      eagle.velocityY = -10;
    }
  }
  if (keyDown("left")||keyDown("a")) {
    eagle.velocityX = eagle.velocityX-4/delta;
    eagle.setAnimation("eagle_left");
  }
  if (keyDown("right")||keyDown("d")) {
    eagle.velocityX = eagle.velocityX+4/delta;
    eagle.setAnimation("eagle_right");
    
  }
  if (keyDown("down")||keyDown("s")) {
    gravity=0;
    eagle.velocityY = 3;
  }
  missile1.displace(eagle);
  missile2.displace(eagle);
  missile3.displace(eagle);
  missile4.displace(eagle);
  if (missile1.x>500) {
    missile1.x = -100;
    missile1.y = randomNumber(0,400);
  }
  if (missile2.y>500) {
    missile2.y = -100;
    missile2.x = randomNumber(0,400);
  }
  if (missile3.x<-100) {
    missile3.x = 500;
    missile3.y = randomNumber(0,400);
  }
  if (missile4.y<-100) {
    missile4.y = 500;
    missile4.x = randomNumber(0,400);
  }
  // DRAW SPRITES
  drawSprites();
  
  // GAME OVER
  if (eagle.x < -50 || eagle.x > 450 || eagle.y < -50 || eagle.y > 450) {
    background("black");
    textSize(50);
    fill("green");
    text("Game Over!", 50, 200);
  }
  
}

delta accounts for the 0 at the beginning and swaps it on 1 you could also make it return on the 1st frame as well if your too lazy and only care about it working if that’s your aim I’ll show that as well but it’ll delay your game by 1 frame

if (songPlaying === false) {
    playSound("USA-Anthem-but-with-explosions-and-bald-eagle-screeches.mp3");
    songPlaying = true;
    if (World.frameCount == 0) {
      return;
    }
  }