Implementing Basic Game Controls in HarmonyOS
This example demonstrates how to implement basic game controls using H5, leveraging the button component in ArkUI to showcase the ability to inject and execute JavaScript in a WebView, as well as communication between native applications and H5 pages.
The H5 game page used in this example is provided by https://yangyunhe369.github.io/h5-game-blockBreaker/.
Preview
Instructions:
- Connect your device to a hotspot to access the internet.
- Open the app and control the game using the buttons on the interface.
Project Structure
entry/src/main/ets/
|---entryability
| |---EntryAbility.ts // Dialog component
|---model
| |---Logger.ts // Logging utility
|---pages
| |---Index.ets // Home page
Implementation Details
This example uses a module that enables basic game control through buttons. It leverages the WebviewController methods to manage the behavior of the Web component, along with JavaScript injection and execution capabilities.
Permissions Required
Internet access permission: ohos.permission.INTERNET
Dependencies
No dependencies are required.
Constraints and Limitations
- This example runs only on standard systems.
- Its compatible with API version 9 SDK, version 3.2.11.9.
- It requires DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400, built on April 7, 2023) for compilation and execution.
Download
To download the project separately, run the following commands:
git init
git config core.sparsecheckout true
echo code/BasicFeature/Telephony/RunJsInWeb/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master
To further enhance your understanding of HarmonyOS development, we have prepared a complete set of the latest HarmonyOS NEXT learning resources. Click the link below to get the full version:
HarmonyOS Video Tutorial: Syntax of ArkTS, TypeScript, ArkUI, etc.
HarmonyOS Ecosystem Application Development White Paper V2.0 PDF
Getting Started
- Application Development Guide (ArkTS)
- HarmonyOS Concepts
- How to Get Started Quickly?
- Development Fundamentals
- ArkTS Development
For more information about HarmonyOS development, refer to: https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3