This guide targets Cocos2d-x 3.17 with Lua. Project-generation commands and SDK paths vary by operating system, but the engine concepts below are stable within the 3.x bindings.
Screen design and sprites
Configure the design resolution once and choose an appropriate policy for the supported aspect ratios. Place important controls inside a safe layout region rather than assuming one fixed screen size.
1 | local sprite = cc.Sprite:create("images/hero.png") |
Buttons and touch
1 | local button = ccui.Button:create("button.png") |
For custom touch handling, create cc.EventListenerTouchOneByOne, return true only when the node claims the touch, and handle began, moved, ended, and cancelled states. Convert coordinates into the target node’s local space before hit testing.
Storage
Small preferences can use cc.UserDefault:
1 | local store = cc.UserDefault:getInstance() |
Do not treat it as secure storage. Structured saves need versioning, validation, atomic writes, and platform-appropriate protection for sensitive values.
Actions and scheduling
1 | sprite:runAction(cc.Sequence:create( |
Use the scheduler for repeated updates and unschedule callbacks when their owner exits. Frame animation uses cc.Animation and cc.Animate; UI timelines use CSB actions.
Geometry
Use cc.rectContainsPoint for point tests and cc.rectIntersectsRect for axis-aligned rectangle overlap. Direction angles can be calculated with math.atan2 where provided by the runtime or the equivalent math.atan(y, x) form in newer Lua versions. Always verify coordinate spaces before comparing geometry.
Build scenes from small nodes with clear ownership, retain only needed references, and centralize cleanup for listeners, timers, textures, and asynchronous callbacks.