6.cc.Component组件类的基本使用

1. 组件入口函数

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

 

2. cc.Component属性

组件类: 所有组件的基类;
node: 指向这个组件实例所挂载的这个节点(cc.Node);
name: 这个组件实例所挂载的节点的名字<组件的名字>;
properties:

 

3. 组件添加查找删除

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

 

4. Shedule定时器操作

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

 

例:game_scence.js

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

var my_item = require("my_item");

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

    //属性列表
    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

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

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

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

        //自定义代码组件
        custom_comp: {
            type: my_item,
            default: null,
        },

    },

    // LIFE-CYCLE CALLBACKS:

    //组件在加载的时候运行
    //可以onload中访问场景的节点和数据,这时候已经准备好了
    onLoad () {
        console.log("--------- onload");

        //this 为当前组件的实例
        //this.node 为当前组件所挂在的节点对象
        console.log(this.node);

        console.log(this.name, this.node.name);

    },

    //组件在第一次update之前调用
    start () {
        console.log("--------- start");

        //添加组件 "组件代码的名字"
        //返回挂上组件的实例
        var com_inst = this.addComponent("my_item");
        this.node.addComponent("my_item");
        //end

        //查找组件实例
        com_inst = this.getComponent("my_item");  //返回一个
        var com_array = this.getComponents("my_item");  //返回组件列表  数组[]
        console.log(com_inst, com_array);
        //end

        //删除一个组件
        //this.destory();

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

        //schedule(函数, 多长时间调用一次, 次数(永远), 隔多少秒之后开始执行schedule)
        //5秒钟以后,每隔一秒, 调用6+1次
        this.schedule(function() {
            console.log("schedule----------");
        }, 1, 6, 5);

        this.schedule(function() {
            console.log("schedule+++++++++++");
        }, 1, cc.macro.REPEAT_FOREVER, 5);

        this.scheduleOnce(function() {  //只会触发一次
            console.log("cancel all sch!!!!!!!!");
            //取消所有定时器
            this.unscheduleAllCallbacks();
        }, 30);

        //只取消一个定时器 unschedule(函数对象)
        var callBack = function() {
            console.log("-------------------------------");
        };
        this.schedule(callBack, 0.5)
        this.scheduleOnce(function() {
            this.unschedule(callBack);
        }, 5);
        //end

    },

    //游戏每次刷新的时候调用,dt距离上一次刷新的时间
    update (dt) {
        //console.log("--------- update", dt);
    },

    //不常用
    lateUpdate: function(dt) {

    },

    //组件被激活时调用
    onEnable: function() {

    },

    //组件被禁止时调用
    onDisable: function() {

    },

    //组件被销毁时调用
    onDestroy: function() {

    },

});

my_item.js

// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    // update (dt) {},
});

工程截图

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 cc.Line 组件绘制随机闪电可以通过以下步骤实现: 1. 在场景编辑器中创建一个节点,添加 cc.Line 组件。 2. 编写脚本,获取 cc.Line 组件,并在 onEnable 生命周期回调函数中调用 draw 方法绘制随机闪电。具体实现方法如下: ``` cc.Class({ extends: cc.Component, properties: { line: cc.Line, duration: 0.2, // 闪电持续时间 offset: 10, // 闪电偏移量 boltWidth: 5, // 闪电宽度 color: cc.Color.WHITE // 闪电颜色 }, onEnable: function () { this.schedule(this.drawBolt, this.duration); }, onDisable: function () { this.unschedule(this.drawBolt); }, drawBolt: function () { var startPos = cc.v2(0, 0); // 起点坐标 var endPos = cc.v2(cc.winSize.width, cc.winSize.height); // 终点坐标 var midPos = cc.v2(startPos.x + (endPos.x - startPos.x) / 2, startPos.y + (endPos.y - startPos.y) / 2); // 中点坐标 // 生成随机偏移量 var offset1 = cc.v2(Math.random() * this.offset - this.offset / 2, Math.random() * this.offset - this.offset / 2); var offset2 = cc.v2(Math.random() * this.offset - this.offset / 2, Math.random() * this.offset - this.offset / 2); // 绘制闪电 this.line.setColor(this.color); this.line.setWidth(this.boltWidth); this.line.draw(startPos, midPos.add(offset1)); this.line.draw(midPos.add(offset1), endPos.add(offset2)); } }); ``` 这段代码将在节点上绘制随机闪电,每隔一定时间刷新一次。可以根据需要调整闪电的持续时间、偏移量、宽度和颜色等参数。需要注意的是,随机闪电的绘制效果可能会受到屏幕分辨率的影响,可以根据需要调整起点和终点坐标来适配不同的屏幕大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值