// Learn cc.Class:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
itemTemplate: {
default: null,
type: cc.Prefab
},
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
this.xCount = 10;
this.yCount = 10;
this.iconCount = 6; //圖片數是5個, 計數到6重新開始計數。
this.cleanMap();
this.initMap();
},
start() {
},
// update (dt) {},
cleanMap: function () {
//地圖
this.mMap = new Array(); //先聲明一維
for (var i = 0; i < this.xCount; i++) { //一維長度爲5
this.mMap[i] = new Array(i); //在聲明二維
for (var j = 0; j < this.yCount; j++) { //二維長度爲5
this.mMap[i][j] = 0;
console.log("arr[" + i + "][" + j + "]" + this.mMap[i][j]);
//console.log("\n");
}
}
},
initMap() {
console.log("initMap");
//這裏x用來控制地圖數組的每格的值,y用來控制兩次反轉,即每兩個格標記同一個值
let x = 1;
let y = 0;
// 數組下標從0開始,這裏從1開始遍歷,那麼最外面一圈就是0不變
for (let i = 1; i < this.xCount - 1; i++) {
for (let j = 1; j < this.yCount - 1; j++) {
// 地圖數組賦值
this.mMap[i][j] = x;
// y控制反轉,x控制每格值增加,增大到圖片總數後從再1開始遞增
if (y == 1) {
x++;
y = 0;
if (x == this.iconCount) {
x = 1;
}
} else {
y = 1;
}
}
}
this.change();
this.drawMap();
},
// 隨機改變一下棋局位置
change() {
// 臨時變量,用來交換兩個位置的數據
let tempX, tempY, tempM;
// 遍歷地圖數組,隨機交換位置
for (let x = 1; x < this.xCount - 1; x++) {
for (let y = 1; y < this.yCount - 1; y++) {
tempX = this.GetRandomNum(1, this.xCount - 2);
tempY = this.GetRandomNum(1, this.yCount - 2);
tempM = this.mMap[x][y];
this.mMap[x][y] = this.mMap[tempX][tempY];
this.mMap[tempX][tempY] = tempM;
}
}
},
// 獲取最小值到最大值之間的整數隨機數([min,max])
GetRandomNum: function (Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return (Min + Math.round(Rand * Range));
},
indextoScreen: function (x, y, iconSize,icon) {
//return new cc.Point(x * iconSize, y * iconSize);
icon.setPosition(x * iconSize, y * iconSize);
},
drawMap: function () {
// 繪製棋盤上精靈
for (let x = 0; x < this.xCount; x++) {
for (let y = 0; y < this.yCount; y++) {
if (this.mMap[x][y] > 0) {
//let iconName[64] = { 0};
//let iconName = []; // array to store spawned iconName
//this.iconName = [];
// 格式化圖片名
//sprintf(iconName, "%d.png", this.mMap[x][y]);
let iconName = "";
iconName = this.mMap[x][y] + ".png";
let icon = cc.instantiate(this.itemTemplate);
icon.spriteFrame = new cc.SpriteFrame(iconName);
let position = this.indextoScreen(x, y,icon.height/2, icon);
// 所有圖片已經加到幀緩存,這裏直接用名字取一幀,創建一個精靈
//let icon = Sprite:: createWithSpriteFrame(SpriteFrameCache:: getInstance() -> getSpriteFrameByName(iconName));
// icon->setAnchorPoint(Vec2(0, 0));
//icon.setPosition(position);
// 設置一個tag,方便後續識別點擊的是那個圖標
//let tag = (this.yCount - 2) * (x - 1) + y;
// 這是Z序和tag值,Z序設置大一些確保圖標在其他精靈的前面
//this.addChild(icon, 100, tag);
//this.node.addChild(icon, 100, x);
}
}
}
},
});
TestProj
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.