Unit 3 Lesson 22 help

This is a student project, so I don’t know it you can view itcode
He is getting the error message: ERROR: Line: 170: Error: Error: overlap can only be checked between sprites or groups. I don’t know what that means.

Hi Lisa,

It would be very helpful and make it easier for us to help, if you could fill out the template in the debugging category. The information includes the following:

  • Share a link to the project / level that’s giving them trouble
    • A description of what specifically you expect to happen, ideally talking about a specific element on the screen or specific lines of code
    • A description of what happens instead
    • A description of what you already have tried to solve the problem
    • The more details the better

Here is the code for the issue:

// Create your variables here
var score;
var life;
life = 100;
score = 0;
var fun;
fun = randomNumber(1, 3);

// Create your sprites here
var boi = createSprite(200, 200);
var alien = createSprite(200, 0);
var b = createSprite(boi.x, boi.y);
var hole_1 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
var hole_2 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
var hole_3 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
b.setAnimation(“axe_iron_1”);
hole_2.setAnimation(“hole”);
hole_1.setAnimation(“hole”);
hole_3.setAnimation(“hole”);
hole_1.visible = 0;
hole_2.visible = 0;
hole_3.visible = 0;
boi.setAnimation(“boi_1”);
boi.scale = 0.5;
b.scale = 0.2;
b.visible = 0;
alien.velocityY = 3;

function draw() {
hit();
b.rotationSpeed = 21;
if (fun == 1) {
alien.setAnimation(“alienBlue_bounce_1”);
}
if (fun == 2) {
alien.setAnimation(“alienBlue_bounce_1_copy_2”);
}
if (fun == 3) {
alien.setAnimation(“alienBlue_bounce_1_copy_1”);
b.setAnimation(“blender_1”);
}
if (alien.isTouching(boi)) {
alien = 200;
alien.y = 0;
life = life - 10;
}
if (life == 0) {
alien.velocityX = 0;
alien.velocityY = 0;
boi.velocityX = 0;
boi.velocityY = 0;
b.velocityX = 0;
b.velocityY = 0;
playSound(“yeeet.mp3”, false);
playSound(“yeet.mp3”, false);
}
if (score >= 1000) {
hole_1.visible = 100;
hole_2.visible = 100;
background(“yellow”);
hole_1.displace(boi);
hole_2.displace(boi);
}
if (score >= 2000) {
hole_3.visible = 100;
background(“purple”);
hole_3.displace(boi);
}
// draw background
move();
if ((alien.y) <= 0) {
alien.velocityY = randomNumber(4, 6);
alien.velocityX = randomNumber(4, 6);
}
if (alien.y >= 400) {
alien.velocityY = randomNumber(-4, -6);
alien.velocityX = randomNumber(-4, -6);
}
if ((alien.x) <= 0) {
alien.velocityY = randomNumber(4, 6);
alien.velocityX = randomNumber(4, 6);
}
if (alien.x >= 400) {
alien.velocityY = randomNumber(-4, -6);
alien.velocityX = randomNumber(-4, -6);
}
if ((boi.y) <= 0) {
boi.velocityY = 5;
}
if (boi.y >= 400) {
boi.velocityY = -5;
}
if ((boi.x) <= 0) {
boi.velocityX = 5;
}
if (boi.x >= 400) {
boi.velocityX = -5;
}
background(“grey”);
fill(“black”);
textSize(20);
text(“Score:”, 5, 5, 20, 20);
text(score , 70, 5, 20, 20);
fill(“black”);
textSize(20);
text(“Life:”, 5, 50, 20, 20);
text(life , 50, 50, 20, 20);

// update sprites

drawSprites();
}
function move() {
if (keyWentDown(“w”)) {
b.x = boi.x;
b.y = boi.y;
b.velocityY = -3;
b.velocityX = 0;
b.visible = 100;
}
if (keyWentDown(“s”)) {
b.visible = 100;
b.x = boi.x;
b.y = boi.y;
b.velocityY = 3;
b.velocityX = 0;
}
if (keyWentDown(“d”)) {
b.x = boi.x;
b.y = boi.y;
b.visible = 100;
b.velocityX = 3;
b.velocityY = 0;
}
if (keyWentDown(“a”)) {
b.x = boi.x;
b.y = boi.y;
b.visible = 100;
b.velocityX = -3;
b.velocityY = 0;
}
if (keyWentDown(“left”)) {
boi.velocityX = -5;
boi.setAnimation(“Boi_2”);
}
if (keyWentUp(“left”)) {
boi.velocityX = 0;
}
if (keyWentDown(“right”)) {
boi.velocityX = 5;
boi.setAnimation(“boi_1”);
}
if (keyWentUp(“right”)) {
boi.velocityX = 0;
}
if (keyWentDown(“down”)) {
boi.velocityY = 5;
}
if (keyWentUp(“down”)) {
boi.velocityY = 0;
}
if (keyWentDown(“up”)) {
boi.velocityY = -5;
}
if (keyWentUp(“up”)) {
boi.velocityY = 0;
}
}
function hit() {
if (b.isTouching(alien)) { This is where the red box appears. Line 170}
b.visible = 0;
b.x = 420;
b.y = 420;
alien.x = 200;
alien.y = 0;
b.velocityY = 0;
b.velocityX = 0;
score = score + 100;
}
}

