简体   繁体   English

JS / Canvas - 如何仅在按下键时更新 draw() 函数?

[英]JS / Canvas - How do I update the draw() function only when key is pressed?

edit: solved it myself, pretty sure it's not the ideal way to do it (:D ) but it works for now:P编辑:我自己解决了,很确定这不是理想的方法(:D)但现在可以使用:P

solution:解决方案:

document.addEventListener('keydown', e => {
  switch (e.keyCode) {
    case 37:
      game.toggleTurn();
      gameLoop();
      break;

    case 39:
      game.toggleTurn();
      gameLoop();
      break;

    case 40:
      game.toggleTurn();
      gameLoop();
      break;

    case 38:
      game.toggleTurn();
      gameLoop();
      break;
  }
});

I'm trying to make a "roguelike" game for practice in canvas and I have ran into some trouble.我正在尝试制作一个“roguelike”游戏以在画布上进行练习,但遇到了一些麻烦。

What I would like to do:我想做什么:

I would like to implement a "turn" system to my game loop, so that it only runs when the turn event happens.我想在我的游戏循环中实现一个“转弯”系统,这样它只在转弯事件发生时运行。

Here is some code so you can see what I'm talking about:这是一些代码,因此您可以看到我在说什么:

my index.js我的索引.js


let canvas = document.getElementById('gameScreen');
let ctx = canvas.getContext('2d');

const GAME_WIDTH = 1024;
const GAME_HEIGHT = 1024;

ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

let lastTime = 0;

let game = new Game(GAME_WIDTH, GAME_HEIGHT);
game.start();

function gameLoop() {

  ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

  game.draw(ctx);
  requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);

my game.js:我的游戏.js:

import Player from './player.js';
import InputHandler from './input.js';
import { buildLevel, map1 } from './levels.js';

export default class Game {
  constructor(gameWidth, gameHeight) {
    this.gameWidth = gameWidth;
    this.gameHeight = gameHeight;
    this.player = new Player(this);
    this.gameObjects = [];
    this.walls = [];

    this.levels = [map1];
    this.currentLevel = 0;
    new InputHandler(this.player, this);
  }

  start() {
    this.walls = buildLevel(this, this.levels[this.currentLevel]);
    console.log('yes');
    this.gameObjects = [this.player];
  }

  update(deltaTime) {
    console.log('update');
  }

  draw(ctx) {
    console.log('draw');
    [...this.gameObjects, ...this.walls].forEach(object => object.draw(ctx));
  }
}

So right now, I can start the game, generate the map, I have the player and I can move him.所以现在,我可以开始游戏,生成地图,我有玩家并且可以移动他。 The thing that bugs me, is that the "draw()" function is running in a loop, all the time.让我烦恼的是,“draw()”函数一直在循环运行。

What would be the correct way to re-draw the game, or player, only when the turn event is triggered?只有在触发转弯事件时才重新绘制游戏或玩家的正确方法是什么?

Hope I didn't confuse you, thanks !希望我没有混淆你,谢谢!

You could use something similar to this.你可以使用类似的东西。 You can change the state of the isTurn boolean in your turn event and use an if within the gameLoop to check its value and stop the loop.您可以在 turn 事件中更改isTurn布尔值的状态,并在 gameLoop 中使用if检查其值并停止循环。

var isTurn = true;

function pauseGame() {
    isTurn = !isTurn;
    if (!isTurn) gameLoop();
}

function gameLoop() {

    if (isTurn) return;
    ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

    game.draw(ctx);
    requestAnimationFrame(gameLoop);
}

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

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