0

i have a bit of a problem.

For school we are working on making a children's game. Now we have 5 different assignments they need to go through.

So every game (i called it) is an function.

Now when you click on the cloud the array should randomly pick one and set that up.

I searched all over but couldn't find it.

Can someone help me?

This is the overview of the game

function Game1, Game2, Game3, Game4 and Game5 are the 1st one (it's basically find the right time by the clock) But this works.

If you look at the second one you see the cloud which says goed gedaan (means good job and a button says next) Now if you push that button 1 of the functions Game 1 till 5 needs to be called to play again.

My question is how do i do that

function Start(){    
    $("#start").click(function(){
          $("#start").hide();
          testStart();
          });
        }

    function testStart(){
        if($('#start').is(':hidden')) {
            Game1();
        }
    }

    function Game1(){
        $(".klok1").css("visibility", "visible");
        $(".opt1").css("visibility", "visible");
        $(".opt2").css("visibility", "visible");
        $(".opt3").css("visibility", "visible");
        $(".tekst").css("visibility", "visible");
            $(".klok1").attr("src", "game1/klok1.png");
                $(".opt1").click(function(){
                $(this).hide();
                });
                $(".opt2").click(function(){
                $(this).hide();
                });
                $(".opt3").click(function(){
                $("#volgende").css("visibility", "visible");
                });
    } 

    var RandomGames = [Game1, Game2, Game3, Game4, Game5];

    function Volgende(){
        $("#volgende").click(function(){
            alert ("functie volgende opgeroepen");
            $("#volgende").css("visibility", "hidden");
            shuffle[RandomGames];
    })

}

    function Game2(){
        $(".klok1").css("visibility", "visible");
        $(".opt1").css("visibility", "visible");
        $(".opt2").css("visibility", "visible");
        $(".opt3").css("visibility", "visible");
        $(".tekst").css("visibility", "visible");
            $(".klok1").attr("src", "game2/klok2.png");
                $(".opt1").attr("src", "game2/optie1.png");
                $(".opt1").click(function(){
                $("#volgende").css("visibility", "visible");
                });
                $(".opt2").attr("src", "game2/optie2.png");
                $(".opt2").click(function(){
                $(this).hide();
                });
                $(".opt3").attr("src", "game2/optie3.png");
                $(".opt3").click(function(){
                $(this).hide();
                });
    }

    function Game3(){
        $(".klok1").css("visibility", "visible");
        $(".opt1").css("visibility", "visible");
        $(".opt2").css("visibility", "visible");
        $(".opt3").css("visibility", "visible");
        $(".tekst").css("visibility", "visible");
            $(".klok1").attr("src", "game3/klok3.png");
                $(".opt1").attr("src", "game3/optie1.png");
                $(".opt1").click(function(){
                $(this).hide();
                });
                $(".opt2").attr("src", "game3/optie2.png");
                $(".opt2").click(function(){
                $("#volgende").css("visibility", "visible");
                });
                $(".opt3").attr("src", "game3/optie3.png");
                $(".opt3").click(function(){
                $(this).hide();
                });
    }  

    function Game4(){
        $(".klok1").css("visibility", "visible");
        $(".opt1").css("visibility", "visible");
        $(".opt2").css("visibility", "visible");
        $(".opt3").css("visibility", "visible");
        $(".tekst").css("visibility", "visible");
            $(".klok1").attr("src", "game4/klok4.png");
                $(".opt1").attr("src", "game4/optie1.png");
                $(".opt1").click(function(){
                $("#volgende").css("visibility", "visible");
                });
                $(".opt2").attr("src", "game4/optie2.png");
                $(".opt2").click(function(){
                $(this).hide();
                });
                $(".opt3").attr("src", "game4/optie3.png");
                $(".opt3").click(function(){
                $(this).hide();
                });
    }  

    function Game5(){
        $(".klok1").css("visibility", "visible");
        $(".opt1").css("visibility", "visible");
        $(".opt2").css("visibility", "visible");
        $(".opt3").css("visibility", "visible");
        $(".tekst").css("visibility", "visible");
            $(".klok1").attr("src", "game5/klok5.png");
                $(".opt1").attr("src", "game5/optie1.png");
                $(".opt1").click(function(){
                $(this).hide();
                });
                $(".opt2").attr("src", "game5/optie2.png");
                $(".opt2").click(function(){
                $(this).hide();
                });
                $(".opt3").attr("src", "game5/optie3.png");
                $(".opt3").click(function(){
                $("#volgende").css("visibility", "visible");
                });
    } 
  • 2
    What is your qeustion? Since you're asking about randomization I would expect to see `Math.random()` in the code somewhere. – Halcyon Mar 09 '16 at 13:47
  • 2
    So basically you're asking [**how to get random value from an array**](http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array) – adeneo Mar 09 '16 at 13:48
  • Basically my question is how to activate another random function like Game1 if i push the image #volgende – Kelly Vorstenbosch Mar 09 '16 at 18:02

2 Answers2

1

You can get randomize array using this function:

We have item array : var items = [523,3452,334,31,...5346];

using Math.random() you can get random value.

var item = items[Math.floor(Math.random()*items.length)];
kishan Radadiya
  • 788
  • 6
  • 14
0

Try this:

var RandomGames = [];
RandomGames.push(function(){
    //game code
});
//... Do this in each game.

Now for execute a game:

RandomGames[randomIndex]();
Cespejo
  • 414
  • 2
  • 11