The code works but when boi touches alien, the game stops with the following error message “ERROR: Line: 170: Error: Error: overlap can only be checked between sprites or groups”. I have no idea what overlap means.

Hi Lisa,

Can your student click SHARE, copy the link, and then send to you so you can post it here? Being able to access all the sprites and tabs would help problem solving.

Thank you,
Michelle

It is shared. Here is the link: https://studio.code.org/s/csd3-2019/stage/22/puzzle/9

@leachlisa unfortunately, that’s just the generic link to that stage of unit 3 in CSD. To get the link to the student’s specific project, he will have to click the “Remix” button and then the “Share” button and then we will have the exact link to his project and be able to look and see what the error is doing.

Sorry for the extra work!

Mike

Sorry. Here is the link>

All I’m seeing in that link is the code.org sign in link:

https://studio.code.org/users/sign_in

Does your link look different? It should be longer.

Mike

Also, one quick thing … I was able to “somewhat” recreate his program using his code and adding my own sprites. I’m sure it looks different than his, but I can see that error code. Not sure yet that I’ve fully isolated the problem, but if I move the “boi” out of the way, the alien does hit the axe and the intended code does run, the score updates, etc. My first thought is that it’s something to do with having the boy in the way of the collision, but I’m not sure why that is throwing an error. That’s just my first attempt at it and it would be easier with a link to his code using his animations so I can better see what he is trying to do exactly.

thanks,

Mike

Another quick observation, the error seems to completely go away in my version if you change line 170 to read:

if (boi.isTouching(alien)){

this checks for a collision between the boy and the alien. Not sure if that’s what he is after, but it may be helpful if he plugs that in. It may give him some inspiration.

Mike

Ok… just got your link. I did that same change I mentioned in line 170

if (boi.isTouching(alien)){

It seems to solve the problem most of the time, but the error sometime still reoccurs. I’ll see if I can get another pair of eyes on it, but that substitution does work better even if not yet perfect.

Mike

Let me see if I can tackle this…

First, I usually like to go top to bottom to find the error but this one was harder to do that because of the function calls. So, I tried to figure out what the student wanted to do with the axe (b). It looks like it was a weapon. My thinking was the overlap error was generated because the boi and axe were connected so I thought if you separated them in the conditional, it may work (or at least be more accurate). So, my thinking said “if the axe is visible AND it is touching the alien, then you should call the function hit”. The student had the function hit running at the top of the draw loop. When I changed this, an error came up about alien not being a function. I found a great post by @elizabeth_admin that talks about his. Your student did have alien=200 (vs alien.x or alien.y). When I took out this line, there does not seem to be an error but I am open to the idea that one is still there if the game is played enough. Also, my version takes out the fun variable (that randomly generates an alien animation) but it should go back in without a problem.

Also, many of the sprite.visible properties are set to a number but it is a boolean property meaning it is either true or false only (not a number). Here is some documentation on sprite.visible.

Let us know if these tips help out or if I missed the mark (or the alien-ha!) Here is my remix: https://studio.code.org/projects/gamelab/ags5A5O-qzXbi3rMVwbYH8SPW61r7uk38Yrw4CwYGYk

1 Like

@melynn is right. I missed the line (43, I believe), where he coded alien=200; It looks like he meant to write alien.x = 200; I’m sure that’s the line of code that is throwing the error because it tried to turn a sprite (alien) into a regular variable with a value so when it started checking for the collision, it couldn’t find the “sprite” called alien because it had been changed into a variable.

Thanks @melynn!

Mike

1 Like