简体   繁体   English

requestAnimationFrame无法按预期的Java脚本运行

[英]requestAnimationFrame not working as intended Javascript

I'm making a game in which a player character moves left and right. 我正在做一个游戏,其中玩家角色左右移动。 Since simply using an onKeyDown eventListener had my character move in a choppy fashion, and with a slight delay, I tried using requestAnimationFrame to call the movement function as often as possible, as suggested by another answer( How can I move my JS objects smoothly using keyboard input? ) 由于仅使用onKeyDown eventListener可使我的角色起伏不定,并且稍有延迟,所以我尝试使用requestAnimationFrame尽可能多地调用该移动函数,如另一个答案所建议的( 如何使用键盘输入?

however, that has changed nothing. 但是,这并没有改变。 Here is my Javascript Code 这是我的Javascript代码

var NodoCampo = document.getElementById("campo");
var NodoGiocatore = null;

var Left = false;
var Right = false;

var FRAMERATE = 20;

//cache giocatore
var LARG_GIOCATORE = 30;
var ALT_GIOCATORE = 30;
var X_GIOCATORE = 300;
var Y_GIOCATORE = 1100;
var VEL_GIOCATORE = 10;

function mostra_giocatore() {
    if (NodoGiocatore === null) {
        NodoGiocatore = document.createElement('div');
        NodoGiocatore.setAttribute ('id', 'player');
        NodoCampo.appendChild (NodoGiocatore);
    }
    NodoGiocatore.style.marginLeft = (X_GIOCATORE - LARG_GIOCATORE) + 'px';
    NodoGiocatore.style.marginTop = (Y_GIOCATORE - ALT_GIOCATORE) + 'px';
}

function muovi() {
    if (Left) {
        X_GIOCATORE = X_GIOCATORE - VEL_GIOCATORE;
        //aggiorno immagine
        mostra_giocatore();
    }
    else if (Right) {
        X_GIOCATORE = X_GIOCATORE + VEL_GIOCATORE;
        //aggiorno immagine
        mostra_giocatore();
    }
}

function stop() {
    Left = false;
    Right = false;
}

function interfaccia(e) {
    //freccia sinstra
    if (e.keyCode === 37) {
        X_GIOCATORE = X_GIOCATORE - VEL_GIOCATORE;
        //aggiorno immagine
        mostra_giocatore();
    }
    //freccia destra
    else if (e.keyCode === 39) {
        X_GIOCATORE = X_GIOCATORE + VEL_GIOCATORE;
        //aggiorno immagine
        mostra_giocatore();
    }

}

function inizia() {
    mostra_giocatore();
    requestAnimationFrame(muovi);
}

window.document.onkeypress = interfaccia;
window.document.onkeyup = stop;

Your choppy movement is likely a result of the amount you are moving the player on each frame with VEL_GIOCATORE . VEL_GIOCATORE动作可能是由于您使用VEL_GIOCATORE在每一帧上移动播放器的数量VEL_GIOCATORE Try reducing this amount to observe smoother movement. 尝试减少此量以观察更平稳的运动。

The delay you are experiencing is likely due to your operating system or browsers individual settings on how key presses should repeat. 您遇到的延迟可能是由于您的操作系统或浏览器在按键重复方式上的个别设置。 You can work around this by implementing your own key tracking -- it looks like you've started to experiment with this. 您可以通过实现自己的按键跟踪来解决此问题-好像您已经开始尝试此操作。 Track the state of your left and right keys by updating a boolean value in onkeydown and onkeyup event listeners. 跟踪你的状态, leftright通过更新一个布尔值,按键onkeydownonkeyup事件侦听器。

var keys = {
  left: false,
  right: false
};

window.document.onkeydown = function (e) {
  if (e.keyCode === 37) {
    keys.left = true;
  } else if (e.keyCode === 39) {
    keys.right = true;
}

window.document.onkeyup = function (e) {
  if (e.keyCode === 37) {
    keys.left = false;
  } else if (e.keyCode === 39) {
    keys.right = false;
}

Then, in your muovi function, check the state of these variables to determine if you should update the position of the player. 然后,在muovi函数中,检查这些变量的状态,以确定是否应更新播放器的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM