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