Help for student Unit 3L18

Student would like to have his character not go off screen when the game is played - how would he achieve this?

var character = createSprite(200, 200);
character.setAnimation(“flyer”);
var coin = createSprite(randomNumber(0, 400), randomNumber(0, 400));
coin.setAnimation(“coin”);
var e = createSprite(200, 200);
e.setAnimation(“e”);
e.scale = 0.5;
e.rotationSpeed = 3;

function draw() {
background(“skyblue”);

// Simulating Gravity
character.velocityY = character.velocityY + 0.1;

// update sprites
//it’s right here
if(keyWentDown(“up”)){
character.velocityY = -5;
}
if (character.velocityX >= -5) {
if (keyDown(“left”)) {
character.velocityX = character.velocityX + -.5;
}
}
if ((character.velocityX) <= 5) {
if (keyDown(“right”)) {
character.velocityX = character.velocityX + .5;
}
}
if (character.isTouching(coin)) {
coin.x = randomNumber(0, 400);
coin.y = randomNumber(0, 400);
}
if (coin.isTouching(e)) {
coin.x = randomNumber(0, 400);
coin.y = randomNumber(0, 400);
}

character.bounceOff(e);
rect(0, 0, 400, 10);
rect(0, 0, 10, 400);
rect(0, 400, 400, -10);
rect(400, 400, -10, -400);
drawSprites();
}
**https://studio.code.org/s/csd3-2019/stage/18/puzzle/16?section_id=2284631&user_id=53149456
What I expect to happen: character not go off screen
What actually happens: character disppears off screen

@crosado,

Good to see you again. It’s a bit hard to fully troubleshoot the program without the link to the shared project. Your link unfortunately just takes me to my own program on that level. You could have the student remix and then share the project to get the link, however, I just helped another teacher with almost the exact same issue on almost the same level, so my answer from earlier today may help.

Here’s a link to that answer. Let us know if that helps!

Mike

1 Like

If you don’t see the two blocks mentioned in the reply above, you may need to have the student remix the project and that will open all of the sprite blocks.

Mike

1 Like

Thanks a mil, this helped alot :slight_smile: