1.creator是由一个一个的游戏场景组成,通过代码逻辑来控制场景跳转;creator场景是一个树形结构;有父节点与孩子节点;cc.Node就是场景树中的节点对象了;任何一个节点都有一个cc.Node,换言之cc.Node挂载组件
2.cc.Node的属性
3.所有的组件都扩展自cc.Component; 每个cc.Component组件实例都有个成员node,指向它关联节点的cc.Node; name: 每一个cc.Component组件通过name属性可以获得节点的名字;
组件实例入口函数:
onLoad: 在组件加载的时候调用;
start: 组件第一次激活前, 调用在第一次update之前;
update(dt): 每次游戏刷新的时候调用,
lateUpdate(dt): 在update之后调用;
enabled:组件是否被启动;
onEnable: 组件被允许的时候调用;
onDisable: 组件不被允许的时候调用;
4.代码组件
每个代码组件实例都继承自cc.Component(构造函数),所以有一个node数据成员指向cc.Node; cc.Class({...}) 定义导出了一个新的类的构造函数,它继承自cc.Component; 当为每个节点添加组件的时候,会实例化(new)这个组件类,生成一个组件实例;(js语法new); 当组件加载运行的时候,代码函数里面的this指向这个组件的实例; 代码组件在挂载的时候扩展自cc.Component, 里面有个成员node会指向节点(cc.Node); 所以在代码组件里面,可以使用this.node来访问这个组件实例说挂载的节点对象; 代码里访问cc.Node总要属性;
例:
game_script_script.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 () {
//this, -->当前组件实例
console.log(this);
//找到指向这个组件实例所挂载的节点
console.log("==================");
console.log(this.node);
console.log(this.node.name);
//找到当前节点的父节点
console.log(this.node.parent);
console.log(this.node.parent.name);
//找到孩子节点
var childs = this.node.children;
for(var i = 0; i < childs.length; i++) {
console.log(childs[i].name);
}
console.log(this.node.childrenCount);
//方法
var new_node = new cc.Node();
//添加节点
this.node.addChild(new_node);
//查找节点 局部搜索
var item = this.node.getChildByName("item1");
console.log(item.name);
//全局搜索不建议使用
//位置
var pos = item.getPosition(); //相对位置
console.log(pos);
pos = cc.p(100, 100);
item.setPosition(pos);
// setLocalZOreder 绘制顺序
},
start () {
},
// update (dt) {},
});