Error: Overlap can only be checked between Sprites or groups

csd-unit-3

#1

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


#2

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


#3

Does this link work?


#4

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