微信小游戏-CocosCreator 基础(六)

var my_item = require("my_item");

// 返回了一个构造函数,然后继承了cc.Component
// 代码组件也有cc.Component组件的方法;
// cc.Component, 固定的入口函数
cc.Class({
    extends: cc.Component,

    // 属性列表
    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 
         //其他类型设置:
        //打开engine=》cclabel.js

    },
    // 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 
        //查询孩子节点组件实例
        //getComponentInChildren(组件类型)
        //getComponentsInChildren(组件类型)

        // 删除组件
        // 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);
    },

// 不是特别常用,update之后   画面更新后
    lateUpdate: function(dt) {
        // console.log("lateUpdate");
    },

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

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

    // 组件实例销毁的时候调用
    onDestroy: function() {
        console.log("onDestroy");
    },
    //顺序:
    OnLoad=》OnEnable=》OnStart=》Update
});


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值