Cocos Creator HTML5 Game Tutorial: Build and Export Your First Web Game
Cocos Creator HTML5 Game Tutorial: Build and Export Your First Web Game
Honesty note: I chose Phaser for my game (decision explained in the engine deep-dive), but I evaluated Cocos Creator closely for its web-first export and mini-game ecosystem (the comparison). This tutorial is written from Cocos’s official docs and public 2026 community practice — accurate as a roadmap, not as my own shipped project.
TL;DR
- Cocos Creator is a visual, node-component editor built for web delivery — you drag nodes into a scene, attach components (including TypeScript scripts), and hit Build → Web. First export is a folder you can deploy anywhere (or import straight into portals/mini-game platforms).
- Learn four editor panels and you can build: Scene (the visual canvas), Hierarchy (node tree), Inspector (component properties on the selected node), Assets (your files). Everything else is convenience.
- The mental model: Scene = tree of Nodes; behavior = Components. A script component is a TypeScript class with lifecycle hooks (
onLoad,update(dt)) — attach it to a node and it runs. That’s 90% of Cocos. - TypeScript is first-class — Cocos scripts are
.tsby default, with decorators (@ccclass,@property) that expose fields in the Inspector. If you want to learn one language for both Cocos and web, TS is it (my notes on adding TS to a Phaser stack).
The four panels that matter
| Panel | What it is | What you do there |
|---|---|---|
| Scene | The visual canvas | Drag/position/scale nodes; see the game |
| Hierarchy | The node tree | Create/rename/parent nodes; the structure of the scene |
| Inspector | Properties of the selected node | Set components, edit @property fields, tweak values |
| Assets | Your files | Import art/audio, create scripts, manage prefabs |
Open the editor, and those four are your cockpit. A beginner tutorial that teaches these four first is teaching the right 20%.
The node-component model in practice
A Cocos scene is a tree of Nodes; a node is a container with a transform (position/rotation/scale). Behavior comes from Components attached to nodes:
- Built-in components: Sprite (render image), Label (text), Button, Canvas (the root of UI), Camera.
- Script components: your own TypeScript class with lifecycle hooks.
// Your first script component — attach to a node and it runs
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('PlayerMove')
export class PlayerMove extends Component {
@property speed: number = 200; // shows up in the Inspector, editable live
update(dt: number) {
// dt = seconds since last frame; move the node every frame
this.node.setPosition(
this.node.position.x + this.speed * dt,
this.node.position.y
);
}
}
That script attached to a node is a working moving sprite — the “hello world” of Cocos. The @property decorator is the bridge: declare a field, it appears in the Inspector, and you tune values without code.
From scene to playable: the 10-step path
- Create a project — Cocos Dashboard → New Project → Empty (2D) template.
- Build the scene — create a Canvas node (UI root), add a Sprite node under it.
- Import art — drag a PNG into the Assets panel; drag it onto the Sprite to set the image.
- Add behavior — create a
.tsscript (right-click Assets → Create → TypeScript), write a component, drag the script onto the node. - Wire input — the
inputmodule (input.on(Input.EventType.TOUCH_START, ...)) or the Button component’s click event. - Tune in Inspector — set
@propertyvalues while the game preview runs (live editing). - Preview — the built-in preview runs the game in browser instantly.
- Build → Web — Project → Build: choose Web Mobile (for browsers/portals) or Web Desktop; Cocos outputs a deployable folder.
- Deploy — upload that folder to any static host (Cloudflare Pages, etc.) or package for a portal; for mini-games, choose the WeChat/ByteDance build target instead (portal publishing).
- Optimize — check the built size, keep textures compressed, and watch draw calls (the 60fps budget).
The honest part: what to watch for
- Build size discipline still applies. Cocos web builds are leaner than Godot’s default (that’s a Cocos strength — numbers here), but a fat asset folder still bloats load; compress and check the gzipped size (size guide).
- Scripting matters more than the visual editor as the game grows — the node tree gets complex fast; keep logic in script components, not in fiddly inspector wiring.
- TS is the real language — if you’re not comfortable with TypeScript, budget learning time; it’s the same language you’d use on the web anyway.
- The mini-game path is the differentiator — if WeChat/ByteDance mini-games are the goal, Cocos’s first-class build targets beat most alternatives (comparison).
Pitfalls
- Clicking through panels without the model — understand node-component first; everything else follows.
- Giant textures imported as-is — art goes in at PNG size; compress to what the game actually displays (asset pipeline applies to any engine).
- No prefab discipline — reuse scenes/systems as prefabs; hand-duplicating nodes is how scenes rot.
- Skipping TypeScript — fighting the editor to avoid code is a losing battle as complexity grows.
- Ignoring export size — measure the gzipped web build before portal submission (why).
Bottom line
Cocos Creator’s path to a first web game is genuinely short: four editor panels, the node-component model, one TypeScript component, and Build → Web. The visual editor removes boilerplate; TypeScript gives it real structure as you grow; the mini-game export story is its strongest differentiator. It’s a legitimate alternative to Phaser for web-first developers who want a visual editor (the honest engine comparison) — and this tutorial gives you the 20% of Cocos that does 80% of the work.