Cocos2d

From j4flmao/agent-skills · View interactive version

Cocos2d is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about Cocos Creator 3.x, Cocos2d-x, a Cocos game, node and component scripting in TypeScript, Cocos builds for web or mobile, or Cocos performance. Produces Cocos project structure, gameplay code and build settings. Not for Cocos2d-x engine internals (use cocos2d-patterns) or

How to use this skill

Skill file

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

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

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:

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);

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

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.

4. Physics

4.1 Character Controller

const controller = this.node.getComponent(CharacterController);
controller.move(dir, deltaTime);

5. Audio

6. UI (UI Toolkit equivalent: Cocos UI)

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

8. Multiplayer

9. Asset Pipeline & Build

10. Performance Rules

  1. Profile: Cocos DevTools, Performance Monitor, console.time().
  2. Node count: keep <5000 active; use culling (NodePool or visibility checks).
  3. Object pool: NodePool for bullets/enemies; never instantiate/destroy per event.
  4. destroy() is deferred; removeFromParent() + pool recycling is faster.
  5. Light count: keep 3D dynamic lights < 4 on mobile.
  6. Audio: limit simultaneous audio sources (3-5 on mobile).
  7. Memory: release unused AssetManager bundles 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

13. References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.