Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

javascript - I can't make dino jump

the complete code is on gitgub. full code on github the chrome game dino, dino is not jumping. thats handle key up:

function handleKeyUp(event){
    if (event.KeyCode === 32) {
        if (!isJumping) {
            jump();
        }
    }
}

and that's jump function:

function jump() {
    isJumping = true;

    let upInterval = setInterval (() => {
        if (position >= 150) {
            clearInterval(upInterval);
             //descendo
             let downInterval = setInterval(() => {
                 if (position <= 0) {
                     clearInterval(downInterval);
                     isJumping = false;
                 }
                 else {
                 position -= 20;
                 dinito.sytle.bottom = position + 'px';
                 }
             }, 20);
        }
        else {
        //subindo
        position += 20;
        dinito.sytle.bottom = position + 'px';
        }
    }, 20);
}
question from:https://stackoverflow.com/questions/65846396/i-cant-make-dino-jump

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You've spelt style incorrectly.

dinito.sytle.bottom

Should be

dinito.style.bottom

This appears in 2 places.

Your github doesn't seems to be up to date so I've added bits to get it working.

Declaring variables with initial values...

var position = 0;
var isJumping = false;
var dinito = document.getElementById("dinito");

Added an id to dinito

 <div id="dinito" class="dinito"></div>

Changed the way keycode is obtained

 if (event.code === "ArrowUp") {

Here is a working fiddle of all the above changes: https://jsfiddle.net/4cs97fmk/

If it doesn't work then you need to click on the page before keyboard event start working.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...