官术网_书友最值得收藏!

Keyboard Input

Now that we have sprites and animations, and can move these sprites around our canvas, we will need to add some interaction into our game. There are a few ways we can get keyboard input for our game. One way is through JavaScript, making calls to different functions in our WebAssembly module based on that input. The first section of our code will do just that. We will add some functions inside the WebAssembly module for us to wrap in JavaScript wrappers. We will also set up some JavaScript keyboard event handlers that we will use to make calls into our WebAssembly module whenever the keyboard events are triggered.

The other way we can get input into our WebAssembly module is to allow SDL to do all the heavy lifting for us. That involves adding C code into our WebAssembly module that captures the SDL_KEYDOWN and SDL_KEYUP events. The module will then look at the event keycode to determine what key triggered the event. There are costs and benefits to writing our code using either method. Generally speaking, having SDL managing our keyboard input costs us some of the flexibility of writing our keyboard input manager inside the JavaScript, while, at the same time, we gain the benefit of more straightforward code.

You will need to include several images in your build to make this project work. Make sure you include the /Chapter05/sprites/ folder from the project's GitHub. If you haven't yet downloaded the GitHub project, you can get it online at:  https://github.com/PacktPublishing/Hands-On-Game-Development-with-WebAssembly.

In this chapter, we will do the following:

  • Learn how to use JavaScript keyboard events to make calls into our WebAssembly module
  • Learn how to use SDL events to manage keyboard input from inside our WebAssembly module
  • Demonstrate what we have learned by using keyboard input to move a spaceship sprite around the canvas
主站蜘蛛池模板: 华安县| 潢川县| 汉阴县| 深州市| 左云县| 彰化市| 岑巩县| 江西省| 安平县| 阿拉善左旗| 阿合奇县| 临夏县| 宁都县| 青铜峡市| 乌兰浩特市| 陇西县| 西乌珠穆沁旗| 宜都市| 彝良县| 金山区| 靖西县| 临洮县| 鲁山县| 连州市| 西畴县| 二手房| 太保市| 临夏市| 西平县| 资中县| 湖南省| 六盘水市| 临沧市| 宣城市| 昭平县| 锡林浩特市| 循化| 裕民县| 石棉县| 贵州省| 梁山县|