Cocos Creator access to Nodes and components (excerpt from official documentation)

Source: Internet
Author: User
Tags documentation require

Transferred from: http://blog.csdn.net/alpha_xiao/article/details/52102044

You can modify the nodes and components in the property inspector, as well as dynamically modify them in the script. The benefit of dynamic modification is the ability to continuously modify attributes and transition properties over time to achieve a gradient effect. The script also responds to player input and can modify, create, and destroy nodes or components to implement a variety of game logic. To achieve these effects, you need to first get the node or component you want to modify in the script.

In this tutorial, we will show you how to get the node where the component is located get other components using the Property Inspector Settings node and component lookup child node global node lookup accessing values in existing variables
get the node where the component resides

It is simple to get the node where the component is located, as long as the This.node variable is accessed in the component method:

    Start:function () {
        var node = this.node;
        node.x = +;
    }
Get Other Components

You will often need to get other components on the same node, which will use the Getcomponent API, which can help you find the components you want.

    Start:function () {
        var label = This.getcomponent (cc). Label);
        var text = THIS.name + ' started ';

        Change the text in Label Component
        label.string = text;
    }

You can also pass in a class name for Getcomponent. For user-defined components, the class name is the file name of the script and is case-sensitive. For example, the component declared in "Sinrotate.js", the class name is "Sinrotate".

    var label = This.getcomponent ("CC. Label ");

There is also a Getcomponent method on the node, which works the same way:

    Start:function () {
        Cc.log (This.node.getComponent (cc). Label) = = = This.getcomponent (cc. Label));  True
    }

If you do not find the component you want on the node, getcomponent will return null, and if you try to access the null value, it will throw a "TypeError" error at run time. So if you're not sure if the component exists, remember to make a decision:

    Start:function () {
        var label = This.getcomponent (cc). Label);
        if (label) {
            label.string = "Hello";
        }
        else {
            Cc.error ("Something wrong?");
        }
    }
get other nodes and their components

It is often not enough to access only the node's own components, and the script often needs to interact with multiple nodes. For example, a cannon that automatically targets the player needs to keep getting the player's latest position. Cocos Creator provides a number of different ways to get other nodes or components. setting up nodes with the property Inspector

The most straightforward way is to set the object you want in the property inspector. In the case of a node, this only needs to declare a type of CC in the script. Node's Properties:

Cannon.js

cc. Class {
    extends:cc.Component,
    properties: {
        //declares the player property of
        player: {
            default:null,
            type:cc. Node
        }
    }
);

This code declares a player property inside the properties, the default value is NULL, and specifies that its object type is CC. Node. This is equivalent to declaring public cc in other languages. Node player = null;. After the script is compiled, this component looks like this in the property Inspector:

You can then drag any node on the Hierarchy Manager to the Player control:

The Player property will be set successfully so that you can access the player directly in the script:

Cannon.js

var player = require ("Player");

Cc. Class {
    extends:cc.Component,
    properties: {
        //declares the player property of
        player: {
            default:null,
            type:cc. Node
        }
    },

    start:function () {
        var playercomp = this.player.getComponent (player);
        This.checkplayer (Playercomp);
    },

    //...
});
setting up components with the property Inspector

In the example above, if you declare the type of the property as the player component, when you drag the node "Player node" to the property inspector, the Player property is set to the player component within that node. So you don't have to call getcomponent yourself.

Cannon.js

var player = require ("Player");

Cc. Class ({
    extends:cc. Component,
    properties: {
        //declares the Player property, this time directly the component type
        player: {
            default:null,
            type:player
        }
    },

    start:function () {
        var playercomp = This.player;
        This.checkplayer (Playercomp);
    },

    //...
});

You can also change the default value of a property from null to an array [], so you can set multiple objects at the same time in the property inspector.
However, if you need to get other objects dynamically at run time, you need to use the lookup method described below. Find child nodes

Sometimes there are many objects of the same type in the game scene, such as turrets, enemies, and special effects, and they usually have a global script that is managed uniformly. If you use the property inspector to associate them with a script, the work is tedious. To better manage these objects together, we can put them under a uniform parent object and then get all the sub-objects through the parent object:

Cannonmanager.js

cc. Class ({
    extends:cc.Component,

    start:function () {
        this.cannons = [];
        This.cannons = This.node.getChildren ();
    }
});

The GetChildren here is CC. Node's original API, you can get an array containing all the child nodes.

You can also use Getchildbyname:

This.node.getChildByName ("Cannon 01");

If the child nodes are deep, you can also use Cc.find,cc.find to search through the incoming path:

Cc.find ("Cannon 01/barrel/sfx", This.node);
Global Name Lookup

When Cc.find only passes in the first parameter, it is searched from the scene root node:

This.backnode = Cc.find ("Canvas/menu/back");
accessing values in an existing variable

If you have saved references to nodes or components in one place, you can also access them directly, usually in two ways:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.