1.创建牌的预制件,利用利用for循序 吊用创建预制件方法,将i值传入创建预制件中作为牌的id属性 并将其传入预制件绑定的脚本,
//------------------------------------------
onLoad: function () {
this._loadedCards = 0
this._totalNum = 52
for(var i=1;i<=this._totalNum;i++){
this._loadPic(i)
}
},
_loadPic:function(num){
var that = this
cc.loader.loadRes("pokerPrefab", function (err, prefab) {
if(err) cc.log(err)
var newNode = cc.instantiate(prefab);
cc.find('Canvas/cards').addChild(newNode);
newNode.setPosition(0,0)
newNode.scale = 0.3
newNode.getComponent('card').pub_setNum(num)
that._cardsArr.push(newNode)
that._checkLoadFinish()
});
},
_checkLoadFinish:function(){
this._loadedCards++
if(this._loadedCards == this._totalNum){
this._splitCards()
}
},
//------------------------------------
2.创建一个脚本绑定在预制件上,创建一个cardArr全局数组,在创建预制件的方法中将其node 节点push进入数组中,利用cardArr 数组创建for循环,创建一个变量 var random= parseInt(Math.random()*this._cardsArr.length)
//创建随机下标,在for循环中创建一个变量temp = this.cardArr[i] 再讲将temp 和 this._cardsArr[random] 进行交换
/---------------------------------
_splitCards:function(){ for(var i = 0;i<this._cardsArr.length;i++){ var random = parseInt(Math.random()*this._cardsArr.length) var temp = this._cardsArr[i] this._cardsArr[i] = this._cardsArr[random] this._cardsArr[random] = temp }
/-----------------------------------------------------------------
3. 将打乱的数组移动到 3个位置 创建 3个玩家数组 将其打算数组的下标push进玩家数组 ,将其移动到3个位置
发牌结束后 进入排序方法 将 在 预制件脚本创建一个新的变量 cardNum将 id =cardNum,利用判断将cardNum大小分为1- 13,之后在game脚本中获取预制件脚本中cardNum的大小,并且进行排序。
/-------------------------------------------------------------------
_sortsort: function(arr){
for(var i = 0; i < arr.length; i++)
{
for(var j = 0; j < arr.length; j++)
{
if(arr[i].getComponent("Num").value < arr[j].getComponent("Num").value)
{
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
},
_sorting:function(){
this._sortsort(this._GamePlayer1);
this._sortsort(this._GamePlayer2);
this._sortsort(this._GamePlayer3);
for(var i = 0; i < this._GamePlayer1.length;i++){
this._GamePlayer1[i].setPosition(-463, 211 - (25*i))
this._GamePlayer1[i].setLocalZOrder(i+100);
}
for(var i = 0; i < this._GamePlayer2.length;i++){
this._GamePlayer2[i].setPosition(-320+(25*i), -310)
this._GamePlayer2[i].setLocalZOrder(i+100);
}
for(var i = 0; i < this._GamePlayer3.length;i++){
this._GamePlayer3[i].setPosition(450, -235+(25*i))
this._GamePlayer3[i].setLocalZOrder(i+100);
}
},
/--------------------------------------------------------------