How do I write code so when the sprite jumps, the key press is disabled until the sprite reaches the ground again? So you can’t keep going up and up…
Hi @anna.arkema,
Do you have a sample student project for us to look at? There are several ways you can make this behavior work, but it’ll be better if we can look at what the student has done so far so we can suggest ways to help the student arrive at their own answer rather than us just giving them an example of a working code.
–Michael K.
Here is his code:
World.frameRate = 45;
// Create your variables here
var up = 0;
var right = 0;
// Create your sprites here
var cityBlock = createSprite(400, 175);
cityBlock.setAnimation(“cityblock”);
var cityBlock2 = createSprite(1200, 175);
cityBlock2.setAnimation(“cityblock2”);
var player = createSprite(100, 320);
player.setAnimation(“man”);
player.setCollider(“rectangle”, 0, 0, 20, 100, 0);
var collider = createSprite(100, 380);
collider.setAnimation(“collider”);
collider.scale = 0.2;
collider.visible = false;
player.debug = false;
collider.debug = false;
function draw() {
// draw background
background(rgb(124, 221, 213));
noStroke();
rect(0, 350, 400, 50);
// update sprites
playerPhysics();
movePlayer();
moveBG();
repeatBG();
jumping();
jumpOnce();
solidGround();
drawSprites();
}
// Create your functions here
function playerPhysics(){
player.velocityY = player.velocityY + 0.25;
}
function movePlayer(){
if (keyDown(“right”)) {
player.setAnimation(“manA”);
} else {
player.setAnimation(“man”);
}
}
function moveBG(){
if (keyDown(“right”)) {
cityBlock.x = cityBlock.x - 3;
cityBlock2.x = cityBlock2.x - 3;
}
}
function repeatBG(){
if (cityBlock.x < -401){
cityBlock.x = 1200;
}
if (cityBlock2.x < -401){
cityBlock2.x = 1200;
}
}
function jumping(){
if (keyWentDown(“up”)){
player.velocityY = -4;
}
if (player.y < 320){
player.setAnimation(“manJ”);
}
}
function jumpOnce(){
}
function solidGround(){
if (player.y >= 320){
player.y = 320;
}
if (player.isTouching(collider)){
player.collide(collider);
}
}
Although this may help, it would be easier if you could have him click on the share link and share a link to the actual project. The code itself is harder to debug without us having access to the sprites he used and the share link will let us see the project as he sees it.
Sorry for the extra step!
Mike
What about changing the jumping function a little, so when the up key is pressed, it checks for the jumper’s current location. If the jumper is on the ground, it jumps (as it is currently coded), but if it’s in the air, it doesn’t.
I think that would be one simple way to accomplish this task.
Mike
Ok he understands the checking the current location, but his question still is how do you create boolean where if it comes up true the sprite can jump, but if it comes up false the sprite cannot jump?
In this portion: function jumping(){
if (keyWentDown(“up”)){
player.velocityY = -4;
}
if (player.y < 320){
player.setAnimation(“manJ”);
}
}
function jumpOnce(){
}
He wants: “if the player is touching a colllider underneath then he can jump, but if he is not touching anything he has NO choice but to fall back down” He can jump once and falls back down.
Right now, if the up key is pressed, the velocity of the jumper changes.
Instead, after the up key is pressed, I would have another conditional that checked if it was on the ground (ie. jumper.y>=320). Only if it was > or = to 320 would I allow the velocity to change.
If the jumper is in the air (<320), the velocity would not be affected.
Does this help?
Mike