zoukankan      html  css  js  c++  java
  • cocos creator基础-(五)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 取消所有的定时器操作;
    注意,如果节点或组件没有激活是不会调用的;

    var my_item = require("my_item");
    
    // 返回了一个构造函数,然后继承了cc.Component
    // 代码组件也有cc.Component组件的方法;
    // cc.Component, 固定的入口函数
    cc.Class({
        extends: cc.Component,
    
        // 属性列表
        properties: {
            // foo: {
            //    default: null,      // The default value will be used only when the component attaching
            //                           to a node for the first time
            //    url: cc.Texture2D,  // optional, default is typeof default
            //    serializable: true, // optional, default is true
            //    visible: true,      // optional, default is true
            //    displayName: 'Foo', // optional
            //    readonly: false,    // optional, default is false
            // },
            // ...
            // 基本数据类型, 数,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);
            // Canvas<game_scene> Canvas
            console.log(this.name, this.node.name); // 组件实例所挂载的节点的名称<组件名称>, 节点.name 直接为名称;
        },
    
        // 组件在第一次update调用之前调用
        start: function() {
            console.log("start");
            // 添加组件,系统组件cc.Sprite, cc.Label等, "组件代码的名字"
            // 返回,返回挂上的组件实例
            var com_inst = this.addComponent("my_item");
            com_inst = this.node.addComponent("my_item");
            // end 
    
            // 查找组件实例
            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);
            // end 
    
            // 删除组件
            // this.destroy(); // 删除当前的组件实例,触发onDisable, onDestroy的调用
            // end 
    
            // 启动定时器, 节点或组件必须是激活状态,  例如被隐藏的节点,都是无法启动定时器的;
            // 这里只会触发一次调用
            this.scheduleOnce(function() {
                console.log("scheduleOnce called");
            }.bind(this), 5);
            // end 
    
            // schedule(函数, 多长时间掉一次, 次数(永远), 隔多少秒以后开始执行shedule)
            // 5秒钟以后,每隔1秒,我们调用6 + 1次函数;
            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); // 次数 6 + 1 = 7;  cc.macro.REPEAT_FOREVER 永远
            // end 
    
    
            // 取消所有的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");
        },
    
        // 组件被激活的时候调用
        onEnable: function() {
            console.log("onEnable");
        },
    
        // 组件被禁用的时候调用
        onDisable: function() {
            console.log("onDisable");
        }, 
    
        // 组件实例销毁的时候调用
        onDestroy: function() {
            console.log("onDestroy");
        },
    });
  • 相关阅读:
    杭电1029--Ignatius and the Princess IV(哈希)
    杭电1465--不容易系列之一
    杭电1021--Fibonacci Again
    杭电5018--Revenge of Fibonacci
    UVa10651(记忆化搜索)
    <Win32_5>深入浅出Win32的计时器
    偷个空,写个博客——各种沟通各种纠结
    Arbitrage HDU
    常用的字符串处理方法
    无法捕获的异常:MissingMethodException
  • 原文地址:https://www.cnblogs.com/orxx/p/10419387.html
Copyright © 2011-2022 走看看