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

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
主站蜘蛛池模板: 武乡县| 吉林市| 溧水县| 射洪县| 武鸣县| 渑池县| 屏边| 柯坪县| 金川县| 沙洋县| 南昌市| 桃园市| 青州市| 山阳县| 牡丹江市| 仁布县| 芦溪县| 高要市| 安西县| 松原市| 苍南县| 天津市| 德阳市| 偏关县| 富阳市| 明光市| 蕲春县| 滁州市| 肇东市| 福州市| 龙游县| 来安县| 隆化县| 通州区| 喀喇沁旗| 邯郸县| 阿合奇县| 措美县| 雅江县| 萍乡市| 荃湾区|