Author: Not Washing Dishes Studio – Xin Zai

The copyright belongs to the author, please indicate the source

Node operation

When we create a scene, we create a Canvas node by default, and then we can create child nodes under the Canvas or sibling nodes on the Canvas.

On a node, we can mount various components, and we can also customize our own script components. When we mount script components on a node, we can operate node properties in the script as follows:

This // represents the current component this.node // represents the node on which this component is mounted this.node.parent // The parent node on which this component is mounted this.node.children // The array of children on which this component is mounted this.node.getChildByName('Child node name'// Get the specified child nodeCopy the code

A good way to get a Node is to define cc.node in properties and drag the Node to that property

Find nodes

cc.find

// find the Sprite component cc.find('node-1/node-2<cc.Sprite>')
Copy the code

action

The API for the action is provided by the node, so we call this.node, which is positioned relative to its parent node

Time interval action

Cc. MoveTo (Duration, Position) cc.moveto (1, cc.v2(1, 1)) or cc.moveto (1, 1, 1) /* Rotate */ cc.rotateto (1, 180) // 1s rotate 180 degrees /* zoom */ cc.scareto (1, 2, 2) // 1 /* * * */ cc. FadeOut (1) // 1s becomes transparentCopy the code

Immediate action

Cc.removeself () // Remove yourself from the parent node (does not mean you are removed)true) // Remove yourself cc.sequence() // Animates the sequence to execute cc.callfunc (callback, this points to, the argument passed in) (self, args) => {} // The callback to ease () // The easing function, passing in the easing typeCopy the code

The event

// Custom event this.node.on('eventName'.'func', context)
this.node.emit('eventName', args) this.node.once this.node.off (e) => e.fix // e.fix is the parameter passed inCopy the code

System events

Touch events

Events have scope for a node, and events defined at that node are valid only when clicked within that node

this.node.on('touchstart', (e) => {}, this)
this.node.on('touchmove', (e) => {}, this)
this.node.on('touchend', (e) => {}, this)
this.node.on('touchcancel', (e) = > {}, this) / / move to the outside of the node transformation * / / * this. Check node.'touchstart', (e) => {LLDB etLocation() // get the coordinates of the global coordinate system (with the lower left corner as the origin) // go from the global coordinate system to the local coordinate system // go to the relative coordinate system in this.nodeletLocationOfThisNode = this. Node. ConvertToNodeSpaceAR (um participant etLocation ()) / / in the coordinate system under the parent nodeletLocationOfThisNodeParent = this. Node. The parent. ConvertToNodeSpaceAR (um participant etLocation ()) / / thus moving nodes to click on the location of the enclosing node. The position =  locationOfThisNodeParent })Copy the code

Mouse events

this.node.on('mouseup', () => {}, context)
this.node.on('mousemove', () => {}, context)
this.node.on('mousedown', () => {}, context)
this.node.on('mouseenter', () => {}, context)
this.node.on('mouseleave', () => {}, context)
this.node.on('mousewheel', () => {}, context)
Copy the code

Keyboard events

cc.systemEvent.on('keydown', () => {}, context)
cc.systemEvent.on('keyup', () => {}, context)
cc.systemEvent.on('keydown', (e) => {
    if(e.keyCode === cc.KEY.w) {
        console.log('Press w')}})Copy the code

The precast body

Let’s dynamically generate a node

let tmpNode = new cc.Node()
tmpNode.name = 'New node'This.node.addchild (tmpNode) /* The component */ followslet sprite = this.node.addComponent('cc.Sprite')
this.node.removeComponent('cc.Sprite')
this.node.getComponent('cc.Sprite')
Copy the code

The nodes are generated using preforms

let node = cc.instantitate('preform')
this.node.addChild(node)
Copy the code

A prefab can only store the contents of this component, not references to other components

The global variable

A = 1 // module.export and require module.export = {a, b, c}let {a, b, c} = require('Module name') / / static variables defined in the instance of Shared static: {a: 1} / / addPersistRootNode cc. Game. AddPersistRootNode ('node1') / / the node resident in memory, and will not be destroyed by scene switching cc. Game. RemovePersistRootNode ('node1')
cc.director.getScene().getChildByName('node1'// It is equivalent tolocalStorage
cc.sys.localStorage.getItem()
cc.sys.localStorage.setItem()
Copy the code

The timer

this.schedule(func, 1)
this.unschedule(func)
this.scheduleOnce(func, 1)
Copy the code

animation

Animation can modify this node and its children

// Get the animation instancelet runState = this.anim.getAnimationState('run') // Register the event runstate.on ('lastframe', () = > {}, the context) / / frame dynamically add event runState clip. Events. Push ()Copy the code

The collision

Colliding components under the same node do not collide