# Can't figure out what is wrong Lesson 3 final game-help

**URL:** <https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449>\
**Category:** Coding and Debugging Help\
**Tags:** csd-unit-3\
**Created:** [January 30, 2019, 6:45pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449 "2019-01-30T18:45:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 30, 2019, 6:45pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/1 "2019-01-30T18:45:39Z")

</div>

No matter what, the student gets an error that says line 56 “undefined is not a function” But it is defined… Student error or code error?

// Create your functions here  
var benson = createSprite(200, 0);  
benson.setAnimation(“benson”);  
benson.scale = 0.75;  
var cloud = createSprite(50, 300);  
cloud.setAnimation(“cloud\_1”);  
cloud.scale = 0.65;  
var cloud2 = createSprite(300, 150);  
cloud2.setAnimation(“cloud\_2”);  
cloud2.scale = 0.65;  
var honey = createSprite(randomNumber(25, 350), randomNumber(-30, -60));  
honey.setAnimation(“honey”);  
honey.scale = 0.4;  
honey.velocityY = 2;  
var honey2 = createSprite(randomNumber(25, 350), randomNumber(-30, -60));  
honey2.setAnimation(“honey2”);  
honey2.scale = 0.4;  
honey2.velocityY = 2;  
var honey3 = createSprite(randomNumber(30, 350), randomNumber(-30, -60));  
honey3.setAnimation(“honey3”);  
honey3.scale = 0.4;  
honey3.velocityY = 2;  
var score = 0;

// Create your sprites here

function draw() {  
background(“blue”);  
// draw background  
if (score \> 19) {  
background(“red”);  
}  
if (keyDown(“right”)) {  
benson.x = benson.x + 5;  
}  
if (keyDown(“left”)) {  
benson.x = benson.x - 5;  
}  
if (keyDown(“up”)) {  
benson.velocityY = -3;  
}  
benson = benson.velocityY + 0.1;  
Score();  
drawSprites();

// update sprites

fill(“black”);  
textSize(24);  
text(“score”, 50, 25);  
text(score, 120, 27);  
}  
function Score() {  
if (benson.isTouching(honey3)) {  
score = score + 1;  
honey3.x = randomNumber(30, 350);  
honey3.y = randomNumber(-30, -60);  
}  
if (benson.isTouching(honey2)) {  
score = score + 1;  
honey2.x = randomNumber(30, 350);  
honey2.y = randomNumber(-30, -60);  
}  
if (benson.isTouching(honey)) {  
score = score + 1;  
honey.x = randomNumber(30, 350);  
honey.y = randomNumber(-30, -60);  
}  
}

---

<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:** [January 30, 2019, 7:54pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/2 "2019-01-30T19:54:06Z")

</div>

Hi Lauren,

Can you please link to the original code by pasting the result of hitting the “Share” button? It’s hard to debug without having access to the project itself, especially since we can’t see which line is line 56 without counting down all the lines.

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:** [January 30, 2019, 8:05pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/3 "2019-01-30T20:05:27Z")

</div>

It’s likely that you have a similar problem to this code:

> [@Need help with an error](https://forum.code.org/t/need-help-with-an-error/24238/2):
>
> @smcelwrath Hi Stephanie, Really interesting bug! I’ll start off with the background of what happened and the debugging process in case you want to tell your students, but if you want you can skim to the end. Usually an error message like this means that you are treating something as a function (calling it), even though it is not one. This can either be because you forgot to define the function, misspelled it, or (most commonly) have redefined your function as something else in a different …

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 30, 2019, 8:26pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/4 "2019-01-30T20:26:17Z")

</div>

Okay,  
There is not a way for a teacher to press save and share right? I can see the code, but that button is disabled in my view.

---

<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:** [January 30, 2019, 8:40pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/5 "2019-01-30T20:40:46Z")

</div>

@laurenlu.davis

Can you try remixing it first?

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 30, 2019, 9:03pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/6 "2019-01-30T21:03:55Z")

</div>

I’ll have to try tomorrow. When you open the lesson as a teacher it is in view only. This is not in gameLab

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 31, 2019, 5:24pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/7 "2019-01-31T17:24:13Z")

</div>

Hi Elizabeth,  
Below is his code. We have tried renaming and moving and taking in and out of function… at a loss?

