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

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

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

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

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.

@laurenlu.davis

Can you try remixing it first?

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

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

thanks

lauren

Hi Lauren,

You have the same problem as in the thread that I linked to above (Need help with an error). The student is redefining a sprite to be a number.

Elizabeth

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

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

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

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.

@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

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

Could you also send your response to 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

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

 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

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

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