Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    randomising images

    Hiya all with the code i have

    http://www.mikecotton.com/johnny/joh...vo_puzzle.html

    how do i create a button so that the user can scramble(mix) the puzzle up when the button is cliked??

    fanx

  • #2
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A quick and dirty way would be to add this code - stolen and newbified from Vladdy's post to answer the same question

    Code:
    // number of cells - assumes the index starts with 1
    var length=16
    
    // modified from your switch_cells()
    function swap_cells(a,b)
    {
      a_td = document.getElementById( "cell"+a );
      b_td = document.getElementById( "cell"+b);
      temp_src = b_td.firstChild.src;
      b_td.firstChild.src = a_td.firstChild.src;
      a_td.firstChild.src = temp_src;
    }
    
    function shuffle_cells()
      { for(var i=1; i<=length; i++)
          { ind1 = Math.floor(Math.random()*length);
            ind2 = Math.floor(Math.random()*length);
            swap_cells(ind1,ind2);
          }
        return;
      }
    and then call this function OnClick of a button
    Code:
    <input type=button name=Shuffle value=Shuffle OnClick="shuffle_cells()">
    see if that works... Gurus please do suggest a better method.. I am willing to learn
    Nobody is Perfect. I am Nobody.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    	function shuffle()
    	{
    	  var tempArray=document.getElementById('table2').getElementsByTagName('img');
    		var tempArray2=new Array(tempArray.length);
    		for(I=0;I<tempArray.length;I++)
    		{
    		  var P=Math.floor(Math.random()*(tempArray.length-I));
    			tempArray2[I]=tempArray[P].src;
    			for(J=P;J<tempArray.length-1;J++)
    			{
    			  tempArray[J].src=tempArray[J+1].src;
    			}				
    		}
    		for(I=0;I<tempArray.length;I++)
    		{
    		  tempArray[I].src=tempArray2[I];
    		}
    	}
    Code:
    <input type=button name=Shuffle value=Shuffle onClick="shuffle();">
    Last edited by Garadon; 03-12-2004 at 08:04 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •