TypeError: object is not a function HELP

csd-unit-3
csd-unit-3-lesson-22

#1

I have a student that is getting the “TypeError: object is not a funtion” Here is the code and the error is on line 45

var background2 = createSprite(200, 200);
background2.setAnimation(“jk3.jpg_1”);
background2.scale = 2;
var level2 = createSprite(200, 200);
level2.setAnimation(“level2”);
level2.visible = false;
level2.scale = 3;
var lazar = createSprite(200, 200);
lazar.setAnimation(“lazar”);
lazar.scale = 0.1;
lazar.visible = false;
var win = createSprite(200, 200);
win.setAnimation(“win”);
win.visible = false;
win.scale = 1;
var plane = createSprite(200, 200);
plane.setAnimation(“Ship”);
var enemy = createSprite(450, 200);
enemy.setAnimation(“enemy”);
enemy.scale = 0.4;
var score = 0;
var lives = 3;
var boom = createSprite(200, 200);
boom.setAnimation(“pieceYellow_single10 1”);
boom.scale = 0.25;
boom.visible = false;
var end = createSprite(200, 200);
end.setAnimation(“textGameOver_1”);
end.visible = false;
end.scale = 1.5;
function draw() {
background(“black”);
showScore();
showLives();
playerMove();
barriers();
WhenWin();
WhenLose();
l();
lazarMovement();
enemyMovement();
gainPoint();
lazar();
loseLife();
LINE 45 ERROR if (enemy.x < 375) {
boom.visible = false;
}
if (keyDown(“a”)) {
playSound(“Kero Kero Bonito - Flamingo.mp3”, true);
}
drawSprites();
}
function playerMove() {
if (keyDown(“up”)) {
plane.velocityY = plane.velocityY - 0.5;
}
if (keyDown(“down”)) {
plane.velocityY = plane.velocityY + 0.5;
}
if (keyDown(“left”)) {
plane.velocityX = plane.velocityX - 0.5;
}
if (keyDown(“right”)) {
plane.velocityX = plane.velocityX + 0.5;
}
}
function barriers() {
if (plane.x < -10) {
plane.velocityX = 2.5;
}
if (plane.y > 410) {
plane.velocityY = -2.5;
}
if (plane.y < -10) {
plane.velocityY = 2.5;
}
}
function l() {
if (score > 24) {
enemy.velocityX = -7.5;
level2.visible = true;
}
}
function WhenWin() {
if (score > 49) {
background(“black”);
background2.visible = false;
win.visible = true;
enemy.visible = false;
plane.visible = false;
boom.visible = false;
lazar.visible = false;
end.visible = false;
}
}
function WhenLose() {
if (lives < 1) {
background(“black”);
end.visible = true;
enemy.visible = false;
plane.visible = false;
boom.visible = false;
lazar.visible = false;
win.visible = false;
}
}
function lazar() {
if (keyDown(“space”)) {
lazar.visible = true;
lazar.velocityX = lazar.velocityX + 0.5;
} else if ((plane.isTouching(lazar))) {
lazar.x = plane.x;
lazar.y = plane.y;
}
}
function lazarMovement() {
if (lazar.x > 400) {
lazar.x = plane.x;
lazar.y = plane.y;
}
if (lazar.velocityX > 100) {
lazar.velocityX = 100;
}
}
function enemyMovement() {
enemy.velocityX = -5;
}
function gainPoint() {
if (lazar.isTouching(enemy)) {
enemy.x = 500;
enemy.y = randomNumber(50, 350);
score = score + 1;
}
}
function loseLife() {
if (enemy.isTouching(plane)) {
enemy.x = 410;
enemy.y = randomNumber(0, 400);
lives = lives - 1;
boom.visible = true;
boom.x = plane.x;
boom.y = plane.y;
}
if (enemy.x < 0) {
lives = lives - 1;
enemy.x = 400;
enemy.y = randomNumber(50, 350);
}
}
function showScore() {
fill(“blue”);
textSize(20);
text(“Score:”, 5, 5, 20, 20);
text(score, 70, 5, 20, 20);
}
function showLives() {
fill(“red”);
textSize(12);
text(“Lives:”, 300, 5, 20, 20);
text(lives, 365, 5, 20, 20);
}


#2

Hi Jason,

Can you link us directly to the project by posting the “Share” link here? Otherwise, it can be difficult for us to debug the code.

Thanks,
Elizabeth


#3

Actually, I think the error is getting triggered two lines up. When it says that something is an object and not a function, it usually means that you are treating a sprite, which is an object, as a function, most of the time by putting parentheses after it as you would with a function.

It looks like you are doing that in this line:
lazar()

lazar is defined as both a sprite (near the beginning of the program) and as a function (more towards the end of the program), so the computer is confused what you are doing. It is using the sprite definition when you want to use the function definition. The student needs to change either the function name or the sprite name so that the same name is not used for two different things.

Elizabeth


#4

Thank you. Sometimes after looking at it for so long a new set of eyes finds the simple mistake.


#5

I have a similar issue where my students are being told that “number is not a function” but all of their sprites and functions are named separately. Any other thoughts?


#6

Can you post the share link here so we can look at the code?


#8

Yes, here it is! I am still unsure of why it does this and appreciate the fresh eyes!


#9

Beginning on line 11, you are making a variable and setting it to a random number between 0,400
but it seems like you want to be making a sprite since the next line is trying to set the animation.

var alien1 = randomNumber(0, 400);
alien1.setAnimation("alien1");

var alien2 = randomNumber(0, 400);
alien2.setAnimation("alien2");

If you want to make two sprites in a random spot on the screen, it should look like:

var alien1 = createSprite( randomNumber(0, 400), randomNumber(0, 400) ) ; 
alien1.setAnimation("alien1");

var alien2 = createSprite( randomNumber(0, 400), randomNumber(0, 400) ) ; 
alien2.setAnimation("alien2");

Try that out and let us know how it goes.