Bubble 16 - Help with Edges debugging

Hello. I spent a lot of time with the edge code, and got mine to detect the edges. An 8th student of mine came up and asked about making it detect edges because it is in the suggested extras. I cannot find the difference between his code and mine. Mine works, with the sprite.bounceOff(edges) command, but when he puts it in his, it freezes everything. When he hits run, everything shows like it is going to work, but the control keys do not move and he loses his velocity. There is an error code, but I’m not sure how to fix it. The error code goes away when we remove the bounce off command and everything works without it. This code works on mine though. We tried putting it in an if statement, but that didn’t work either.

This is a screenshot of his screen. (The picture did not upload so I’m trying a hyperlink to the picture) Screenshot of error

The code is below. The line that is causing the problem is in the last line above the }

Thanks for any help!

var character = createSprite(200, 200);

character.setAnimation(“flyer”);

var score = 0;

var plane = createSprite(200, 400);

plane.setAnimation(“plane”);

var coin = createSprite(randomNumber(0, 400), randomNumber(40, 360));

coin.setAnimation(“coin”);

var sun = createSprite(200, 200);

sun.setAnimation(“obstacle”);

sun.scale = 0.50;

sun.setCollider(“circle”);

sun.debug = true;

function draw() {

background(“skyblue”);

fill(“black”);

textSize(20);

text(“Score:”, 5, 5, 20, 20);

text(score , 70, 5, 20, 20);

// Simulating Gravity

character.velocityY = character.velocityY + 0.1;

// update sprites

if(keyWentDown(“up”)){

character.velocityY = -5;

}

if (keyWentDown(“right”)) {

character.velocityX = character.velocityX+0.3;

}

if (keyWentDown(“left”)) {

character.velocityX = character.velocityX-0.3;

}

if (character.isTouching(coin)) {

coin.x = randomNumber(0, 400);

coin.y = randomNumber(40,360);

score = score+1;

}

if (character.isTouching(sun)) {

character.bounceOff(sun);

}

if (coin.isTouching(sun)) {

coin.bounceOff(sun);

}

if (character.isTouching(plane)) {

character.bounceOff(plane);

}

drawSprites();

character.bounceOff(edges);

}

I don’t have permission to see the image.
It also may be helpful to post a link to the project so that we can more easily debug it.

1 Like

I fixed the link, and thanks for the idea to share the link to his project. It is here! Project LInk

thanks!
To use the edges you have to
createEdgeSprites();
before you can bounce off of them. I added that method call before the draw loop and it made the error go away.

Here is an example from the documentation.

createEdgeSprites();
var sprite = createSprite(200, 200);
sprite.setVelocity(randomNumber(-5, 5), randomNumber(-5, 5));
function draw() {
  background("white");
  drawSprites();
  sprite.bounceOff(edges);
}
1 Like

Ohhh!! Thank you so much! I have that in my program, and completely missed he didn’t have that. Thanks for finding that!

1 Like

That smile on his face when his game worked! Thanks again!

4 Likes

I created a few How To guides for my students. Here is an example Edge Sprite How To

2 Likes