cocos creator学习笔记5——Component

cc.Component

组件的基类,是用于控制整个组件运行的基类

组件入口函数 说明
onLoad() 组件加载时运行
start() 在第一次update()运行前调用
update(dt) 场景刷新时调用
lateUpdate(dt) 场景刷新完后调用
onEnable() 启用组件的时候调用
onDisable() 停用组件的时候调用
onDestory() 组件实例被销毁时调用

cc.Component属性

常用属性 说明
this.node 当前组件挂载的节点对象
this.name 挂载该组件的节点的名称<组件名称>
this.node.name 挂载了该组件的节点名称


properties属性列表基本类型 说明
num:100 数字类型属性
bool:true 布尔类型属性
str:”“ 字符串类型属性
color:cc.color(0,0,0,255) 颜色类型属性
pos:cc.p(0,0) 位置类型属性
size:cc.size(0,0) 大小类型属性
cc.Sprite 精灵组件实例(如果default:[]则为数组)
cc.Label 文本实例
cc.SpriteFrame 精灵帧类型
cc.Node 节点实例
cc.Prefab 预制文件实例
require() 文件

组件操作

API 说明
addComponent() 向节点上添加一个组件实例
getComponent() 查找一个为指定类型的组件实例(如果有多个,第一个匹配);
getComponents() 查找这个节点上所有这个类型的组件实例,返回一个数组
getComponentInChildren() 在自己与孩子节点里面查找
getComponentsInChildren() 在自己与孩子节点里面查找
destroy() 从节点中删除组件实例
var test = this.addComponent("test");
test = this.node.addComponent("test"); //两个代码实现相同的功能
this.destroy();//将会触发onDisable()和onDestroy()

定时器

API 说明
scheduleOnce(callback,delay) 根据delay参数(秒为单位),在delay秒之后调用callback一次
schedule(callback,interval,repeat ,delay) 在delay秒后调用callback重复repeat+1次,并且每隔interval秒调用一次。如果repeat为cc.macro.REPEAT_FOREVER则无限调用callback函数
unschedule(callback_fn) 取消调度一个自定义的回调函数
unscheduleAllCallbacks() 取消所有的定时器操作

前提:节点必须是激活、可见状态才会调用

猜你喜欢

转载自blog.csdn.net/agsgh/article/details/79557320