Cocos Creator常見問題彙總

常見問題彙總:java

1.本地存儲數據node

cc.sys.localStorage.getItem(key,value);android

cc.sys.localStorage.setItem(key,value); canvas

2.讀取文本文件編輯器

cc.loader.loadTxt(cc.url.raw('scripts/test.proto'), function(err, txt) {函數

            // 讀取成功,err爲null,讀取失敗纔會有錯誤信息:err:{‘status':0,’errorMessage’:’….'}動畫

            // txt爲文本內容ui

});this

3.打印LOG信息:url

須要先初始化Debug配置

cc._initDebugSetting(cc.DebugMode.INFO);

cc.log('print log info ...');

4.場景切換動畫:

【A場景切換到B場景】

A場景:

this.node.runAction(cc.fadeOut(1.0));

cc.director.loadScene('B');

--------------------------------------------------

B場景

Canvas下或background節點下

onload:function(){

this.node.runAction(cc.fadeIn(1.0));

.......

}

或者將runAction那行換成this.getComponent(cc.Animation).play('customAnim');

5.動態更換精靈的紋理:

var imgUrl = cc.url.raw('imgs/xx.png');

var texture = cc.textureCache.addImage(imgUrl);

this.getComponent(cc.Sprite).spriteFrame.setTexture(texture);

6.cc.Lable組件隱藏方法:

label.enabled = false   // 隱藏單個組件

寫在 onLoad: function () 裏是沒有效果,能夠在編輯器中把勾選去掉

或者

label.node.active = false    // 整個 node 的全部組件一塊兒禁用 

7.require腳本文件,腳本的load函數要放在外頭調用,不要在js加載的時候就去調用,由於這個時候引擎的一些參數還未初始化呢。

8.節點組件的獲取和查找用法:

a.獲取canvas節點下面的子節點score-bg

 -> 子節點沒有層級: this.node.getChildByName('score-bg');

 -> 子節點在多個層級下面: cc.find("background/score-bg", this.node);

b.獲取當前節點scoreNode下的Label組件:

scoreNode.getComponent(cc.Label);

9.Node上的Action動做系統與Widget兼容問題:

node.runAction(..);

注意事項:若是node節點上面,包含Widget組件,且設定了top,bottom,left,right中的數值。

例:right=50px,效果就是node節點始終距離屏幕右側邊緣50px;

這時你給node加上cc.moveBy(2,-100,-100)動做,此時node只會在y方向運動,x方向不會運動。

此時須要先解除Widget組件,才能去修改node的position屬性。

// 獲取節點的組件,而後移除

var widget = pauseNode.getComponent(cc.Widget);

pauseNode.removeComponent(widget);

10.動態添加節點:

// 動態添加節點 

var dynamicNode = new cc.Node('sprite 1');

// 添加Sprite組件

var sp = dynamicNode.addComponent(cc.Sprite);

// 讀取圖片紋理

var imgUrl = cc.url.raw('textures/tangguo/floor.png');

var texture = cc.textureCache.addImage(imgUrl);

// 建立渲染圖片資源

sp.spriteFrame = new cc.SpriteFrame();

// 綁定紋理

sp.spriteFrame.setTexture(texture);

// 設置節點座標

dynamicNode.setPosition(200,0);

// 將節點加入父節點

this.node.addChild(dynamicNode);

11.動畫組件:

var anim = this.player.getComponent(cc.Animation);

anim.play();

若是動畫組件在修改position屬性了,代碼中就沒法修改了。

12.Android工程導入Eclipse報錯問題:

去截圖中的位置檢查引用的lib路徑是否正確,若是不正確,你能夠把下面的lib工程導入到Eclipse裏面,

/Users/haocao/CocosCreatorWorkPlace/HelloWorld/build/HelloWorld/jsb/frameworks/cocos2d-x/cocos/platform/android/java

而後修改HelloWorld的Library依賴。

相關文章
相關標籤/搜索