# Need help regarding a puzzle game

**URL:** https://forum.kirupa.com/t/need-help-regarding-a-puzzle-game/278459
**Category:** flash
**Created:** [December 30, 2008, 2:36pm UTC](https://forum.kirupa.com/t/need-help-regarding-a-puzzle-game/278459 "2008-12-30T14:36:02Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![strawhat](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@strawhat](https://forum.kirupa.com/u/strawhat)
#### Post date: [December 30, 2008, 2:36pm UTC](https://forum.kirupa.com/t/need-help-regarding-a-puzzle-game/278459/1 "2008-12-30T14:36:02Z")

</div>

I am now working on a puzzle game however it is slighty different from a normal jigsaw puzzle game. Usually, users are able to drag the pieces so that all the pieces fit together to form a picture. In my case, users are not allow to drag the pieces but instead by clicking only.

For eg. when user click on piece1, piece1 disappears. He/She has a choice to choose which box to click (target1 - target8). If the selection is wrong, piece1 reappears on the stage. On the contray if its correct, piece1 will reappear on the postion of target1.

I have actually came out with the codes but it is VERY VERY LONG. I was wondering if there is a way to shorten it by using ‘for’ loop or is there any other way of doing it.

[COLOR=red]Example of the codes that I came out with:[/COLOR]  
[COLOR=red][/COLOR]  
[COLOR=red]//Piece 1  
piece1.onRelease = function (){  
piece1.\_visible = false;  
piece2.enabled = false;  
piece3.enabled = false;  
piece4.enabled = false;  
piece5.enabled = false;  
piece6.enabled = false;  
piece7.enabled = false;  
piece8.enabled = false;[/COLOR]

[COLOR=red] target1.onRelease = function() {  
piece1.\_x = target1.\_x;  
piece1.\_y = target1.\_y;  
piece1.\_xscale = 100;  
piece1.\_yscale = 100;  
piece1.\_visible = true;  
piece1.enabled = false;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;[/COLOR]  
[COLOR=red] }[/COLOR]

[COLOR=red] target2.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}[/COLOR]

[COLOR=red] target3.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}[/COLOR]

[COLOR=red] target4.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}[/COLOR]

[COLOR=red] target5.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}[/COLOR]

[COLOR=red] target6.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}[/COLOR]

[COLOR=red] target7.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}[/COLOR]

[COLOR=red] target8.onRelease = function(){  
//this.\_x = random(230)+380;  
//this.\_y = random(320)+80;  
piece1.\_visible = true;  
piece2.enabled = true;  
piece3.enabled = true;  
piece4.enabled = true;  
piece5.enabled = true;  
piece6.enabled = true;  
piece7.enabled = true;  
piece8.enabled = true;  
}  
}[/COLOR]

This is only for piece1 and i have 7 other pieces so you guys can imagine how long my codes are.

As for the shorter version, I am stuck some where so I hope I can get some help here

[COLOR=blue]For loop version:[/COLOR]  
[COLOR=blue][/COLOR]  
[COLOR=blue]piece = new Array();  
target = new Array();[/COLOR]  
[COLOR=blue][/COLOR]  
[COLOR=blue]for (i=1; i==8; i++) {  
this[“piece”+i].\_xscale = 70;  
this[“piece”+i].\_yscale = 70;  
[/COLOR]  
[COLOR=blue]this[“piece”+i].onRelease = function () {  
this[“piece”+i].\_visible = false;  
[/COLOR]  
[COLOR=blue]this[“target”+i].onRelease = function() {  
this[“piece”+i].\_x = this[“target”+i].\_x;  
this[“piece”+i].\_y = this[“target”+i].\_y;  
this[“piece”+i].\_xscale = 100;  
this[“piece”+i].\_yscale = 100;  
this[“piece”+i].\_visible = true;  
}  
}  
}[/COLOR]  
[COLOR=#0000ff][/COLOR]  
[COLOR=black]Greatly appreciated with you guys out there could offer some help :D[/COLOR]