[https://studio.code.org/projects/gamelab/y4lhRGTQvVkqVK9o4RxmbIUsy0SHt2x9y5ir-8qYDh4](https://studio.code.org/projects/gamelab/y4lhRGTQvVkqVK9o4RxmbIUsy0SHt2x9y5ir-8qYDh4)

thanks

lauren

---

<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:** [January 31, 2019, 5:46pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/8 "2019-01-31T17:46:09Z")

</div>

Hi Lauren,

You have the same problem as in the thread that I linked to above ([Need help with an error](https://forum.code.org/t/need-help-with-an-error/24238/2)). The student is redefining a sprite to be a number.

Elizabeth

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 31, 2019, 6:16pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/9 "2019-01-31T18:16:54Z")

</div>

Okay. so the sprite can’t be honey1, honey3 etcc?

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 31, 2019, 6:23pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/10 "2019-01-31T18:23:47Z")

</div>

Took the numbers out of the sprite and it still doesn’t work?  
[https://studio.code.org/projects/gamelab/JrJHUUjNPPmlNpbOq12eqT0E6xcsq-A8WF27VePle8A](https://studio.code.org/projects/gamelab/JrJHUUjNPPmlNpbOq12eqT0E6xcsq-A8WF27VePle8A)

---

<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:** [January 31, 2019, 7:22pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/11 "2019-01-31T19:22:36Z")

</div>

Hi Lauren,

Did you have a chance to read the explanation from the other thread? The problem isn’t with the variable names. The problem is that the student has redefined the sprite to be a number, so it doesn’t have an `isTouching` method to call.

Eizabeth

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [January 31, 2019, 9:24pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/12 "2019-01-31T21:24:58Z")

</div>

Hi again,  
I am confused. Yes, I did read the thread about double naming something and he has not done that. He took the numbers honey1, honey2, honey3 and changed them to honey, honeyy, honeyyy so there are no numbers anywhere.

I am not sure what you mean by the the sprites are called numbers so there’s not is touching to call??

Thanks for your patience.

---

<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:** [February 1, 2019, 1:22am UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/13 "2019-02-01T01:22:35Z")

</div>

@laurenlu.davis

Hi Lauren,

I’m sorry I couldn’t give you a good, comprehensive answer before. I was running between meetings but was trying to get you something before your school day was over, but it wasn’t very helpful.

Here’s the background information on what’s going on with your sprite and what happened with the error.

Sprites are special types of data structures that basically have variables (properties) and functions (methods) attached to them. You access these properties and methods using dot notation, the period between the sprite and the property or method that you want to use.

For example, one property of a sprite is `velocityY`, and one method is `isTouching`. So, when you create a sprite, you automatically create special versions of `velocityY` and `isTouching` (and all the other properties and methods) that are specifically attached to this sprite. That’s why each sprite has its own velocityY that will not get confused with those of other sprites. Then, you take the entire sprite and store it into a variable.

Here’s an example from your student code: on the first line of the program, he created a new sprite at (200, 0). Even though you don’t see it, it has a `velocityY` property, which is defaulted to 0, and an `isTouching` method, that can check whether this _specific_ sprite is touching something. ALSO on this line, he created a variable called `benson`, and he stored the new sprite into the `benson` variable so that he can use it later.

The problem comes on Line 41. This line does a few things. First it tries to add `benson.velocityY + 0.1`. It looks inside the `benson` variable and sees the sprite that you have created. Then, it looks at the sprite’s `velocityY` property, which is zero, so it evaluates 0 + 0.1 and gets 0.1. So far so good. Then it stores 0.1 into the `benson` variable. This is a problem because the sprite was inside the `benson` variable. So, now the sprite is kicked out and nowhere to be found, and there is only the number 0.1 inside the `benson` variable.

When the program gets to Line 53, it tries to run `benson.isTouching`, but when it looks inside the variable `benson`, there is only a number in there. It tries to look for a method `isTouching` that goes with that number, but it doesn’t exist, and it says that the function is undefined.

To fix this problem, your student will need to go back to Line 41. He should be saving the number into the sprite’s `velocityY` property, so `benson.velocityY = benson.velocityY + 0.1;`.

Elizabeth

---

<div class="post-metadata">

**Author:** ![laurenlu.davis](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@laurenlu.davis](https://forum.code.org/u/laurenlu.davis)\
**Post date:** [February 1, 2019, 8:24pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/14 "2019-02-01T20:24:05Z")

</div>

Hi Elizabeth, Thanks for your response and that fixed it. I am sending you another one student and I are having difficulty with I was hoping you could tell us how to fix.

[https://studio.code.org/projects/gamelab/w8oR\_WeThwc20hqKwTyTvoHK1Nql4lp6S-9vjP7WGN0/edit](https://studio.code.org/projects/gamelab/w8oR_WeThwc20hqKwTyTvoHK1Nql4lp6S-9vjP7WGN0/edit)

Could you also send your response to [0070167108@my.tvusd.k12.ca.us](mailto:0070167108@my.tvusd.k12.ca.us)

Max. I am out on Monday and I know he will be anxious to fix it.

Best,

lauren davis

---

<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:** [February 4, 2019, 8:08pm UTC](https://forum.code.org/t/cant-figure-out-what-is-wrong-lesson-3-final-game-help/24449/15 "2019-02-04T20:08:07Z")

</div>

> [@laurenlu.davis](#):
>
> I was hoping you could tell us how to fix.
> 
> [https://studio.code.org/projects/gamelab/w8oR\_WeThwc20hqKwTyTvoHK1Nql4lp6S-9vjP7WGN0/edit](https://studio.code.org/projects/gamelab/w8oR_WeThwc20hqKwTyTvoHK1Nql4lp6S-9vjP7WGN0/edit)

Line 44 in the function direction.  
All of the if statements are using the

```auto
 if(keyDown.isTouching(floor1)) {

```

but keyDown is not a sprite object. Maybe this is a typo. I’m not quite sure what the student is trying to accomplish.  
Maybe they are trying to say if the player is touching the floor OR if the space key is down then set the player’s velocity y to -10.  
If that is the case then it should look like

```auto
if(player.isTouching(floor1) || keyDown("space") ) 
{
      player.velocityY = -10;
}

```

Maybe try that out. Let us know how it goes.
