Lesson 23 Flyer Game CS Discoveries)

\ 40x40 Christopher Franklin

Nov 19, 2021, 7:33 PST

My student just recreated Lesson 23 activity 2 Flyer game. In his code (below) the left arrow key does not control the sprite’s movement. When he takes out the right arrow key’s code, it works. How could this be?
// GAME SETUP
// create player, target, and obstacles
var fly_bot = createSprite(200, 100);
fly_bot.setAnimation(“fly_bot”);
fly_bot.scale = 0.8;

var coin = createSprite(300,350);
coin.setAnimation(“coin”);

var rock = createSprite(10,50);
rock.setAnimation(“rock”);

var rock = createSprite(300,25);
rock.setAnimation(“rock”);

//baclground
function draw() {
background(“lightblue”);

// LOOPING

// PLAYER CONTROLS
// change the y velocity when the user clicks “up”
if (keyDown(“up”)) {
fly_bot.velocityY= -10;
}

// decrease the x velocity when user clicks “left”
if (keyWentDown(“left”)) {
fly_bot.velocityX= -10;
}

// increase the x velocity when the user clicks “right”
if (keyDown(“right”)) {
fly_bot.velocityX= 10;
}
// FALLING
else {
fly_bot.velocityY = fly_bot.velocityY + 0.4;
fly_bot.velocityX=0;
}

// SPRITE INTERACTIONS
// reset the coin when the player touches it

// make the obstacles push the player

// DRAW SPRITES
drawSprites();

// GAME OVER
if (fly_bot.x < -50 fly_bot.x > 450 fly_bot.y < -50 fly_bot.y > 450) {
background(“black”);
textSize(50);
fill(“green”);
text(“Game Over!”, 50, 200);
}

}

Hi @christopher_franklin,

I see two things and one I think is causing your first error. First, the else statement under //FALLING might be causing errors in the x axis movement. It seems fly_bot.velocityY = fly_bot.velocityY + 0.4; should always exist (so it doesn’t need to be in the else so that the flyer is always falling) and I’m not sure if fly_bot.velocityX=0 is needed. Also, the line near the bottom: if (fly_bot.x < -50 fly_bot.x > 450 fly_bot.y < -50 fly_bot.y > 450) is in need of a compound operator like an OR to connect each statement.

I hope these tips help. If there are further issues, can you please paste the SHARE link to the student’s project so we can better help:)
Good luck!
~Michelle

I also am just wondering aloud if the command below shouldn’t be keyDown("left") instead of keyWentDown("left")) to match the other ones.

Good luck!

Mike