# TypeError: object is not a function HELP

**URL:** <https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591>\
**Category:** Unit and Lesson Discussion\
**Tags:** csd-unit-3, csd-unit-3-lesson-22\
**Created:** [November 26, 2018, 4:31pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591 "2018-11-26T16:31:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason.whiting](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jason.whiting](https://forum.code.org/u/jason.whiting)\
**Post date:** [November 26, 2018, 4:31pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/1 "2018-11-26T16:31:18Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)\
**Post date:** [November 26, 2018, 7:33pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/2 "2018-11-26T19:33:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)\
**Post date:** [November 26, 2018, 7:38pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/3 "2018-11-26T19:38:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jason.whiting](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jason.whiting](https://forum.code.org/u/jason.whiting)\
**Post date:** [November 26, 2018, 8:26pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/4 "2018-11-26T20:26:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clarkea](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@clarkea](https://forum.code.org/u/clarkea)\
**Post date:** [December 14, 2018, 9:06pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/5 "2018-12-14T21:06:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)\
**Post date:** [December 14, 2018, 10:37pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/6 "2018-12-14T22:37:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clarkea](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@clarkea](https://forum.code.org/u/clarkea)\
**Post date:** [December 17, 2018, 7:01pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/8 "2018-12-17T19:01:34Z")

</div>

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

> **[Check out what I made](https://studio.code.org/projects/gamelab/NgUPT9tu_EZOEbhNQ2D3YsEgUJ4ywlwnIVJEBUbnvtA)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![jold](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jold/32/5811_2.png) [@jold](https://forum.code.org/u/jold)\
**Post date:** [December 21, 2018, 3:31pm UTC](https://forum.code.org/t/typeerror-object-is-not-a-function-help/22591/9 "2018-12-21T15:31:34Z")

</div>

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.

```auto
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:

```auto
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.
