場景樹
1: creator是由一個(gè)一個(gè)的游戲場景組成,通過代碼邏輯來控制場景跳轉(zhuǎn);
2: creator場景是一個(gè)樹形結(jié)構(gòu);
3: 父節(jié)點(diǎn), 孩子節(jié)點(diǎn);
4: cc.Node就是場景樹中的節(jié)點(diǎn)對象。
5: 每個(gè)節(jié)點(diǎn)只要在場景里面,所以任何一個(gè)節(jié)點(diǎn)都是一個(gè)cc.Node;
cc.Node屬性
1: name: 獲取節(jié)點(diǎn)的名字
2: active: 設(shè)置節(jié)點(diǎn)的可見性;
3: position: 相對坐標(biāo),參照物是父親節(jié)點(diǎn);
4: rotation: 旋轉(zhuǎn),順時(shí)針為正, 數(shù)學(xué)逆時(shí)針為正;
5: scale: 縮放;
6: anchor: 錨點(diǎn), 左下角(0, 0), 右上角(1, 1) 可以超過這個(gè)范圍可以
7: Size: 大小
8: Color: 環(huán)境顏色;
9: Opacity: 透明度,
10: Skew: 扭曲;
11: Group: 分組; 進(jìn)行碰撞檢測
12: parent: 父親節(jié)點(diǎn)的cc.Node;
13: children/childrenCount: 孩子節(jié)點(diǎn)的數(shù)組;
14: tag : 節(jié)點(diǎn)標(biāo)簽;
cc.Component
1:所有的組件都擴(kuò)展自cc.Component(類, 構(gòu)造函數(shù));
2:每個(gè)cc.Component組件實(shí)例都有個(gè)成員node,指向它關(guān)聯(lián)節(jié)點(diǎn)的cc.Node;
3: name: 每一個(gè)cc.Component組件通過name屬性可以獲得節(jié)點(diǎn)的名字;
4: 組件實(shí)例入口函數(shù):
onLoad: 在組件加載的時(shí)候調(diào)用;
start: 組件第一次激活前, 調(diào)用在第一次update之前;
update(dt): 每次游戲刷新的時(shí)候調(diào)用,
lateUpdate(dt): 在update之后調(diào)用;
enabled:組件是否被啟動(dòng);
onEnable: 組件被允許的時(shí)候調(diào)用;
onDisable: 組件不被允許的時(shí)候調(diào)用;
代碼組件
1:每個(gè)代碼組件實(shí)例都繼承自cc.Component(構(gòu)造函數(shù)),所以有一個(gè)node數(shù)據(jù)成員指向cc.Node;
2: cc.Class({...}) 定義導(dǎo)出了一個(gè)新的類的構(gòu)造函數(shù),它繼承自cc.Component;
3: 當(dāng)為每個(gè)節(jié)點(diǎn)添加組件的時(shí)候,會(huì)實(shí)例化(new)這個(gè)組件類,生成一個(gè)組件實(shí)例;(js語法new)
4: 當(dāng)組件加載運(yùn)行的時(shí)候,代碼函數(shù)里面的this指向這個(gè)組件的實(shí)例;
5: 代碼組件在掛載的時(shí)候擴(kuò)展自cc.Component, 里面有個(gè)成員node會(huì)指向節(jié)點(diǎn)(cc.Node);
所以在代碼組件里面,可以使用this.node來訪問這個(gè)組件實(shí)例說掛載的節(jié)點(diǎn)對象;
6: 代碼里訪問cc.Node總要屬性;
cc.Node場景樹相關(guān)方法
1: 代碼中創(chuàng)建一個(gè)節(jié)點(diǎn)new cc.Node();
1: addChild; 加一個(gè)子節(jié)點(diǎn)
2: removeFromParent/ removeAllChildren; 從父節(jié)點(diǎn)刪除單個(gè) / 刪除所有孩子
3: setLocalZOrder/ 繪制順序, 在下面(值越大)的會(huì)繪制在屏幕的上面;
4: 遍歷節(jié)點(diǎn)的子節(jié)點(diǎn);
5: setPosition/getPosition,
6: getChildByName/getChildByTag, getChildByIndex, 局部查找
7: cc.find(): 方便,不通用, 消耗 全局查找
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// default: null, // The default value will be used only when the component attaching
// to a node for the first time
// url: cc.Texture2D, // optional, default is typeof default
// serializable: true, // optional, default is true
// visible: true, // optional, default is true
// displayName: 'Foo', // optional
// readonly: false, // optional, default is false
// },
// ...
},
// use this for initialization
// 組件實(shí)例在加載的時(shí)候運(yùn)行
// 組件實(shí)例.onLoad(), 組件實(shí)例.start;
onLoad: function () {
// this, --> 當(dāng)前組件實(shí)例
console.log(this);
console.log("this.onLoad");
// 代碼里面怎么找到節(jié)點(diǎn)?
// 指向這個(gè)組件實(shí)例所掛載的節(jié)點(diǎn)
console.log("=======================");
console.log(this.node);
console.log(this.node.name);
console.log(this.node.active);
console.log(this.node.x, this.node.y, this.node.position);
console.log(this.node.group, this.node.groupIndex);
if (this.node.parent) {
console.log(this.node.parent.name);
console.log("go if @@@@@");
}
else {
// console.log(this.node.parent);
console.log("go else @@@@@");
}
console.log("========================");
// end
// 孩子
var children = this.node.children; // [cc.Node, cc.Node, cc.Node]
for(var i = 0; i < children.length; i ++) {
console.log(children[i].name);
}
// end
console.log("yes we have:", this.node.childrenCount,"chilren");
// 添加
/*var new_node = new cc.Node();
this.node.addChild(new_node);
new_node.removeFromParent();
this.node.removeAllChildren();*/
// end
// 查找,局部查找
var item = this.node.getChildByName("item1");
console.log("^^^^^^^", item.name);
// end
// 全局, 時(shí)間消耗,對于編寫通過用的模塊
item = cc.find("Canvas/parent/item1");
console.log("#######", item.name);
// end
var pos = item.getPosition(); // 相對位置
console.log("pos = ", pos);
pos = cc.p(100, 100); // cc.Vec,
item.setPosition(pos);
var item2 = this.node.getChildByName("item2");
item2.setLocalZOrder(100);
item2.active = false; //
},
// 組件實(shí)例
start: function() {
},
// called every frame, uncomment this function to activate update callback
// 組件實(shí)例.update
update: function (dt) {
},
});