Cocos Creator 3.x Development Guide
Guide for Cocos Creator 3.x game development using TypeScript.
Quick Start
Basic Component Template
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('MyComponent')
export class MyComponent extends Component {
@property(Node)
targetNode: Node = null;
@property
speed: number = 10;
onLoad() {
// Initialization
}
start() {
// First frame update
}
update(deltaTime: number) {
// Per-frame update
}
}
Common Operations
Add component dynamically:
this.node.addComponent(MyComponent);
Get component:
const comp = this.node.getComponent(MyComponent);
Load scene:
director.loadScene('SceneName');
Load resource:
resources.load('textures/sprite', SpriteFrame, (err, asset) => {
this.node.getComponent(Sprite).spriteFrame = asset;
});
Reference Documentation
For detailed information on specific topics, consult these reference files:
- Script Basics: references/script-basics.md - TypeScript setup, decorators, class definitions, property declarations
- Component System: references/component-system.md - Lifecycle methods, component management, execution order
- Node & Hierarchy: references/node-hierarchy.md - Node creation, traversal, transformations, coordinate systems
- Scene Management: references/scene-management.md - Scene loading, persist nodes, preloading
- Resource Management: references/resource-management.md - Asset loading, releasing, Asset Bundles, memory management
- Event System: references/event-system.md - Input handling, custom events, node events
Development Guidelines
Version Check
Always confirm the user is working with Cocos Creator 3.x, not 2.x. APIs and workflows differ significantly.
Code Style
- Use TypeScript exclusively (JavaScript not supported in 3.x)
- Follow decorator pattern:
@ccclass, @property
- Use type annotations for all properties
- Prefer
this.node for current node access
Performance Best Practices
- Release unused resources with
assetManager.releaseAsset()
- Use object pooling for frequently created/destroyed nodes
- Cache component references instead of repeated
getComponent() calls
- Use
scheduleOnce instead of timers for one-time delays
Common Pitfalls
- Lifecycle order:
onLoad → start → update. Don't access other components in onLoad if they might not be ready.
- Resource leaks: Always release dynamically loaded assets
- Node activation: Check
node.active before accessing components
- Scene switching: Use persist nodes for data that needs to survive scene changes
Key APIs Reference
Node Operations
this.node - Current node
this.node.parent - Parent node
this.node.children - Child nodes array
this.node.addChild(child) - Add child node
this.node.removeFromParent() - Remove from parent
this.node.destroy() - Destroy node
this.node.setPosition(x, y, z) - Set position
this.node.setRotation(x, y, z) - Set rotation
this.node.setScale(x, y, z) - Set scale
Component Operations
this.node.addComponent(T) - Add component
this.node.getComponent(T) - Get component
this.getComponentInChildren(T) - Get component in children
this.getComponentsInChildren(T) - Get all components in children
Scene Operations
director.loadScene(name) - Load scene
director.preloadScene(name) - Preload scene
director.getScene() - Get current scene
game.addPersistRoot(node) - Add persist node
game.removePersistRoot(node) - Remove persist node
Resource Operations
resources.load(path, type, callback) - Load resource
assetManager.loadRemote(url, type, callback) - Load remote resource
assetManager.releaseAsset(asset) - Release resource
resources.loadDir(path, type, callback) - Load directory
Event Operations
this.node.on(type, callback, target) - Register event
this.node.off(type, callback, target) - Unregister event
this.node.emit(type, detail) - Emit event
input.on(type, callback, target) - Register input event
External Resources
Troubleshooting
Component Not Found
- Verify component is attached in editor
- Check node is active:
if (this.node.active)
- Use
getComponentInChildren() for nested components
Scene Not Loading
- Check scene name in Build Settings
- Verify scene is added to build
- Use
preloadScene() for large scenes
Memory Leaks
- Release assets after use
- Destroy unused nodes
- Remove event listeners in
onDestroy
- Use Asset Bundles for modular loading
Performance Issues
- Profile with Chrome DevTools
- Reduce draw calls with batching
- Use object pooling
- Optimize sprite atlases
- Check script execution time in updates
When to Read References
Read specific reference files when:
- script-basics.md: Creating new components, defining properties, configuring TypeScript
- component-system.md: Implementing lifecycle callbacks, managing component dependencies
- node-hierarchy.md: Creating/destroying nodes, building scene hierarchy, coordinate transformations
- scene-management.md: Loading scenes, passing data between scenes, managing game flow
- resource-management.md: Loading assets, managing memory, using Asset Bundles
- event-system.md: Handling user input, implementing custom events, component communication
Decision Guide
Multiple valid approaches? Use high freedom - provide options and let context guide choice.
Standard pattern exists? Use medium freedom - provide template code with parameters.
Error-prone operation? Use low freedom - provide specific, validated code pattern.
1---2name: cocos-creator-dev3description: Cocos Creator 3.x Development Guide4---56# Cocos Creator 3.x Development Guide78Guide for Cocos Creator 3.x game development using TypeScript.910## Quick Start1112### Basic Component Template1314```typescript15import { _decorator, Component, Node } from 'cc';16const { ccclass, property } = _decorator;1718@ccclass('MyComponent')19export class MyComponent extends Component {20 @property(Node)21 targetNode: Node = null;2223 @property24 speed: number = 10;2526 onLoad() {27 // Initialization28 }2930 start() {31 // First frame update32 }3334 update(deltaTime: number) {35 // Per-frame update36 }37}38```3940### Common Operations4142**Add component dynamically:**43```typescript44this.node.addComponent(MyComponent);45```4647**Get component:**48```typescript49const comp = this.node.getComponent(MyComponent);50```5152**Load scene:**53```typescript54director.loadScene('SceneName');55```5657**Load resource:**58```typescript59resources.load('textures/sprite', SpriteFrame, (err, asset) => {60 this.node.getComponent(Sprite).spriteFrame = asset;61});62```6364## Reference Documentation6566For detailed information on specific topics, consult these reference files:6768- **Script Basics**: [references/script-basics.md](references/script-basics.md) - TypeScript setup, decorators, class definitions, property declarations69- **Component System**: [references/component-system.md](references/component-system.md) - Lifecycle methods, component management, execution order70- **Node & Hierarchy**: [references/node-hierarchy.md](references/node-hierarchy.md) - Node creation, traversal, transformations, coordinate systems71- **Scene Management**: [references/scene-management.md](references/scene-management.md) - Scene loading, persist nodes, preloading72- **Resource Management**: [references/resource-management.md](references/resource-management.md) - Asset loading, releasing, Asset Bundles, memory management73- **Event System**: [references/event-system.md](references/event-system.md) - Input handling, custom events, node events7475## Development Guidelines7677### Version Check7879Always confirm the user is working with **Cocos Creator 3.x**, not 2.x. APIs and workflows differ significantly.8081### Code Style8283- Use **TypeScript** exclusively (JavaScript not supported in 3.x)84- Follow decorator pattern: `@ccclass`, `@property`85- Use type annotations for all properties86- Prefer `this.node` for current node access8788### Performance Best Practices8990- Release unused resources with `assetManager.releaseAsset()`91- Use object pooling for frequently created/destroyed nodes92- Cache component references instead of repeated `getComponent()` calls93- Use `scheduleOnce` instead of timers for one-time delays9495### Common Pitfalls9697- **Lifecycle order**: `onLoad` → `start` → `update`. Don't access other components in `onLoad` if they might not be ready.98- **Resource leaks**: Always release dynamically loaded assets99- **Node activation**: Check `node.active` before accessing components100- **Scene switching**: Use persist nodes for data that needs to survive scene changes101102## Key APIs Reference103104### Node Operations105- `this.node` - Current node106- `this.node.parent` - Parent node107- `this.node.children` - Child nodes array108- `this.node.addChild(child)` - Add child node109- `this.node.removeFromParent()` - Remove from parent110- `this.node.destroy()` - Destroy node111- `this.node.setPosition(x, y, z)` - Set position112- `this.node.setRotation(x, y, z)` - Set rotation113- `this.node.setScale(x, y, z)` - Set scale114115### Component Operations116- `this.node.addComponent(T)` - Add component117- `this.node.getComponent(T)` - Get component118- `this.getComponentInChildren(T)` - Get component in children119- `this.getComponentsInChildren(T)` - Get all components in children120121### Scene Operations122- `director.loadScene(name)` - Load scene123- `director.preloadScene(name)` - Preload scene124- `director.getScene()` - Get current scene125- `game.addPersistRoot(node)` - Add persist node126- `game.removePersistRoot(node)` - Remove persist node127128### Resource Operations129- `resources.load(path, type, callback)` - Load resource130- `assetManager.loadRemote(url, type, callback)` - Load remote resource131- `assetManager.releaseAsset(asset)` - Release resource132- `resources.loadDir(path, type, callback)` - Load directory133134### Event Operations135- `this.node.on(type, callback, target)` - Register event136- `this.node.off(type, callback, target)` - Unregister event137- `this.node.emit(type, detail)` - Emit event138- `input.on(type, callback, target)` - Register input event139140## External Resources141142- **Official Manual**: https://docs.cocos.com/creator/3.8/manual/zh/143- **API Reference**: https://docs.cocos.com/creator/3.8/api/zh/144- **Example Projects**: https://github.com/cocos/cocos-example-projects145146## Troubleshooting147148### Component Not Found149- Verify component is attached in editor150- Check node is active: `if (this.node.active)`151- Use `getComponentInChildren()` for nested components152153### Scene Not Loading154- Check scene name in Build Settings155- Verify scene is added to build156- Use `preloadScene()` for large scenes157158### Memory Leaks159- Release assets after use160- Destroy unused nodes161- Remove event listeners in `onDestroy`162- Use Asset Bundles for modular loading163164### Performance Issues165- Profile with Chrome DevTools166- Reduce draw calls with batching167- Use object pooling168- Optimize sprite atlases169- Check script execution time in updates170171## When to Read References172173Read specific reference files when:174175- **script-basics.md**: Creating new components, defining properties, configuring TypeScript176- **component-system.md**: Implementing lifecycle callbacks, managing component dependencies177- **node-hierarchy.md**: Creating/destroying nodes, building scene hierarchy, coordinate transformations178- **scene-management.md**: Loading scenes, passing data between scenes, managing game flow179- **resource-management.md**: Loading assets, managing memory, using Asset Bundles180- **event-system.md**: Handling user input, implementing custom events, component communication181182## Decision Guide183184**Multiple valid approaches?** Use high freedom - provide options and let context guide choice.185186**Standard pattern exists?** Use medium freedom - provide template code with parameters.187188**Error-prone operation?** Use low freedom - provide specific, validated code pattern.