ERROR: Line: 99: TypeError: player.collide is not a function

I am not sure how to resolve this error message for this student. The code runs for almost 5 secs and then errors out? ERROR: Line: 99: TypeError: player.collide is not a function
Any help is appreciated.

// Variables
var score = 0;

// Create Sprites
var player = createSprite(200, 200);
player.setAnimation(“alien”);
var star1 = createSprite(randomNumber(0, 400), 0);
star1.setAnimation(“star”);
var star2 = createSprite(randomNumber(0, 400), 0);
star2.setAnimation(“star”);
var platform1 = createSprite(100, 0);
platform1.setAnimation(“platform”);
platform1.velocityY = 1;
var platform2 = createSprite(300, 200);
platform2.setAnimation(“platform”);
platform2.velocityY = 1;
star1.velocityY = 2;
star2.velocityY = 2;

function draw() {
// draw the background
background1();
if (score >= 15) {
background2();
}
controlPlayer();
playerLands();
showScore();
loopPlatform();
loopItems();
playerFall();
collectItems();
// update the sprites

drawSprites();
}

// Functions
function background1() {
background(“darkBlue”);
noStroke();
fill(“yellow”);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(340, 50, 60, 60);
fill(“darkBlue”);
ellipse(320, 30, 60, 60);
}
function background2() {
background(“lightBlue”);
noStroke();
fill(“blue”);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(340, 50, 60, 60);
fill(“darkBlue”);
ellipse(320, 30, 60, 60);
}
function showScore() {
fill(“white”);
textSize(20);
text(“Score:” + score,10, 10, 80, 20);
}
function loopPlatform() {
if (platform1.y >= 410) {
platform1.x = randomNumber(0, 400);
platform1.y = -5;
}
if (platform2.y >= 410) {
platform2.x = randomNumber(0, 400);
platform2.y = -5;
}
}
function loopItems() {
if (star1.y >= 400) {
star1.x = randomNumber(0, 400);
star1.y = 0;
}
if (star2.y >= 400) {
star2.x = randomNumber(0, 400);
star2.y = 0;
}
}
function playerFall() {
player.velocityY = 2;
}
function controlPlayer() {
if (keyDown(“right”)) {
player.x = player.x + 2;
}
if (keyDown(“left”)) {
player = player.x - 2;
}
if (keyDown(“up”)) {
player.y = player.y + -5;
}
}
function playerLands() {

  • player.collide(platform1);*
  • player.collide(platform2);*
    }
    function collectItems() {
    if (player.isTouching(star1)) {
    star1.x = randomNumber(0, 400);
    star1.y = 0;
    score = score + 1;
    }
    if (player.isTouching(star2)) {
    star2.x = randomNumber(0, 400);
    star2.y = 0;
    score = score + 1;
    }
    }

Hi Joell,

collide is a method/function that is automatically there for any sprite. If player.collide isn’t a function, that means that player isn’t a sprite anymore. I’d have the student check over the code for somewhere that the player variable was assigned something that is not a sprite, probably because the student meant to assign something to a sprite property, but instead assigned the value to the entire sprite, thus knocking the sprite out of the variable and leaving a new (non-sprite) value there instead.

You could also use watchers to “watch” player . Eventually, it will change from a sprite to something else, and you’l know when the problem shows up.

Elizabeth

1 Like