2012-02-28 143 views
1

好的。我很難找出答案。我有一個遊戲,有一個問題和四個圖像,回答這個問題需要點擊正確的圖像。創建圖像問答遊戲

代碼有很多問題(我知道)我只是試圖分解它來獲得一些工作。

如何讓程序更好地與自己交談,例如,現在如果我點擊任何圖像,我會根據image[2] = true;得到反應。但我似乎無法寫出一個if語句,說如果這個圖像被點擊,然後做一些事情。任何幫助或建議,讓我在這個駝峯會很好。

<div class="game"> 

    <div class="round round1"> 
     <div class="question"> If put in alphabetical order, which image would come third? </div> 
     <div class="image image0" > <img src="images/image1.gif" /></div> 
     <div class="image image1" ><img src="images/image2.gif" /> </div> 
     <div class=" image image2" > <img src="images/image3.gif" /></div> 
     <div class="image image3" > <img src="images/image4.jpg" /></div> 
    </div> 
</div> 
<script> 
    var image = [false, false, false, false]; 
    var imageName = ["Car Image","Boat Imagege","Scooter Image","Snow Mobile Image"]; 

    $(".image0").click(function() { 
     image[0] = true;  
     $(".question").replaceWith("The " + imageName[0] + " image is incorrect"); 
     $('.image0').css({"background-color":"red"}); 

    }); 

    $(".image1").click(function() { 
     image[1] = true;  
     $(".question").replaceWith("The " + imageName[1] + " image is incorrect"); 
     $('.image1').css({"background-color":"red"}); 
    }); 


    $(".image2").mouseup(function() { 
     image[2] = true; 

     $(".question").replaceWith("Correct, the answer is <b>" + imageName[2] + "</b> image"); 
     $('.image2').css({"background-color":"#95d456"}); 

    }); 

    $(".image3").click(function() { 
     image[3] = true; 

     $(".question").replaceWith("The " + imageName[3] + " image is incorrect"); 
     $('.image3').css({"background-color":"red"}); 

    }); 
</script> 

回答

2

點擊圖像後,您的點擊功能已經執行。你可以讓「正確」的圖像執行你需要的任何代碼(以及更新你的數組)。否則,你可以寫一個for循環來檢查image []是否爲真值。

+0

'這樣的事情? 。 \t \t \t \t \t \t $(」。ROUND1" )點擊(函數(){\t \t \t \t \t \t \t爲(I = 0; I <= 3;我++){ \t \t \t \t \t \t \t \t $( 「圖像」 + I)。點擊(函數(){ \t \t \t \t圖像[I] = TRUE; \t \t \t \t}); \t \t \t \t \t \t \t \t \t如果(圖像[I] == TRUE){ \t \t \t \t \t \t $( 「問題」。)replaceWith(imageName [I] + 「是正確的」)。 \t \t \t \t \t \t $('。image2')。CSS({ 「背景色」: 「綠色」}); \t \t \t \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t \t} \t \t \t \t}); ' – 2012-02-29 00:34:40

+0

我不知道如何在這些評論框後的代碼。這個'和'似乎並沒有訣竅,所以對於這個爛攤子抱歉 – 2012-02-29 00:35:24

0

「但我似乎無法寫if語句,說如果這個圖像是 點擊然後做點什麼。」

您已經有圖像的onclick事件,那麼有什麼問題?

嘗試在您的頁面上包含jquery/javascript腳本標記,並用$(function(){ });環繞您的​​腳本。