?
場(chǎng)景樹(shù)
1: creator是由一個(gè)一個(gè)的游戲場(chǎng)景組成,通過(guò)代碼邏輯來(lái)控制場(chǎng)景跳轉(zhuǎn);
2: creator場(chǎng)景是一個(gè)樹(shù)形結(jié)構(gòu);
3: 父節(jié)點(diǎn), 孩子節(jié)點(diǎn);
4: cc.Node就是場(chǎng)景樹(shù)中的節(jié)點(diǎn)對(duì)象。
5: 每個(gè)節(jié)點(diǎn)只要在場(chǎng)景里面障斋,所以任何一個(gè)節(jié)點(diǎn)都是一個(gè)cc.Node;
cc.Node屬性
1: name: 獲取節(jié)點(diǎn)的名字
2: active: 設(shè)置節(jié)點(diǎn)的可見(jiàn)性;
3: position: 相對(duì)坐標(biāo),參照物是父親節(jié)點(diǎn);
4: rotation: 旋轉(zhuǎn),順時(shí)針為正, 數(shù)學(xué)逆時(shí)針為正;
5: scale: 縮放;
6: anchor: 錨點(diǎn), 左下角(0, 0), 右上角(1, 1) 可以超過(guò)這個(gè)范圍可以
7: Size: 大小
8: Color: 環(huán)境顏色;
9: Opacity: 透明度,
10: Skew: 扭曲;
11: Group: 分組; 進(jìn)行碰撞檢測(cè)
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(類(lèi), 構(gòu)造函數(shù));
2:每個(gè)cc.Component組件實(shí)例都有個(gè)成員node,指向它關(guān)聯(lián)節(jié)點(diǎn)的cc.Node;
3: name: 每一個(gè)cc.Component組件通過(guò)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è)新的類(lèi)的構(gòu)造函數(shù)旧找,它繼承自cc.Component;
3: 當(dāng)為每個(gè)節(jié)點(diǎn)添加組件的時(shí)候,會(huì)實(shí)例化(new)這個(gè)組件類(lèi)辜贵,生成一個(gè)組件實(shí)例;(js語(yǔ)法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來(lái)訪問(wèn)這個(gè)組件實(shí)例說(shuō)掛載的節(jié)點(diǎn)對(duì)象;
6: 代碼里訪問(wèn)cc.Node總要屬性;
cc.Node場(chǎng)景樹(shù)相關(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í)間消耗,對(duì)于編寫(xiě)通過(guò)用的模塊
? ? ? ? item = cc.find("Canvas/parent/item1");
? ? ? ? console.log("#######", item.name);
? ? ? ? // end?
? ? ? ? var pos = item.getPosition(); // 相對(duì)位置
? ? ? ? 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) {
? ? },
});
我也創(chuàng)建了個(gè)cocos creator的學(xué)習(xí)交流群歡迎大家一起來(lái)討論點(diǎn)擊鏈接加入群聊【cocos/unity交流群】