21.20 CSD--ERROR: Line: 104: Error: Error: overlap can only be checked between sprites or groups

Any help you can give would be great!

// Variables
var score = 0;
// Create Sprites
var platform = createSprite(90,-10);
platform.setAnimation(“platform”);
platform.velocityY = 1;
platform.setCollider(“rectangle”);

var platform2 = createSprite(280,200);
platform2.setAnimation(“platform”);
platform2.velocityY = 1;
platform2.setCollider(“rectangle”);

var star = createSprite(randomNumber(50,350),randomNumber(-30,-60));
star.setAnimation(“star”);
star.velocityY = 2;

var star2 = createSprite(randomNumber(50,350),randomNumber(-30,-60));
star2.setAnimation(“star”);
star2.velocityY = 2;

var player = createSprite(200,350);
player.setAnimation(“alien”);
player.setCollider(“rectangle”);

function draw() {
// draw the background
background1();
if (score >= 100) {
background2();
}
showScore();
// update the sprites
loopPlatforms();
loopPlatforms2();
loopStars();
playerFall();
controlPlayer();
playerLands();
collectItems();
drawSprites();
}

// Functions
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 background2() {
background(“lightblue”);
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);
}
function showScore() {
fill(“white”);
textSize(20);
text("Score: ",10, 10, 80, 20);
text(score,75, 27);
}
function loopPlatforms() {
if (platform.y === 400) {
platform.y = -10;
}}
function loopPlatforms2() {
if (platform2.y === 400) {
platform2.y = -10;
}}
function loopStars() {
if (star.y > 410) {
star.x = randomNumber(50,350);
star.y = randomNumber(-10,-50);
}
}
function playerFall() {
player.velocityY = 2;
}
function controlPlayer() {
if (keyDown(“up”)) {
player.y = player.y - 5;
}
if (keyDown(“right”)) {
player.x = player.x + 5;
}
if (keyDown(“left”)) {
player.x = player.x - 5;
}
}
function playerLands() {
player.collide(platform);
}

function collectItems() {
if (player.isTouching(“star”)) {
score = score + 1;
star.x = randomNumber(50,350);
star.y = -10;
}
if (player.isTouching(“star2”)){
score = score + 1;
star2.x = randomNumber(50,350);
star2.y = -10;
}
}

Hi, in your collectItems function, you are using the string (quote) "star" rather than the sprite variable (no quote) star when you called the isTouching method. isTouching can only be used with sprites, not with strings.

(The overlap method checks whether two sprites overlap with each other and is used with isTouching, collide, displace, bounce, and bounceOff, so when there is a problem with overlap, those methods are usually where the problem is triggered.)

Elizabeth

2 Likes