一个简单的组件脚本。
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function () {
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
},
});
通过在组件脚本中声明属性,我们可以将脚本组件中的字段可视化地展示在 属性检查器 中,从而方便地在场景中调整属性值。
要声明属性,仅需要在 cc.Class 定义的 properties
字段中,填写属性名字和属性参数即可,在属性检查器中方便查看修改。
编写这些脚本的用处:这些代码就是编写一个组件(脚本)所需的结构。具有这样结构的脚本就是 Cocos Creator 中的 组件(Component),他们能够挂载到场景中的节点上,提供控制节点的各种功能。例如:例子中的Player控制小怪物弹跳,Game产生星星。
完整声明:
properties: {
score: {
default: 0,
displayName: "Score (player)",
tooltip: "The score of player",
}
}
以上代码为 score
属性设置了三个参数 <