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