...

View Full Version : randomising images



baz7621
03-12-2004, 02:49 AM
Hiya all with the code i have

http://www.mikecotton.com/johnny/johnny_bravo_puzzle.html

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

fanx

Unit
03-12-2004, 04:23 AM
A quick and dirty way would be to add this code - stolen and newbified from Vladdy's post to answer the same question :D




// 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


<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 :thumbsup:

Garadon
03-12-2004, 09:02 AM
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];
}
}



<input type=button name=Shuffle value=Shuffle onClick="shuffle();">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum