SoFunction
Updated on 2025-03-03

Organize common knowledge points for CocosCreator

1. Scene loading

  • ('Scene Name');//Scene Jump
  • ('Scene Name');//Preload the scene
  • ();//Get the current scene

2. Find nodes

1. Node search

  • node = ("Canvas/bg");//Path access node has relatively large performance consumption
  • ('name');//Name gets child nodes with low performance consumption
  • ()//Get the label attribute value on the node
  • ; //The current script node
  • ; // Parent node
  • (100); //Get child nodes through tags
  • ("game/test",); //Get nodes through the path under the specified node
  • ; //Get all child nodes
  • (); //Get all child nodes
  • ; //Get the number of child nodes
  • (); //Get the number of child nodes
  • (); //Get the scene master node
  • var sprites = ();//Recursively search for components of the specified type in itself and all child nodes

2. Other node operations

  • (node);//Clone the node
  • = ('Canvas');//Bind parent node
  • (nodeName,zIndex,tag);//Add child nodes to set levels and tags
  • (nodeName);//Remove child nodes
  • (nodeTag);//Remove child nodes through tags
  • ();//Destroy node
  • ;//Determine whether the node is available
  • (newNode);//Remove the child node specified in the node
  • (100);//Remove the specified child node in the node through the tag
  • ();//Remove all child nodes
  • ();//Destroy all child nodes

3. Stop playback action and timer

();//Stop all playing actions and timers

3. Node attribute settings

  • (); or getPositionY() //X axis or Y axis coordinates
  • (); or getScaleY() //X-axis or Y-axis scaling ratio
  • = 100;//Set the node x-axis coordinates
  • = 100;//Set the node y-axis coordinates
  • (x,y); //Set node coordinates
  • = 90; //Set the node rotation angle
  • = 2; //Set the node x-axis scaling multiple
  • = 2; //Set the node y-axis scaling multiple
  • (2); //Set the overall scaling multiple of the node
  • = 100; //Set the node width size
  • = 100; //Set the node height size
  • (100, 100); //Set the node width, height and size
  • = 1; //Set the coordinates of the node x-axis anchor point
  • = 0; //Set the node y-axis anchor coordinates
  • (1, 0); //Set the node anchor coordinates
  • = 255; //Set the node transparency size (0-255)
  • (20); //Set node transparency (0~255)
  • = new (100,100,100,255); //Set the node color (R,G,B, transparency)
  • () //Determine whether the node exists
  • = false; //Close node (hide node)

Resident node

  • (myNode); //Resident node (global variable)
  • (myNode); //Cancel the resident node

4. Node action

  • ()//Show now
  • ()//Hide now
  • ()//Significant switch
  • (1)//Gradually show the effect
  • (1)//Facial effect
  • (1)//Wait for 1 second
  • ((1,0,0)); //Move to the current node (time (s), X-axis coordinates, Y-axis coordinates)
  • ((1,0.7,0.8));//Scale to the current multiple node (time (s), X-axis multiple, Y-axis multiple)
  • ((1,160,160));//Rotate to the specified angle (time (s), X-axis angle, Y-axis angle)
  • ((1,5,-5));//Change node inclination (time (s), X-axis inclination, Y-axis inclination)
  • ((2,0));//Change the transparency of the current node (time (s), transparency)
  • ((2,255,255,0));//Change the current node color (time, R, G, B)
  • ();//Stop all actions
  • var action = (2, 100, 100);// Create an action (moveTo is a move)
  • (action);// Execute the specified action
  • (action);// Stop the specified action
  • (action1,action2); //Execute continuously in order
  • (action1, action2); //Execute simultaneously
  • ((action1,action2)); //The action that keeps repeating

5. Timer

start() {
        // Start timed        // Start after 2S        (() => {
            ("scheduleOnce")
        }, 2)

        // Frequency Number of times +1 Delay        (() => {
            ("schedule")
        }, 1, 3, 5)

        // Perform forever        let one = (() => {
            ("schedule")
        }, 1, .REPEAT_FOREVER, 2)


        // Clear all timings        (() => {
            ("scheduleOnce")
            ()
        }, 5)

        let callb = function () {
            ("callb")
        }
        (callb, 0.5)  //Execute forever by default
        (() => {
            ("scheduleOnce")
            (callb)
        }, 2)
    },

6. Event monitoring

(Start: ‘touchstart’, move: ‘touchmove’, end: ‘touchend’, cancel: ‘touchcancel’)

('touchstart',function(event){
	();
},this);
  • ();//Get the ID of the contact
  • ();//Get the coordinates of the touch point X
  • ();//Get the coordinate Y of the touch point
({
	event: /TOUCH_ONE_BY_ONE,myfunction},);

7. Define global variables

= "global string";//Global variables can be defined in any script

 = {
	a: null,
	b: null,
};

Global variables can be accessed in any script (provided that the script has been executed)
= 0;
= 0;

var something = require(‘something');
(myNode);//Resident node must be located at the root node at the level = {
     config: 123
}

8. Resolution

Obtain device resolution

  • var equipment= ()
  • var equipmentW=
  • var equipmentH=
  • ().width;//Get the width of the device resolution
  • ().height;//Get the height of the device resolution
  • (true);//Display frame number information

9. Audio control

(,true);//Play music (true loop)
()//Stop playback
(,false);//Play sound effects (false means only play once)
(Sound effect variable name);//Stop specifying sound effects (you need to assign sound effects to the variable first)
();//Stop all sound effects
(parameters); //Set the volume of background music (range is 0 to 1)
(parameters); //Set the volume of the sound effect (range is 0 to 1)

10. Equipment judgment

  • //Is it local?
  • //Is it a web page?
  • //Is it a mobile system?
  • //Running platform
  • //The language of the current running system
  • //The currently running system
  • .OS_IOS //Is it an IOS system?
  • .OS_ANDROID //Is it an Android system
  • .OS_WINDOWS //Is it a windows system
  • ('Http://'); //Open the web page

11. Monitoring and launching events

  • (true);//Pause node system event
  • (true);//Recover node system event
  • (this);//Remove all registration events

1. Touch monitoring

Start 'touchstart',
Mobile 'touchmove',
End 'touchend',
Cancel 'touchcancel'

  • var pos = ();//Get the coordinates of the touch point (including X and Y)
  • var x = ();//Get the X coordinate of the touch point
  • var y = ();//Get the Y coordinate of the touch point
  • var a = ();//Get the ID of the contact

2. Mouse monitoring

Press 'mousedown' with the mouse,
Move into node 'mouseenter',
Move 'mousemove' in node,
Mouseleave,
‘Release the mouse’mouseup’

  • ();//Get the Y-axis distance of the roller rolling, which is only valid when scrolling
  • ();//Get the mouse position object, the object contains x and y attributes

3. Input box monitoring

Get focus 'editing-did-began',
Text change 'text-changed',
Lost focus 'editing-did-ended',
Press Enter 'editing-return'

4. Attribute change monitoring

Position 'position-changed',
width and height ‘size-changed’,
Rotation-changed,
Scale-changed

5. ScrollView control monitoring

Scrolling,
Stop scrolling 'scroll-ended'

6. User-defined events

Listening: ("custom event name", function(target) , this);

  • ('Event Name',function,this);//Register listening
  • ('Event Name');//Send a monitor broadcast
  • ('Event Name',function,this);//Close listening

Self-delivery: emit("event name", [detail]); Only you can receive it

onLoad: function () {
        // Recipient        // Event type is your customized string;        // Return to the instance of function: function(e) {} e--->        ("pkg_event", function (e) {
            ("pkg_event", e);
        }, this);
        // The dispatcher can only pass it to himself, not to upload it to the upper part        ("pkg_event", { name: "hanbao" });
    },

Bubble delivery: dispatchEvent(new ("name", whether to bubble delivery));

onLoad: function () {
        // Recipient        // Event type is your customized string;        // Return to the instance of function: function(e) {} e--->        ("pkg_event", function (e) {
            ("pkg_event", );
        }, this);
    },
    start: function () {
        ("pkg_event", { name: "hanbao" });  //It will be distributed to myself once here        // // Here is distributed to the global and sent to this system;        // true/false, true is passed upward, false is not passed upward        var e = new ("pkg_event", true);
         = { name: "haobao" };
        (e);  
    },

Replenish:

  • ();//pause
  • ();//continue
  • ();//Exit the entire application
  • ();//Level acquisition
  • (1);//Level changes
  • (‘canvas/map' + num)//Read the path with variables

The above is a detailed content of sorting out CocosCreator's common knowledge points. For more information about CocosCreator's knowledge points, please pay attention to my other related articles!