cc.Component

组件入口函数
1: onLoad: 组件加载的时候调用, 保证了你可以获取到场景中的其他节点,以及节点关联的资源数据;
2: start: 也就是第一次执行 update 之前触发;
3: update(dt):组件每次刷新的时候调用,距离上一次刷新的时间(会在所有画面更新前执行);
4: lateUpdate(dt) 刷新完后调用(会在所有画面更新后执行);
5: onEnable: 启用这个组件的时候调用;
6: onDisable: 禁用这个组件的时候调用;
7: onDestroy: 组件实例销毁的时候调用;

cc.Component属性
1: 组件类: 所有组件的基类;
2: node: 指向这个组件实例所挂载的这个节点(cc.Node);
3: name: 这个组件实例所挂载的节点的名字<组件的名字>;
4: properties: {} 属性列表;
(1) name: value, 数,bool, 字符串;
(2) 位置,颜色, 大小: cc.p(0, 0), cc.color(0, 0), cc.size(100, 100)
(3) 组件: {
type: 组件类型, 系统类型,也可以require自己编写的组件类型
default: null or []
}
(4)其他: 打开cocos creator源码,找到参考,然后移动到你的代码里面;

组件添加查找删除
1: addComponent(组件的类型): 向节点上添加一个组件实例, 返回添加好的组件实例;
2: getComponent(组件类型): 查找一个为指定类型的组件实例(如果有多个,第一个匹配);
3: getComponents(组件类型): 查找这个节点上所有这个类型的组件实例;
[inst1, inst2, inst3, ...]
4: getComponentInChildren(组件类型): 在自己与孩子节点里面查找;
5: getComponentsInChildren (组件类型): 在自己与孩子节点里面查找;
6: destroy(): 从节点中删除这个组件的实例;

Shedule定时器操作
1: sheduleOnce(函数, time): time秒后启动一次定时器;
2: schedule(函数, time, 次数, 多长时间后开始); 执行的次数为(次数 + 1), cc.macro.REPEAT_FOREVER;
3: unschedule(函数); // 取消这个定时器操作;
5: unscheduleAllCallbacks 取消所有的定时器操作;
注意,如果节点或组件没有激活是不会调用的; 

    properties: {

        // 基本数据类型, 数,bool, 字符串, color, pos, size
        speed: 100,
        is_debug: false,
        url_str: "",
        color: cc.color(0, 0, 0, 255),
        pos: cc.p(0, 0),
        size: cc.size(100, 100),
        // end 

        // 系统的组件, cc.Sprite, cc.Button, cc.Label, ..
        sprite_item: {
            type: cc.Sprite,
            default: null, // null/[]
        },

        sprite_array: {
            type: cc.Sprite,
            default: [],
        },
        // end 

        // 组件的代码组件
        custom_comp: {
            type: my_item,
            default: null, // null /[]
        },
        // end 
    },
    // end 

    // use this for initialization
    // 组件在加载的时候运行
    // 你可以在onLoad里面访问场景的节点和数据,这个时候场景的节点和数据都已经准备好了
    // 不会发生在调用onLoad的时候,还会出现场景节点没有出来的情况
    onLoad: function() {
        console.log("onLoad");
        // this:指的是当前的组件实例
        // this.node --> cc.Node, 这个组件所挂的节点对象
        // 组件实例找对应的节点   组件.node来获取;
        console.log('this.node:', this.node);
        // Canvas<game_scene> Canvas
        console.log('name:%s,node.name:', this.name, this.node.name); // 组件实例所挂载的节点的名称<组件名称>, 节点.name 直接为名称;
    },

    // 组件在第一次update调用之前调用
    start: function() {
        console.log("start");
        // 添加组件,系统组件cc.Sprite, cc.Label等, "组件代码的名字"
        // 返回,返回挂上的组件实例
        var com_inst = this.addComponent("my_item");
        console.log('自定义组件:', com_inst.name);
        com_inst = this.node.addComponent("my_item");
    
        // 查找组件实例
        com_inst = this.node.getComponent("my_item");
        com_inst = this.getComponent("my_item"); // 返回的是第一个找到的组件
        var com_array = this.getComponents("my_item"); // 返回的是组件数组[实例1,实例2, 实例3]
        console.log(com_inst, com_array);
 
        // 删除组件
         this.destroy(); // 删除当前的组件实例,触发onDisable, onDestroy的调用
        // end 

        // 启动定时器, 节点或组件必须是激活状态,  例如被隐藏的节点,都是无法启动定时器的;
        // 这里只会触发一次调用
        this.scheduleOnce(function() {
                console.log("scheduleOnce called");
            }.bind(this),
            5);

        // schedule(函数, 多长时间掉一次, 次数(永远), 隔多少秒以后开始执行shedule)
        // 5秒钟以后,每隔1秒,我们调用6 + 1次函数(重复6次);
        this.schedule(function() {
                console.log("schedule called");
            }.bind(this),1,6,5); // 次数 6 + 1 = 7;
        // end 

        this.schedule(function() {
                console.log("schedule forerver called");
            }.bind(this),
            1,
            cc.macro.REPEAT_FOREVER,
            5); //5秒之后执行;每秒1次;  cc.macro.REPEAT_FOREVER 永远 


        // 取消所有的shedule
        this.scheduleOnce(function() {
                console.log("cancel all schedules");
                this.unscheduleAllCallbacks();
            }.bind(this),
            30);


        // 只取消一个, unschedule(函数对象)
        var callback = function() {
            console.log("======================");
        }.bind(this);
        this.schedule(callback, 0.5); // 默认值为永远执行,马上开始

        this.scheduleOnce(function() {
                // 取消了一个定时器
                this.unschedule(callback);
            }.bind(this),5);
    },

    // called every frame, uncomment this function to activate update callback
    // 每次游戏刷新的时候调用, dt距离闪一次刷新的时间
    update: function(dt) {
        console.log("update called", dt);
    },

    // 不是特别常用
    lateUpdate: function(dt) {
        console.log("lateUpdate",dt);
    },

    // 组件被激活的时候调用
    onEnable: function() {
        console.log("onEnable");
    },

    // 组件被禁用的时候调用
    onDisable: function() {
        console.log("onDisable");
    },

    // 组件实例销毁的时候调用
    onDestroy: function() {
        console.log("onDestroy");
    },

 

posted @ 2018-09-30 15:49  何博  阅读(1994)  评论(0编辑  收藏  举报