Error: Overlap can only be checked between Sprites or groups

Can someone help me with this error message? Here is the code. At random times, the game stops and give this error message to the student.

// Variables
var player = createSprite(200, 100);
player.setAnimation(“alien”);
var platform = createSprite(300, 200);
platform.setAnimation(“platform”);
platform.velocityY = 2;
var platform2 = createSprite(100, 0);
platform2.setAnimation(“platform”);
platform2.velocityY = 2;
var star = createSprite(200, 200);
star.setAnimation(“star”);
star.velocityY = 3;
star.x = randomNumber(50, 350);
star.y = randomNumber(-30, -60);
var star2 = createSprite(200, 200);
star2.setAnimation(“star”);
star2.velocityY = 3;
star2.x = randomNumber(50, 350);
star2.y = randomNumber(-30, -60);
var score = 0;

// Create Sprites

function draw() {
// draw the background
if (score >= 100) {
background2();
} else {
background1();
}
loopiteams();
showScore();
controlplayer();
playerlands();
collectiteams();
// update the sprites
drawSprites();

}

// Functions
function background2() {
if (score > 100) {
background(“blue”);

}
}
function background1() {
background(“darkBlue”);
noStroke();
fill(“yellow”);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(340, 50, 60, 60);
fill(“darkBlue”);
ellipse(320, 30, 60, 60);
}
function showScore() {
fill(“white”);
textSize(20);
text(“Score:”,10, 10, 80, 20);
text(score, 70, 27);
}
function loopiteams() {
if (platform.y > 400) {
platform.y = 0;
}
if (platform2.y > 400) {
platform2.y = 0;
}
if (star.y > 400) {
star.y = randomNumber(-30, -60);
star.x = randomNumber(50, 350);
}
if (star2.y > 400) {
star2.y = randomNumber(-30, -60);
star2 = randomNumber(50, 350);
}
}
function controlplayer() {
if (keyDown(“left”)) {
player.x = player.x + -5;
}
if (keyDown(“right”)) {
player.x = player.x + 5;
}
if (keyDown(“up”)) {
player.velocityY = -10;
if (player.y > 400) {
player.y = 200;
}
}
player.velocityY = player.velocityY + 1;
}
function playerlands() {
if (player.isTouching(platform)) {
platform.setAnimation(“platform color”);
} else {
platform.setAnimation(“platform”);
}
if (player.isTouching(platform2)) {
platform2.setAnimation(“platform color”);
} else {
platform2.setAnimation(“platform”);
}
player.collide(platform);
player.collide(platform2);
}
function collectiteams() {
if (player.isTouching(star)) {
star.x = randomNumber(50, 350);
star.y = randomNumber(-30, -60);
score = score + 1;
}
if (player.isTouching(star2)) {
star2.y = randomNumber(-30, -60);
star2.x = randomNumber(50, 350);
score = score + 1;
}
}

Can you share a link to the project? It makes debugging a lot easier

Does this link work?

Hi Anisa,

Yes, that works. Here’s the rundown of the debugging process for this.

The message “overlap only works with sprites” most likely means that the program was expecting a sprite, but got something else. So, something that the programmer intended to be a sprite was, in fact, not a sprite.

This can be caused be a couple different things. Maybe the programmer accidentally used the wrong variable, thinking it contained a sprite when it did not. More likely, the programmer had stored a sprite inside a variable, then stored SOMETHING ELSE inside the variable, knocking the sprite out of the variable so it is no longer accessible through that variable name (even though the sprite still exists and can be drawn to screen through drawSprites).

Based on the fact that this is line 117, I’m going to assume that the player.isTouching(star2) is the problem for a couple reasons. First, there’s nothing about the code in line 116 or 117 that would REQUIRE a sprite, even though there are sprites there. Second, I know that isTouching checks for whether things are overlapping, so there’s a good chance that isTouching uses an overlap method inside of it.

Given that, I will look through the code and see whether there is somewhere that player or star2 is assigned a value that is not a sprite. A quick skim of the program shows that on line 77, star2 is assigned a random number. This explains the problem, and why it takes a while to show up, because this code is only run AFTER star2 goes past the vertical 400 position. At that point, a random number is stored into the star2 variable and there is no longer a sprite stored in the variable. So, when line 115 calls player.isTouching(star2) there is an error, because isTouching cannot check whether a sprite is touching a number.

That may not be the only place that this is a problem, but similar steps should allow you to find all the bugs.

Elizabeth

1 Like

Hello, I want to bring attention to the debug message on this game. It is giving a misleading error message

ERROR: Line: 34: Error: Error: overlap can only be checked between sprites or groups

However, what is actually wrong is inside the conditional where this error message is triggered, the students is calling

    baseball = randomNumber(50, 400);
    baseball = randomNumber(50, 300);

instead of using the correct way of:

    baseball.x = randomNumber(50, 400);
    baseball.y = randomNumber(50, 300);

Hi @timothy_brinkley,

You are correct in that the programmer should have used baseball.x and baseball.y. I think the explanation above from @elizabeth_admin also describes why the overlap error occurred in your game. When your programmer assigned baseball to a random number and not baseball.x, the variable was no longer storing a sprite but something else. Then, the conditional for isTouching uses an overlap method so that generated the error (because baseball is really no longer a sprite). That’s how I see it based on the explanation above. Glad you discovered how to fix it - sometimes those are tricky to find.

~Michelle

1 Like

So is there any way to rectify this issue? For my students’ game the error comes when the baker touches the egg or the milk. Here is the link to the game: Game Lab - Code.org

@gagwu,

Your student’s error is similar to the others in this thread. On line 144, they are confusing milk and milks and by changing the value of milk to be a number rather than a sprite, it breaks the code. I’m guessing they meant to use milks on this line and not milk (which should remain a sprite).

thanks,

Mike