- Home /
Puzzle Box Level Complete - Help Needed!
I am developing a small puzzle game where the user needs to create the image on a 4x4 puzzle box within a time limit. I have so far coded it so once the timer hits 0 the game over screen will appear but I need a way to ccomplete the level once the image is created.
I will use screenshots to show you what I have.
Current Game Screen when run: http://gyazo.com/e9f86b98c523f449238660255b4c7eb2
I have 16 tiles to hold all 15 images, 1 is left blank to allow for movement. http://gyazo.com/e5db02d98da940cd7d1b0a5a23b97552
Can anyone help me code a win sequence once the tiles reach the correct layout as shown in the 2nd print screen. I will be very gratefull as this is due in in a few weeks.
How to check for a winning position heavily depends on your code. How you do your layout? How and where do you represent positions of the tiles? How do you move the tiles? Post some code or give a good description and likely someone can point you in the right direction.
This is the code for the bricks actual motion in javascript form.
var emptyslot : Transform;
var xtemp;
var ytemp;
var slide : AudioClip;
var complete;
function On$$anonymous$$ouseUp()
{
if (Vector3.Distance(transform.position,emptyslot.position)==1)
{
xtemp = transform.position.x;
ytemp = transform.position.y;
transform.position.x=emptyslot.position.x;
transform.position.y=emptyslot.position.y;
emptyslot.position.x=xtemp;
emptyslot.position.y=ytemp;
audio.PlayOneShot(slide);
}
}
@ HeavensVibe i am the beginner and im also trying to make the slider puzzle game..,i just want to know how did you shuffle the cubes in the puzzle..?? to move i also used the same code please help me
Answer by Meater6 · Feb 25, 2013 at 02:56 PM
I suggest having a "correct" tile position for each of the 15 images. Simply check if they are all in the right place. Check every time a block moves instead of every frame to be more efficient.
Answer by robertbu · Feb 25, 2013 at 05:38 PM
Given how you are handling your movement, I'd create a script that is attached to each tile object. In that script I'd store the winning position for that tile. If you know the positions where you are going to place the tiles, you can just specify the position in the inspector. Or you can specify a Transform variable and drag and drop the tile objects onto the variables (you'll need an empty game object in the open slot). So if a tile is current at (2,1) and its winning position is (0,2), then you drag the game object at (0,2) onto the transform variable. Then you can have a method to check if the Tile is at is home position. Something like (untested):
public var transHome : Transform;
private var v3HomePos : Vector3;
function Start () {
v3HomePos = transHome.position;
}
function IsHome() {
return (transform.position - v3HomePos).magnitude < 0.01;
}
Say this was the "Tile" component. If you had a script at the root object of all the tiles you could do something like:
private var tiles : Component[];
function Start () {
tiles = GetComponentsInChildren (Tile);
}
function HasWon() : boolean {
for (var tile : Tile in tiles) {
if (!tile.IsHome())
return false;
}
return true;
}
Im not fully sure how I should execute this. This is what I have so far.
I have created a new script called "Tile" which contains this piece of code:
public var transHome : Transform;
private var v3HomePos : Vector3;
function Start () {
v3HomePos = transHome.position;
}
function IsHome() {
return (transform.position - v3HomePos).magnitude < 0.01;
}
I then applied this to my 8 tiles (changed from 4x4 to 3x3 for ease) leaving the empty slot without anything. Using the inspector I assigned each tile its correct position so for example the print screen below shows Tile R1-2, but the correct placement should be Tile R3-3 which I have allocated in the inspector.
http://gyazo.com/700117226a9b735834e914ae7af45f5d
Is this correct? What should I do next to make the level complete once all the correct positions have been filled? What do I use the 2nd piece of code for? and where/which script to i add this too?
What you did looks right. Note you'll need an empty game object in the open slot, so you have a position for the piece that will get assigned to that slot.
The game is solved when all the pieces are in the home positions. The code above has each piece checking its own home position. To check if they are all in their home positions, you need to query each piece to see if it at its home position. There are a number of ways of getting and checking all the pieces. The code above is a suggestion and would be attached to a game object that is the parent of all the tiles. Anytime you wanted to know if the game was solved, you would call HasWon(). If it returns true, then the game is won.
I have created a javascript called HasWon and used the code you provided. I have allocated a home for the emptyslot which is already its start position to begin wiith. I am however confused as to what to do next.
So far I have an empty object holding all of my tiles in my hierarchy labled "TILES" is this where I would apply the HasWon script if not then where? http://gyazo.com/029c370557321bf957e5d8fa4f345357
From the look of this script it doesnt seem to be automated, im wanting the puzzle to automatically move onto the next level once the image is correctly matched and i cannot get it to do that.
Yes the scrip is attached to the empty game object which is the parent of all the tiles. I don't know anything about how to move on to the next level, but a simple solution would be to check HasWon() either every frame, or at a regular frequency:
function Update()
{
if (HasWon())
//Code to go to the next level;
}
It would be a bit more complicated to have it only check when a piece moves.
I think i've confused myself even further and made it worse :S
private var tiles : Component[];
function Start ()
{
tiles = GetComponentsInChildren (Tile);
}
function HasWon() : boolean
{
for (var tile : Tile in tiles)
{
if (!tile.IsHome)
return false;
}
return true;
}
function Update()
{
if (HasWon == true)
{
NextLevel();
}
}
function NextLevel()
{
Application.LoadLevel("Level 2");
}
So far I have this, but the game will not move onto "Level 2" once the image is correct, I assume my update function is incorrect but I have no idea how set anything beyond what I currently have :S
Your answer
Follow this Question
Related Questions
Activating Completed Level Panel using if statement 0 Answers
Complete Level Script 1 Answer
Detect the previous level? 2 Answers
Video showing in the new level 0 Answers
How to set up a task that after completed sends you to next level? 2 Answers