/    Sign up×
Community /Pin to ProfileBookmark

Hey all I’m currently learning Java script at the moment. At the moment I’m making a picture slide game using the swap function. I’m trying to make it so when I click on a square then onto a square next to it the pictures will swap around…This is what I have done so far I was wondering if any of you guys could help?

Cheers!

<html>
<title>Puzzle Page</title>
<script>
<head>

script language =(javascript )

function swap (newPosition)
{
temp =document. images[0].src;
temp =document. images[0].src=document.images[1].src;
temp =document. images[1].src=temp;

}

</script>
</head>

<body>

<TABLE border=”1″>
<TR>
<TD><img name=fruits src = fruits/fruit_0.gif onclick=swap(0);> </TD>
<TD><img name=fruits src = fruits/fruit_1.gif onclick=swap(1);> </TD>
<TD><img name=fruits src = fruits/fruit_2.gif onclick=swap(2);> </TD>
</TR>
<TR>
<TD><img name=fruits src = fruits/fruit_3.gif onclick=swap(3);> </TD>
<TD><img name=fruits src = fruits/fruit_4.gif onclick=swap(4);> </TD>
<TD><img name=fruits src = fruits/fruit_5.gif onclick=swap(5);></TD>
</TR>
<TR>
<TD><img name=fruits src = fruits/fruit_6.gif onclick=swap(6);> </TD>
<TD><img name=fruits src = fruits/fruit_7.gif onclick=swap(7);> </TD>
<TD><img name=fruits src = fruits/hole.gif height=”130″ width=”160″ onclick=swap(8);> </TD>

</TR>
</TABLE>

</body>

</html>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@hoangkcNov 29.2007 — function swap (newPosition)

{

temp =document. images[0].src;

temp =document. images[0].src=document.images[1].src;

temp =document. images[1].src=temp;

}
[/QUOTE]

It seems you always swap image0 and image1. what's the newPosition variable for?
Copy linkTweet thisAlerts:
@locksheathsaintauthorNov 29.2007 — I'm not really sure what I have done to be honest with you. Basically I want the any pictures next to the hole.gif too swap with each other? If that make sense....?
Copy linkTweet thisAlerts:
@hoangkcNov 29.2007 — not sure I got your point.

but I try to make 2 examples. Are they what you want?

always swap clicked image with image0
<i>
</i>function swap (index)
{
var next = 0;

<i> </i> temp =document.images[next].src;
<i> </i> document.images[next].src=document.images[index].src;
<i> </i> document.images[index].src=temp;

<i> </i>}



Or swap clicked image with next image, if clicked image is the last one, swap with image0

<i>
</i>function swap (index)
{
var next = index+1;
if(next&gt;=document.images.length) next = 0;

<i> </i> temp =document.images[next].src;
<i> </i> document.images[next].src=document.images[index].src;
<i> </i> document.images[index].src=temp;

<i> </i>}
Copy linkTweet thisAlerts:
@hoangkcNov 29.2007 — My network is terrible, so I also post here, will send you link in yousendit later
<i>
</i>&lt;html&gt;
&lt;title&gt;Puzzle Page&lt;/title&gt;
&lt;head&gt;
&lt;script&gt;
var hole = [2,2];
function swap(row,col)
{
if(CanSwap(row,col))
{
var img = document.getElementById("img"+row+col)
document.getElementById("img"+hole[0]+hole[1]).src = img.src ;
img.src = "fruits/hole.gif";
hole=[row,col];
}
}
function CanSwap(row,col)
{
return (Math.abs(row-hole[0])+Math.abs(col-hole[1]))==1;
}
&lt;/script&gt;
&lt;style&gt;
img{height:130px; width:160px}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;TABLE border="1"&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;img id="img00" src = "fruits/fruit_0.gif" onclick=swap(0,0);&gt; &lt;/TD&gt;
&lt;TD&gt;&lt;img id="img01" src = "fruits/fruit_1.gif" onclick=swap(0,1);&gt; &lt;/TD&gt;
&lt;TD&gt;&lt;img id="img02" src = "fruits/fruit_2.gif" onclick=swap(0,2);&gt; &lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;img id="img10" src = "fruits/fruit_3.gif" onclick=swap(1,0);&gt; &lt;/TD&gt;
&lt;TD&gt;&lt;img id="img11" src = "fruits/fruit_4.gif" onclick=swap(1,1);&gt; &lt;/TD&gt;
&lt;TD&gt;&lt;img id="img12" src = "fruits/fruit_5.gif" onclick=swap(1,2);&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;img id="img20" src = "fruits/fruit_6.gif" onclick=swap(2,0);&gt; &lt;/TD&gt;
&lt;TD&gt;&lt;img id="img21" src = "fruits/fruit_7.gif" onclick=swap(2,1);&gt; &lt;/TD&gt;
&lt;TD&gt;&lt;img id="img22" src = "fruits/hole.gif" onclick=swap(2,2);&gt; &lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@locksheathsaintauthorNov 29.2007 — Mate you are a legend! Cheers so much! Is there anyway I can put a button in there so it goes back too the original picture and randomizes it? Thanks so much again!
Copy linkTweet thisAlerts:
@hoangkcNov 30.2007 — you should have an array which hold the right_state (ie : the src of image00 is fruit_0, the src of image01 is fruit_1, etc) then when you click the "solve" button, you set image src as your array.

randomize button is done by the same way. You have to have some array which holds the pre-define src state of image, note: those status is solvable.

If you generate randomly images (without pre-define state array) , you can make some states which can't be solved
×

Success!

Help @locksheathsaint spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...