Cocos2d-x / Cocos Creator Deep Engineering Guide
Purpose
Cocos2d is a family of open-source game frameworks for 2D (and now 3D in Creator) with strong mobile focus. Cocos2d-x is the C++ core; Cocos Creator is the full editor + scene system built on top. This guide focuses on Cocos Creator 3.x (which uses Cocos2d-x under the hood).
Agent Protocol
Trigger
"cocos2d", "cocos2d-x", "cocos creator", "cocos game", "cocos build".
Input Context
- Cocos Creator or Cocos2d-x version
- Target platforms (web, iOS, Android, mini-games)
- Game type and performance targets
Output Artifact
TypeScript/C++ code, project settings or a performance plan. No file unless requested.
Response Format
Engine: {Cocos Creator 3.x | Cocos2d-x}
Change: {what and why}
Code: {snippet}
Perf check: {draw calls, memory, FPS on target device}
1. Engine Architecture
Cocos Creator 3.x
Application
Scene -> Node tree
Component (Script, Render, Audio, UI, Physics...)
Scheduler (main loop): tick() -> update() -> lateUpdate()
Director: scene management, frame pacing
- Director is the central controller;
director.mainLoop()runs the frame. - Nodes are the tree; Components (scripts) attach behavior.
- Scheduler: manages
update(),lateUpdate(),physicsUpdate()with priorities.
1.1 Update Order
update(dt) // component gameplay logic per frame
physics step // PhysicsSystem.instance.fixedTimeStep (default 1/60), may run 0..maxSubSteps times
lateUpdate(dt) // camera follow, UI
render // pipeline submit
Rules:
- Drive dynamic bodies through the physics API (
RigidBody.setLinearVelocity(),applyForce()), not by writing node positions every frame. - Components have no
fixedUpdate; for fixed-step logic listen to the physics system's step events or run your own accumulator inupdate.
2. The Node-Component Model
import { Component, Collider, ICollisionEvent, _decorator } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Player')
export class Player extends Component {
@property speed: number = 10;
update(deltaTime: number) {
// movement, animation, input
}
start() {
// 3D: collider events are registered, not overridden
this.getComponent(Collider)!.on('onCollisionEnter', this.onHit, this);
// 2D equivalent: collider2d.on(Contact2DType.BEGIN_CONTACT, cb, this)
}
private onHit(event: ICollisionEvent) {
// event.otherCollider, event.contacts
}
}
2.1 Pattern: Event-Driven
// Emit
this.node.emit('damage', amount);
// Listen
this.node.on('damage', this.onDamage, this);
node.on(): listen on the same node or bubble up.targetOff(): remove all listeners on cleanup.- Use typed events where possible (Cocos Creator 3.x supports
EventTarget).
2.2 Prefab Instantiation
import { director, instantiate, Prefab } from 'cc';
const bullet = instantiate(this.bulletPrefab);
director.getScene()!.addChild(bullet);
bullet.setWorldPosition(this.node.worldPosition);
Rule: cache prefab references via @property(Prefab), never load per frame.
3. Rendering
3.1 Pipeline Backends
| Platform | Renderer |
|----------|----------|
| Android / desktop GL | OpenGL ES 3 |
| iOS / Metal devices | Metal |
| Android Vulkan | Vulkan (via Cocos 3.x) |
| Web | WebGL 2 (WebGL 1 fallback); WebGPU experimental in 3.8 |
3.2 Draw Calls & Batching
- 2D renderables sharing material + texture are batched automatically; dynamic atlas merging can pack small textures at runtime. Interleaving different textures/materials in sibling order breaks batches.
- Use texture atlases (
TexturePacker) aggressively. - UI:
MaskandGraphicscomponents break batches; keep them few. Check the draw-call count in the Profiler overlay (profiler.showStats()).
3.3 Custom Rendering (Cocos Creator 3.x)
// Per-object uniform change without affecting the shared material
const renderer = this.getComponent(MeshRenderer)!;
renderer.getMaterialInstance(0)!.setProperty('mainColor', new Color(255, 0, 0, 255));
Material instances break batching/instancing for that object; prefer instanced attributes for many objects.
RenderPipeline(Forward, Deferred) set in Project Settings.- 2D games use Forward renderer; 3D can opt Deferred.
4. Physics
- Cocos Creator 3.x lets you pick the backend in Project Settings > Feature Cropping: 3D = Bullet (wasm, default), PhysX, cannon.js or builtin (collision only); 2D = Box2D or builtin.
- Nodes:
Collider2D/Collider3D+RigidBody2D/RigidBody3D. - Contact callbacks:
onBeginContact,onEndContact. - Layers:
physicsGroupin Project Settings.
4.1 Character Controller
const controller = this.node.getComponent(CharacterController);
controller.move(dir, deltaTime);
- For platformers:
RigidBody2D+BoxCollider2D, manual velocity control. - For 3D (Cocos Creator 3.8+): add a
CapsuleCharacterControllerorBoxCharacterControllercomponent and callmove(); it needs the Bullet or PhysX backend.
5. Audio
AudioSourcecomponent:play(),stop(),volume,loop.- Audio files: MP3 (loop), OGG (streaming), WAV (small SFX).
- Preload audio on scene load; never load audio per event.
6. UI (UI Toolkit equivalent: Cocos UI)
- Nodes:
Sprite,Label,Button,Slider,ScrollView,RichText. - Layout:
Layoutcomponent (horizontal/vertical/grid). - Anchor points:
[0-1, 0-1](unlike Unity's pivot). Canvasnode as root of UI tree; camera renders to UI layer.Widgetcomponent for responsive layout (attach to parent edges).
7. Scripting Languages
| Option | Use |
|--------|-----|
| TypeScript | Primary, first-class support in Cocos Creator |
| C++ | Engine customisation and native plugins exposed to TS through JSB bindings |
| Lua | Cocos2d-x only (legacy); not supported in Cocos Creator 3.x |
7.1 TypeScript Rules
- Use
@propertydecorators to expose fields. - Typed node references:
@property(Node) target: Node;. this.node: current node;this.node.parent: tree up.director.loadScene('sceneName')to switch scenes.
8. Multiplayer
- Cocos Creator has no built-in high-level netcode; use:
- WebSocket/HTTP for REST APIs.
- Native TCP/UDP via C++ bindings for real-time.
- Socket.IO plugin for event-based messaging.
- Rollback/interpolation implemented in user code; see
skills/game/multiplayer-netcode/SKILL.md.
9. Asset Pipeline & Build
- Assets/:
.tsscripts,.prefab,.scene,.texture,.anim. - Resource Manager (
resources.load()): load by path withinresources/folder. AssetManagerfor runtime loading, scene loading.- Build: Editor -> Build -> platform (iOS/Android/Web/Desktop) -> compile (Xcode/Gradle/Webpack).
- Remote asset bundle:
AssetManager.loadBundlefrom CDN for hot updates.
10. Performance Rules
- Profile: Cocos DevTools, Performance Monitor,
console.time(). - Node count: keep <5000 active; use culling (
NodePoolor visibility checks). - Object pool:
NodePoolfor bullets/enemies; neverinstantiate/destroyper event. destroy()is deferred;removeFromParent()+ pool recycling is faster.- Light count: keep 3D dynamic lights < 4 on mobile.
- Audio: limit simultaneous audio sources (3-5 on mobile).
- Memory: release unused
AssetManagerbundles explicitly.
10.1 Frame Budget (Mobile @ 30fps = 33ms)
| System | Budget |
|--------|--------|
| Gameplay | 8ms |
| Physics | 5ms |
| Render (CPU) | 8ms |
| Audio | 2ms |
| GC / misc | 5ms |
| GPU | 16.6ms (GPU-bound, overlap CPU) |
11. Anti-Patterns
| Anti-pattern | Consequence | Fix |
|--------------|-------------|-----|
| Instantiate/destroy per event | Allocation + GC | NodePool |
| All nodes ticking | CPU waste | disable update |
| No texture atlas | Draw calls explode | pack sprites |
| load() per frame | I/O stall | cache preloaded |
| Global variables | GC hits | scoped to class |
| this.schedule() in update | multiple timers | use one timer |
12. When to NOT Use Cocos2d
- AAA 3D → Unreal/Unity.
- Web-only lightweight → Phaser, vanilla Canvas.
- Scripting-heavy desktop → Godot or Unity.
- Console-first → Unreal or Unity.
13. References
skills/game/game-development/cocos2d-patterns/SKILL.md— Cocos2d-x architecture deep diveskills/game/game-engine/patterns/SKILL.md— engine design pattern catalogskills/game/multiplayer-netcode/SKILL.md— networking approach referenceskills/game/game-development/vulkan/SKILL.md— low-level GPU rendering (mobile/desktop)