https://studio.code.org/projects/gamelab/bzbl2HnXvR42dKvHYLBB-XiDhMAu5HHG2n7PxlA1cJU
Lines 29 - 48 whichever 2 directions come 3rd and 4th work but the 1st and 2nd don’t control the shield.
https://studio.code.org/projects/gamelab/bzbl2HnXvR42dKvHYLBB-XiDhMAu5HHG2n7PxlA1cJU
Lines 29 - 48 whichever 2 directions come 3rd and 4th work but the 1st and 2nd don’t control the shield.
The problem is you’re using velocityX
and velocityY
If you replace those with just x
and y
it should work.
also to compact the code a bit, you could do
if (keyDown(“left”)) sprite.x–;
if (keyDown(“right”)) sprite.x++;
if (keyDown(“up”)) sprite.y–;
if (keyDown(“down”)) sprite.y++;
The reason right
and down
won’t work is because they will always get nullified by the if/else statement below. If right
is pressed, the lines below will set velocity to 0.
The best way to fix this is by incrementing/decrementing the x/y values, otherwise
if (keyDown("left")) {
shield.velocityX = -1
} else if (keyDown("right")){
shield.velocityX = 1
} else {
shield.velocityX = 0
}
This could also be simplified to
shield.velocityX = keyDown("left")?-1:(keyDown("right")?1:0)
Similarly,
shield.velocityY = keyDown("up")?-1:(keyDown("down")?1:0)
Hi @koliner,
@infinitestasis is on the right track here: The student is using “if” and “else” statements setting velocity to 0 if the up & left keys aren’t pressed, so that’s the last thing the computer sees.
I’d have them read the documentation on if/else statements, with a nudge toward the “else if” statements.
Comment back here if they need more help & we’ll see if we can assist further.
Cheers,
–Michael K.