Click here to Skip to main content
15,918,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hey everyone I wrote this code this picks a random value like shown and then accordinly adds a class to that specific element. Now when the user clicks that element the class that was added gets removed (the score counter increases by '1') and the program goes back to the start and picking a random number and assigning the class. The code for this has been written below, but I am not able to loop loop the game. After the user click the forst element the whole mechanism does not repeat well and nor does the score increase. What should I do?



JavaScript
$(document).ready(function start(){

  var items = Array(11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44);
  var random = items[Math.floor(Math.random()*items.length)];
  $('.c'+random).addClass('mole');


	$("body").click(function(e) {
    	    if ($(e.target).hasClass("sq-color")) 
        	{ 
            	if ($(e.target).hasClass("mole")) 
        		{ 
            		hit.playclip();
            		$('.c'+random).removeClass('mole');
            		
            		var scoreCounter = $("b[id=show-score]").html();
                    var updateScore = eval(scoreCounter)+ eval(1);
                    $("b[id=show-score]").html(updateScore);
            		
        		}	 
        		else
        		{
            		alert("MISS");
            	}	
        	} 
        
    	});




});

EDIT: bold removed
Posted
Updated 10-Nov-12 23:12pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Nov-12 1:05am    
There is not a loop in your code, so it's hard to see what's your problem. It looks like you are trying to get away creating no functions at all; are you serious?
--SA
Member 9589729 11-Nov-12 1:45am    
Please view line #18. I did this to loop the function. But it does not work. Instead it calls the function the second time but on clicking the element in the second instance the class 'mole' gets added to 3 elements. These increases to 5 in the next instance. I dont know how else I can loop this code and get my scoring to take place correctly.

$(document).ready(function start(){
[Rest of the code removed; OP should use "Improve question" instead -- SA]
...
Sergey Alexandrovich Kryukov 11-Nov-12 1:58am    
Excuse me, do you think I should count your lines to see what you are talking about? Ever heard of such feature as code comment?
Thank you,
--SA
enhzflep 11-Nov-12 5:17am    
:-D :grins:
I think that perhaps "eval(1)" says it all?
Sergey Alexandrovich Kryukov 11-Nov-12 12:19pm    
What have you done?! I was trying hard to avoid looking at it. And what should I do now? Memory bleach? How can I unsee it? :-)
And this is not just being a beginner. This is the attitude: have no idea what to write but still writing.
--SA

1 solution

Watch this code. i think you want to do something like this.

XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
    .score
    {
        background-color: #3399ff;
        color:White;
        width:800px;
        height:200px;
        font-family: 'Comic Sans MS';
        font-size: xx-large;
        font-weight: bold;
        text-align: center;
        margin:0 auto;
        -moz-border-radius: 10px;
        -webkit-border-radius: 10px;
        border-radius: 10px;
        border: 3px solid;
    }
    body
    {
    background-color: #000000;
    }
    .reset
    {
        background-color: #000000;
        color:White;
        font-family: 'Comic Sans MS';
        font-size: xx-large;
        font-weight: bold;
        width:300px;
        height:100px;
        margin:0 auto;
        -moz-border-radius: 10px;
        -webkit-border-radius: 10px;
        border-radius: 10px;
        border: 3px solid;
        text-align:center;
    }
    div img
    {
    width:100px;
    height:100px;
    }

    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function start() {
        var random = -1;
        playclip();


        $("body").click(function (e) {
            if (e.target.nodeName.toLowerCase() == "img") {
                var scoreCounter = $("b[id='click-score']").html();
                var updateScore = eval(scoreCounter) + eval(1);
                $("b[id='click-score']").html(updateScore);

            }
            if ($(e.target).hasClass("sq-color")) {
                if ($(e.target).hasClass("mole")) {
                    // hit.playclip(); i dont knw wat method do here.
                    $('.c' + random).removeClass('mole');

                    var scoreCounter = $("b[id='show-score']").html();
                    var updateScore = eval(scoreCounter) + eval(1);
                    $("b[id='show-score']").html(updateScore);
                    $(e.target).animate({ height: 300 }, "slow");
                    $(e.target).animate({ height: 100 }, "slow");
                    $(e.target).animate({ width: 300 }, "slow");
                    $(e.target).animate({ width: 100 }, "slow");
                    playclip();


                }
                else {
                    var scoreCounter = $("b[id='miss-score']").html();
                    var updateScore = eval(scoreCounter) + eval(1);
                    $("b[id='miss-score']").html(updateScore);
                    alert("MISS");
                }
            }
            if ($(e.target).closest("div").hasClass("reset")) {
                $("b[id='show-score']").html(0);
                $("b[id='miss-score']").html(0);
                $("b[id='click-score']").html(0);
            }
        });

        function playclip() {
            var items = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            var items2 = items.slice();
            var count = 0;
            $(".container").html("");
            for (count = 0; count < items.length; count++) {
                var myImag = $("<img></img>");
                var myRandom = Math.floor(Math.random() * items2.length);
                var slicedItem = items2.splice(myRandom, 1);
                myImag.attr("src", "http://flickholdr.com/200/200/" + slicedItem);
                myImag.attr("class", "c" + slicedItem + " sq-color");

                $(".container").append(myImag);
            }
            random = items[Math.floor(Math.random() * items.length)];
            $('.c' + random).addClass('mole');

        }


    });

</script>
</head>

<body>
    <div class="container">

   </div>
    <div class="score">
    Score is here:<br />
    Click: <b id="click-score" >0</b><br />
    Hit: <b id="show-score" >0</b><br />
    Miss: <b id="miss-score">0</b><br />
    </div>
    <div class="reset">
    <h2 >Reset</h2>
    </div>
</body>
</html>
 
Share this answer
 
Comments
Member 9589729 12-Nov-12 8:14am    
This code is similar to what I have in mind the only difference is that here all the classes(which are not the 'mole' element) have different background images, thus making it hard for a player to know where the mole is. All the classes that are not the 'mole' need to appear with the same bg color/ bg image.
deepak.m.shrma 13-Nov-12 9:33am    
you can modify yar.. :-) i just take your idea n make this one...you can simly hide or do any thing..i canc uplaod folder here so i have to take online image to show some animation...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900