- Hands-On Game Development with WebAssembly
- Rick Battagline
- 317字
- 2021-06-24 13:41:08
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.
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
- 觸摸屏實(shí)用技術(shù)與工程應(yīng)用
- 電腦軟硬件維修大全(實(shí)例精華版)
- 計(jì)算機(jī)應(yīng)用與維護(hù)基礎(chǔ)教程
- 電腦組裝、維護(hù)、維修全能一本通(全彩版)
- Manage Partitions with GParted How-to
- 計(jì)算機(jī)組裝與維修技術(shù)
- Spring Cloud微服務(wù)架構(gòu)實(shí)戰(zhàn)
- Machine Learning with Go Quick Start Guide
- 計(jì)算機(jī)組裝維修與外設(shè)配置(高等職業(yè)院校教改示范教材·計(jì)算機(jī)系列)
- 單片機(jī)原理及應(yīng)用:基于C51+Proteus仿真
- 嵌入式系統(tǒng)原理及應(yīng)用:基于ARM Cortex-M4體系結(jié)構(gòu)
- Arduino項(xiàng)目案例:游戲開(kāi)發(fā)
- 筆記本電腦維修技能實(shí)訓(xùn)
- Raspberry Pi Home Automation with Arduino
- MicroPython Cookbook