简体   繁体   English

暂停后如何进行Javascript函数调用

[英]How to make Javascript function call happen after pauses

I am building a simple game in using HTML5 Canvas/Javascript. 我正在使用HTML5 Canvas / Javascript构建一个简单的游戏。 However, the game will be passed Javascript functions (js injected into the browser). 但是,游戏将通过Javascript函数(将js注入浏览器)。 The code passed will be to move the character ie. 传递的代码将是移动字符,即。 "moveRight(); moveRight(); moveUp();". “ moveRight(); moveRight(); moveUp();”。 Currently, i have the character moving, however for "moveRight(); moveRight();", the character teleports itself to its new position. 当前,我正在移动角色,但是对于“ moveRight(); moveRight();”,角色会将自身传送到新位置。 I would like the character to moveRight and then take a pause before moving right again. 我希望角色先向右移动,然后暂停一下,然后再向右移动。

游戏画面

function right(){
   window.setTimeout(function() {
      character.x += 30;
   }, 2000); 
}

How can I accomplish something like this. 我该如何完成这样的事情。 I tried using a timeout, but it did not help me much. 我尝试使用超时,但并没有太大帮助。

Thanks, 谢谢,

The best way to approach this would be to use requestAnimationFrame , and use a counter for when the player is able to move. 解决此问题的最佳方法是使用requestAnimationFrame ,并在玩家能够移动时使用计数器。

setInterval won't work for this situation, since you need to acknowledge cases where the player moves the other way around, or when the player cancels the movement. setInterval在这种情况下不起作用,因为您需要确认玩家反方向移动或玩家取消移动的情况。

It's hard to understand your current logic without more code, but below you will find an approach to a grid based movement. 没有更多代码很难理解您当前的逻辑,但是下面您将找到一种基于网格的移动方法。

 const c = document.getElementById('canvas'); c.width = window.innerWidth; c.height = window.innerHeight; const ctx = c.getContext('2d'); // player delay for movement const playerMovementDelay = 2; const tileSize = 32; // player starting position const myPlayer = {x: 1, y: 1, h: 1, w: 1}; let pkl = 0, pkr = 0, pku = 0, pkd = 0; let pvelx = 0, pvely = 0; let movementCountdown = 0; function render() { // player logic movementCountdown -= 0.16; const deltax = pkr + pkl; const deltay = pkd + pku; if (movementCountdown <= 0 && (deltax != 0 || deltay != 0)) { movementCountdown = playerMovementDelay; pvelx = deltax; pvely = deltay; } const speed = 1; myPlayer.x += pvelx * speed; myPlayer.y -= pvely * speed; pvelx = pvely = 0; ctx.clearRect(0, 0, c.width, c.height); // player render ctx.fillStyle = '#FFD9B3'; ctx.fillRect(myPlayer.x * tileSize, myPlayer.y * tileSize, myPlayer.w * tileSize, myPlayer.h * tileSize); window.requestAnimationFrame(render); } window.addEventListener('keydown', e => { if (e.key == 'ArrowRight') { pkr = 1; e.preventDefault(); } else if (e.key == 'ArrowLeft') { pkl = -1; e.preventDefault(); } if (e.key == 'ArrowUp') { pku = 1; e.preventDefault(); } else if (e.key == 'ArrowDown') { pkd = -1; e.preventDefault(); } }); window.addEventListener('keyup', e => { if (e.key == 'ArrowRight') { pkr = 0; } else if (e.key == 'ArrowLeft') { pkl = 0; } if (e.key == 'ArrowUp') { pku = 0; } else if (e.key == 'ArrowDown') { pkd = 0; } }); window.requestAnimationFrame(render); 
 body, html { margin: 0; } 
 <canvas id="canvas"></canvas> 

You can use setInterval for the same. 您可以将setInterval用作同一对象。

var rightCount = 1;
var moveRight = function () {
    rightCount++;
    character.x += 30;
}

var move = function () {
    setInterval(function () {
        if (rightCount < 2) {
            moveRight();
        }
    }, 2000)
}

move();

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

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