ERROR: Line: 72: TypeError: alien.setAnimation is not a function

csd-unit-3

#1

My student gets this error at random times. How can I resolve the problem?

// Variables
var platform = createSprite(randomNumber(0, 400), randomNumber(-20, -50));
platform.setAnimation(“platform”);
platform.velocityY = 2;
var platform1 = createSprite(randomNumber(0, 400), randomNumber(0, -10));
platform1.setAnimation(“platform1”);
platform1.velocityY = 2;
var star2 = createSprite(randomNumber(0, 400), randomNumber(0, -30));
star2.setAnimation(“star”);
star2.velocityY = 3;
var star = createSprite(randomNumber(0, 400), randomNumber(0, -10));
star.setAnimation(“star”);
star.velocityY = 3;
var alien = createSprite(200, 0);
alien.setAnimation(“alien”);
alien.velocityY = alien.velocityY + 0.1;
var coin = createSprite(randomNumber(0, 400), randomNumber(-30, -100));
coin.velocityY = 5;
coin.setAnimation(“coin”);
var score;

// Create Sprites

function draw() {
// draw the background
background1();
background2();
showScore();
alien.collide(platform);
alien.collide(platform1);
if (score >= 100) {
rect(1000, 1000, 200, 200);
}
if (platform.y >= 420) {
platform.x = randomNumber(0, 400);
platform.y = randomNumber(0, -10);
platform.velocityY = 2;
}
if (platform1.y >= 420) {
platform1.x = randomNumber(0, 400);
platform1.y = randomNumber(0, -10);
platform1.velocityY = 2;
}
if (star.y >= 430) {
star.x = randomNumber(0, 400);
star.y = randomNumber(0, -10);
}
if (star2.y >= 430) {
star2.x = randomNumber(0, 400);
star2.y = randomNumber(0, -30);
}
if (alien.isTouching(platform)) {
platform.displace(alien);
}
if (alien.isTouching(platform1)) {
platform1.displace(alien);
}
if (keyWentDown(“up”)) {
alien.velocityY = -10;
} else {
alien.velocityY = alien.velocityY + 1;
}
if (keyWentDown(“left”)) {
alien.velocityX = -2;
alien.setAnimation(“alien_copy_1”);
}
if (keyWentDown(“right”)) {
alien = 2;
alien.setAnimation(“alien”);
}
if (alien.isTouching(star)) {
score = score + 1;
star.y = randomNumber(0, -10);
star.x = randomNumber(0, 400);
}
if (alien.isTouching(star2)) {
score = score + 1;
star2.y = randomNumber(0, -10);
star2.x = randomNumber(0, 400);
}
if (alien.isTouching(coin)) {
score = score + 1;
coin.x = randomNumber(-30, -100);
coin.y = randomNumber(0, 400);
}
drawSprites();
}
// update the sprites

// 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() {
fill(“Black”);
rect(0, 300, 500, 0);
}
function showScore() {
fill(“white”);
textSize(20);
text("Score: ",10, 10, 80, 20);
textSize(20);
}


#2

Can you share a link to the project? It makes debugging a lot easier


#3

#4

Hi Anisa,

setAnimation is a function that’s built into every sprite, so the only way for alien.setAnimation to not be a function is if alien isn’t a sprite anymore. That means that you probably have the same error as in the other project I just replied about, that somewhere along the line, the student has assigned something other than a sprite to the alien variable. If you explain this to the student, and have them look through the code, I bet they can find the place that they did this.

Elizabeth