executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
cocos2d-patternsCocos2d-x architectural patterns - Director main loop, scene graph, render queue and batching, event system, memory management, and cross-platform build patterns.
Independent GitHub snapshot · Automated classification · Original documentation governs
Cocos2d-x has a mature scene-graph architecture centered on the Director and Node tree. Understanding its internals lets you design for performance: batched rendering, minimal state changes, and memory discipline on mobile.
The Director singleton drives the game.
Director::mainLoop()
-> calculateDeltaTime()
-> update() // scheduled updates + physics step (if enabled)
-> drawScene() // render current scene
Director::runWithScene() sets the initial scene.Director::replaceScene() swaps current scene (with optional transition).Director::pushScene()/popScene() for pause menus layered over gameplay.Director::setAnimationInterval(1.0/60.0) sets the desired FPS.setProjection/retina scaling (CC_CONTENT_SCALE_FACTOR).Director::setHighResFactor(0) for pure-logic runs.Node is the base: position, rotation, scale, skew, opacity, anchor point, children, z-order.addChild(node, zOrder).visit().setAnchorPoint() affects position relative to bounds (Cocos uses anchor = [0,1] vs Unity center).Vec2/Size; world coordinates via convertToWorldSpaceAR.EventListenerTouchOneByOne / EventListenerTouchAllAtOnce.EventDispatcher::dispatchCustomEvent("name", &data).Input singleton.node->addEventListener()); remember to remove on onExit.Draw calls are NOT issued during traversal. Node::visit() pushes RenderCommands into a Renderer queue:
visit() -> pushCommand(RenderCommand) for each node
Renderer::render() -> sort commands (by z-order; transparent after opaque)
-> group by texture/shader to batch draw calls
-> flush GPU commands
TextureAtlas: keep many sprites sharing ONE texture; they draw in one batch.Renderer batches consecutive commands with the same texture + shader.SpriteBatchNode for repeated textures.Node and override draw(Renderer*) to push custom TrianglesCommand/CustomCommand.Renderer supports custom geometry (a quad list) — great for particle trails, vector UI.Actions: MoveTo, Sequence, Spawn, RepeatForever, EaseIn.node->runAction(...); actions run inside Node::i update loop on the main thread.Sequence/Spawn composite actions; ActionInterval for timed.Director::getScheduler() controls update priorities of actions.scheduler->schedule(...) with time intervals; update(dt) for every frame.scheduleUpdate() / unscheduleUpdate() on nodes.Director::pause().PhysicsBody / PhysicsWorld or direct Box2D).PhysicsBody::createBox, PhysicsWorld integrated to scene.EventListenerPhysicsContact.Create a body with PhysicsMaterial(), set setSensor(true) for triggers, listen onContactBegin for overlap detection.
Ref/RefCounted-style retention: retain()/release() (Cocos uses its own Ref).TextureCache::sharedTextureCache()->addImage() caches textures (avoid duplicate loads).SpriteFrameCache for atlases.ResourcesManager for transparent asset handling.removeUnusedTextures() after scene transitions.// recycle simple objects to avoid alloc/free churn
auto pool = NodePool::getInstance("bullet");
auto bullet = pool->getFromPoolOrCreate(...);
bullet->setPosition(pos);
this->addChild(bullet);
// on death: pool->returnToPool(bullet)
platform/): iOS/Win32/Android/Mac/Linux; define __APPLE__, CC_PLATFORM_* macros.FileUtils::getInstance()->fullPathForFilename(); Assets folder mapped per-platform.AudioEngine::play2d; OGG/MP3/WAV platform-specific loading via AudioDecoder..res or Atlas; on Android use assets/.| Cocos2d-x (C++) | Cocos Creator (TS/editor) | |
|---|---|---|
| Language | C++ | TypeScript |
| Rendering | OpenGL ES/Metal | Vulkan/GL/Metal (multibackend) |
| Editor | none (code-first) | full editor + scene system |
| Use case | perf-critical, engine-level | team-based game dev |
| Anti-pattern | Consequence | Fix |
|---|---|---|
| One texture per sprite | Draw call explosion | atlas + batch node |
per-node retain() leaks | memory growth | pooled/refcache |
| Actions recreated each frame | alloc churn | reuse cached actions |
Global Director misuse in logic | scene-restart bugs | keep in scene node |
| blocking file loads | UI stalls | async load + cache |
flowchart TD
A{2D game?} -->|Yes| B{Same texture heavy?}
A -->|No| C[Use full 3D engine instead]
B -->|Yes| D[SpriteBatchNode + Atlas]
B -->|No| E[Standard Sprite renderer]
D --> F[pool + async load]
E --> F
skills/game/cocos2d/SKILL.md — full Cocos2d game dev guide