cocos2dx-lua

Coding guidance for Cocos2d-x Lua, a Lua-based game development framework. It covers scene objects, touch input, animations, scheduled callbacks, and cleanup of memory and event handlers.

In plain words
What is it for?
Writing or reviewing Cocos2d-x Lua scenes, layers, interfaces, touch handling, actions, schedulers, and object cleanup.
Why use it?
It helps prevent problems such as lingering timers, touch events reaching the wrong layer, failed animations, crashes after scene changes, and memory that keeps growing.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/cocos2dx-lua
Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill cocos2dx-lua
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Per session 38 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,191 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00038 $0.03191
Opus 5 $0.00019 $0.01596
Sonnet 5 $0.00008 $0.00638
Haiku 4.5 $0.00004 $0.00319

Measured 2d ago against content hash 9134c3b9247c, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

cocos2dx-lua scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/cocos2dx-lua/SKILL.md · 197 lines

How it starts

The opening of the file, as written. The whole thing — 197 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Cocos2d-x Lua 最佳实践

何时用

  • 编写任何 Cocos2d-x Lua 场景、Layer、UI 组件脚本前。
  • 发现内存持续增长、退出场景后内存未释放时。
  • 触摸事件穿透、多层点击响应混乱,或事件监听器未被清理时。
  • 动作(Action)执行异常、Scene 切换后动画仍在报错时。
  • 调度器回调在节点销毁后仍被调用,导致访问空节点崩溃时。

核心规则

1. 节点生命周期:addChild/removeChild 必须配对,退出前清理一切

规则: 每个 addChild 都要有对应的清理路径(removeChild 或场景切换自动销毁);onEnter/onExit 是注册/注销外部资源(调度器、事件)的标准时机,不要在构造函数或任意时机注册后忘记注销。

为什么: Cocos2d-x 的 C++ 底层使用引用计数(retain/release),Lua 侧持有的 userdata 会阻止对象释放。AI 生成代码时最常犯的错误:在一个 Layer 里 addChild 了子节点,场景切换时只 removeFromParent 了 Layer,但子节点上挂的调度器回调和事件监听器没有清理,C++ 对象引用计数不归零,内存持续增长。新手则常在 init 里注册调度器,在 onExit 里忘记反注册,下次进场景又注册一次,定时器越堆越多。

怎么做:

  • onEnter 里注册调度器和事件监听器,在 onExit 里注销。
  • 场景/Layer 退出时调用 node:unscheduleAllCallbacks()eventDispatcher:removeEventListenersForTarget(node)
  • 临时创建的节点用完即 removeFromParent(true)(true = cleanup,会停止其上的 Action 和调度器)。
  • 养成习惯:每加一个子节点,立刻想好"它什么时候被移除"。

2. 触摸事件:EventListener 正确注册与移除,处理吞噬与层级冲突

规则: 触摸事件必须用 cc.EventListenerTouchOneByOnecc.EventListenerTouchAllAtOnce 注册到 eventDispatcher,不用旧的 setTouchEnabledsetSwallowTouches(true) 只在确实需要阻止穿透时设置,并理解其对下层 Listener 的影响;节点移除前必须 removeEventListenersForTarget

为什么: Cocos2d-x Lua 里触摸 bug 几乎全部源于两个问题:(1)新手混用新旧 API——layer:setTouchEnabled(true) 在 3.x 里已废弃,和 EventDispatcher 的优先级体系完全独立,导致点击没响应却不报错,排查极为困难;(2)AI 生成的 Listener 注册了但没有对应的注销,节点 removeChild 后 C++ 对象已析构,触摸事件仍然回调进来,访问已释放的 Lua userdata 直接崩溃。setSwallowTouches 设错方向时,弹窗后面的按钮照样可以被点击,产生逻辑混乱。

怎么做:

  • 统一用 cc.EventListenerTouchOneByOne:create() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, node)
  • 弹窗、遮罩层上设 listener:setSwallowTouches(true) 拦截穿透;普通 UI 组件默认不吞噬,让事件往下传。
  • onExit 里:cc.Director:getInstance():getEventDispatcher():removeEventListenersForTarget(self)
  • 多层 UI 共存时,用 addEventListenerWithFixedPriority 显式指定优先级,避免隐式顺序导致的不确定性。

3. 调度器:用引擎 scheduler,销毁前一定 unschedule

规则: 所有定时逻辑通过 node:scheduleOncenode:schedule、或 cc.Director:getInstance():getScheduler():scheduleScriptFunc 驱动;不自建基于 update 计数器的"假计时器";节点销毁前调用 node:unscheduleAllCallbacks()

Read the full file on GitHub · 197 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 197 lines · 38 tokens per session scan A 9134c3b9247c

Subscribe to this mod's changes

cocos2dx-lua is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (6 stars, last pushed 2mo ago), licensed MIT. It adds 38 tokens to every session and 3,191 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

chinese-documentation

中文文档排版参考——中英文空格、全半角标点、术语保留、链接格式、中文文案排版指北约定。仅在用户显式 /chinese-documentation 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-git-workflow

国内 Git 平台配置参考——Gitee、Coding.net、极狐 GitLab、CNB 的 SSH/HTTPS/凭据/CI 接入差异与镜像同步配置。仅在用户显式 /chinese-git-workflow 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 69 tokens

brainstorming

在任何创造性工作之前必须使用此技能——创建功能、构建组件、添加功能或修改行为。在实现之前先探索用户意图、需求和设计。.

jnMetaCode/superpowers-zh · 40 tokens

chinese-code-review

中文 review 沟通参考——话术模板、分级标注(必须修复/建议修改/仅供参考)、国内团队常见反模式应对。仅在用户显式 /chinese-code-review 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-commit-conventions

中文 commit 与 changelog 配置参考——Conventional Commits 中文适配、commitlint/husky/commitizen 中文模板、conventional-changelog 中文配置。仅在用户显式 /chinese-commit-conventions 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 65 tokens

mcp-builder

MCP 服务器构建方法论 — 系统化构建生产级 MCP 工具,让 AI 助手连接外部能力.

jnMetaCode/superpowers-zh · 32 tokens