PHP Ionic Angularjs Phonegap AJAX Javascript CSS MySQL jQuery Forum


ลบกวนพี่ๆ ช่วยอธิบายโค้ดหน่อยนะคะ // เกมส์ Memory อยากทราบว่าเกิดอะไรในแต่ล่ะ function

ลบกวนพี่ๆ ช่วยอธิบายโค้ดหน่อยนะคะ // เกมส์ Memory อยากทราบว่าเกิดอะไรในแต่ล่ะ function

// JavaScript
var startGameBtn; 
var nextBtn; 
var allBricks; 
var daBricks; 
var bildPos; 
var brickCount; 
var matchArr; 
var turnCount; 
var count; 
var msgElem; 
var points; 
var backCount; 
var totPoints; 
var nrOfBricks;
 
 
function init(){
var i; 
 
startGameBtn = document.getElementById("startGameBtn");
nextBtn = document.getElementById("nextBtn");
allBricks = document.getElementById("bricks").getElementsByTagName("img");
backCount = document.getElementsByClassName("brickBack");
turnCount = document.getElementById("turnNr");
msgElem = document.getElementById("message");
totPoints = document.getElementById("userTotPoints");
nrOfBricks = document.getElementById("nrOfBricksMenu");
 
totPoints.innerHTML = Number(getCookie("nykaka"));
if(getCookie("nykaka") === null){ totPoints.innerHTML = 0;}
 
addListener(startGameBtn,"click",startGame);
addListener(nextBtn,"click",newTurn);
 
 
bildPos=[];
brickCount = 0;
matchArr=[];
count= 0;
 
nrOfBricks.disabled = true;
startGameBtn.disabled = false;
nextBtn.disabled = true;
 
}
addListener(window,"load",init);
 
 
function startGame(){
var i; 
var randomBrick; 
 
startGameBtn.disabled = true;
nextBtn.disabled = true;
 
for (i=0; i < allBricks.length; i++){addListener(allBricks[i],"click",turnBrick);}
 
count = 0;
turnCount.innerHTML = count;
msgElem.innerHTML = "";
 
for(i=0; i<allBricks.length;i++){
allBricks[i].className = "brickBack";
document.getElementById("bricks").getElementsByTagName("img")[i].src = "pics/backside.png";
brickCount = 0;
bildPos=[];
}
 
 
daBricks = ["0","1","2","3","4","5","6","7","0","1","2","3","4","5","6","7"];
 
for(i=0; i < allBricks.length;i++){
randomBrick = Math.floor(daBricks.length*Math.random());
bildPos += daBricks[randomBrick];
daBricks.splice(randomBrick,1);
}
 
matchArr = [];
 
function turnBrick(){
var i; 
 
for(i=0; i<16;i++){
if(this === document.getElementById("bricks").getElementsByTagName("img")[i]){
if(brickCount > 1){alert("
"You can only turn two bricks"!"); return;}
document.getElementById("bricks").getElementsByTagName("img")[i].src = "pics/" + bildPos[i] + ".png";
document.getElementById("bricks").getElementsByTagName("img")[i].className="brickFront";
brickCount+= 1;
matchArr.push(bildPos[i]);
removeListener(allBricks[i],"click",turnBrick)
}
}
 
if (brickCount === 2) {
count += 1;
turnCount.innerHTML = count;
nextBtn.disabled = false;
}
 
if ( backCount.length ===0){
endGame();
}
}
 
 
function newTurn(){
var i;
 
if(matchArr[0] === matchArr[1]){
for(i=0; i < allBricks.length;i++){
if(allBricks[i].className==="brickFront"){
allBricks[i].src = "pics/empty.png";
allBricks[i].className = "brickEmpty";
matchArr=[];
brickCount=0;
nextBtn.disabled = true;
}
}
}
else{
for(i=0; i <allBricks.length; i++){
if(allBricks[i].className==="brickFront"){
allBricks[i].className="brickBack";
allBricks[i].src = "pics/backside.png";
addListener(allBricks[i],"click",turnBrick);
matchArr=[];
brickCount=0;
nextBtn.disabled = true;
}
}
}
}
 
 
function endGame(){
var cookiePoints; 
var totalPoints; 
 
startGameBtn.disabled = false;
nextBtn.disabled = true;
 
points = 20 - (count - 8)*1.2;
points = Math.round(points);
if(points<0){points=0;}
msgElem.innerHTML = "You solved the game" + count + "Turns, it will be" + points + "point!";
 
totalPoints = 0;
cookiePoints = getCookie("nykaka");
 
if (cookiePoints === isNaN){
totalPoints = 0;
}
else{
totalPoints += points + Number(cookiePoints);
totPoints.innerHTML = totalPoints;
}
setCookie("nykaka", totalPoints);
}//End endGame


โดย:  T Churairat ChanPhithak IP: 77.243.148.xxx วันที่: 03-05-2017 เวลา: 04:23:36

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ


  • ( หรือ สามารถทำการ สมัครสมาชิก และล็อกอิน ด้วย ปุ่ม Log in with Facebook ด้านล่าง )
 ความคิดเห็นที่ 1


โดย:  Ninenik IP: 14.207.45.xxx วันที่: 03-05-2017