21 Replies Latest reply on Jan 2, 2009 1:09 PM by Ned Murphy

# Where am I going wrong?

I am trying to create a click and slide puzzle for my site and after hours of checking I am still getting errors;

function initGame () {
// set the horizontal and vertical distance
// between tiles
tileDist = 100;

// set all tiles in exactly the correct spot
for (x=1;x<=4;x++) {
for (y=0;y<=3;y++) {
tile = x+y*4;
_root["tile"+tile]._x = x*tileDist;
_root["tile"+tile]._y = y*tileDist+tileDist;
}
}

// make 100 random but valid moves
for(tilenum=0;tilenum<100;tilenum++) {
do {
// pick a random tile
tile = "tile"+(random(15)+1);
// see whether there is an empty space near it
emptySpace = findEmpty(tile);
// keep looping until a tile is found that
// has an empty space near it
} while ( emptySpace == "none" );

// move this tile to the empty space
moveTile(tile,findEmpty(tile));
}
}

// given a tile, see if the empty space is near it
function findEmpty (tile) {
// get location of tile
tilex = _root[tile]._x;
tiley = _root[tile]._y;

// see whether there is a tile to the left
if (tilex > tileDist) {
if (!tileThere(tilex-tileDist, tiley)) {
return("left");
}
}

// see whether there is a tile to the right
if (tilex < tileDist*4) {
if (!tileThere(tilex+tileDist, tiley)) {
return("right");
}
}

// see whether there is a tile above
if (tiley > tileDist) {
if (!tileThere(tilex, tiley-tileDist)) {
return("above");
}
}

// see whether there is a tile below
if (tiley < tileDist*4) {
if (!tileThere(tilex, tiley+tileDist)) {
return("below");
}
}

// tiles are in all directions
return("none");
}

// check to see whether there is a tile at a certain location
function tileThere (thisx, thisy) {
// loop through tiles
for (i=1;i<=15;i++) {
// see if x matches
if (_root["tile"+i]._x == thisx) {
// se if y matches
if (_root["tile"+i]._y == thisy) {
return true;
}
}
}

// no tile there
return false;
}

// move a tile in a certain direction
function moveTile (tile, direction) {
if (direction == "above") {
_root[tile]._y -= tileDist;
} else if (direction == "below") {
_root[tile]._y += tileDist;
} else if (direction == "left") {
_root[tile]._x -= tileDist;
} else if (direction == "right") {
_root[tile]._x += tileDist;
}
}

// utility function to see on which tile the
// player clicked
function tileUnderMouse () {
for (i=1; i<=15; i++) {
if (_root["Tile"+i].hitTest(_xmouse, _ymouse)) {
return (i);
}
}
}

initGame();
stop();

The code above has been taken and adapted (unsuccessfully, obviously) from the Flash MX book eventhough I am using Flash 8. From a novice point of view I would not have thought that there was much difference, but again I am obviously wrong.

The problem I am finding is that when I CTRL/Return to preview it, it takes ages and then I get the message that the movie is causing the Flash Player to run slowly and asks if I want to continue. If I choose yes, this message cycles around and round. If I chose no then I get the file to open but all the pieces are not set right and nothing moves.

Any help will be hugely appreciated!

Cheers

Nig
• ###### 1. Re: Where am I going wrong?
Use the attach code option. Your code is difficult to follow when it is all left justified (which including it in the posting as you did will do by default).
• ###### 2. Re: Where am I going wrong?
Sorry
• ###### 3. Re: Where am I going wrong?
do you have tile1,...,tile15 on stage?
• ###### 4. Re: Where am I going wrong?
I have all 15 tiles on the stage in order. I have 2 layers, one with the tiles on and the other for the actions.
• ###### 5. Where am I going wrong?
Thanks... what I was looking for is now more easily apparent... Any time I see a Do...While involved I get suspscious. Try putting a trace inside that loop and see if it never stops. It may not be the problem, but it's worth checking.
• ###### 6. Re: Where am I going wrong?
Ned, how do I do a trace please and where ought it go? Thanks
• ###### 7. Where am I going wrong?
Insert the trace after this line of code:

emptySpace = findEmpty(tile);
trace(emptySpace);

trace results display in the output panel. It's a handy tool for checking things and troubleshooting code.

In your case, if it is continuously displaying 'none' in the output panel, it means your loop is not closing at any point (soon).
• ###### 8. Re: Where am I going wrong?
I entered the code in the place you mentioned and the message that came up was the same but the Output window read

none
none
none
none

and on and on for loads.
• ###### 9. Re: Where am I going wrong?
if you didn't leave an empty space for a tile move, you're going to have an endless loop trying to find an empty space.

but i'm not sure how you could have done that. even if you have 16 tiles on-stage, your code is only checking against 15 so it should find (what it thinks is) an empty space.

• ###### 10. Re: Where am I going wrong?
there's no problem with your code. you must be doing something screwy in the authoring environment.

• ###### 11. Re: Where am I going wrong?
OK, I think the best thing is to upload it for you to see where I am going wrong. I hope it is something simple. So,

www.ultimate-top-trumps.co.uk/Space Phenomena.fla

Hopefully this will help.
• ###### 12. Where am I going wrong?
I took a quick look at your file and you have a ways to go before you can start testing the code for it. Since kGlad is unlikely to look at the file, I'll describe what I found... it's a quick but accurate description...

There are 15 Graphic symbols (images) placed on the stage. These are all that exist as symbols in the library. Fini.

You'll need to develop the stage elements into movieclips with instance names before you can start dealing with the code for them.
• ###### 13. Re: Where am I going wrong?
Ah, poo! I keep menatlly beating myself up about this. I have changed these now to Movie symbols. Typical noob issue! Sorry!
• ###### 14. Re: Where am I going wrong?
i didn't anticipate that.

but it would cause the problem and definitely meets the "you must be doing something screwy in the authoring environment" test.
• ###### 15. Re: Where am I going wrong?
Yeah, I figured you'd like to know since your diagnosis was (normally is) correct. I had no intention of trying to embarrass Nigs, so hopefully that isn't the case... stuff happens.
• ###### 16. Re: Where am I going wrong?
Even though I have changed all the graphic symbols to Movie symbols it still shows up that the script is causing Flash Player to run slowly etc.
• ###### 17. Where am I going wrong?
Have you given them instances (tile1 thru tile15)? And since there were only 15 things on the stage, did you create/add the empty tile (tile16 I'd assume)?

I'm not sure how identifying the empty tile fits into your code, but you may know what's intended there.

Suggestion for consideration later: Overall, it might make things easier if you somehow define sectors (an array) for the 16 locations and assign each sector to each piece as it lands in one. This may make identifying if one is adjacent to the empty one or not easier.
• ###### 18. Re: Where am I going wrong?
This is by far the most adventurous thing I have tried so I am really learning at every step.

Instances? No, but I will as I can see the logic now it has been pointed out. Doh!

I did have a tile 16 left over when I saved the sliced image but did not import it with the others as it is not needed as it will leave no room to move. (?)

An array? Do you mean set places on the stage that the pieces sit in? Again, obvious now it has been pointed out.

The code is coped from the book Flash MX Actionscript for Games and was assuming that what I had done on the stage combined with the code would go a long way to it all working. But I guess that is just being too naive. Once I have it working I can then start to play around and see what happens if I remove or add something, to see what happens. That way I can make it all sink in leading to bigger and better things. Same way as I taught myself Photoshop and Dreamweaver.
• ###### 19. Where am I going wrong?
That's what happens with Flash. I've been at it for about 8 years and still learn new stuff on a regular basis. If you have the drive to weed thru these things yourself (and lose hair/sleep), go for it. I'm self taught as are others that help out in these forums, but the hairloss started somewhere long before Flash existed.

You will need a tile16 (or something) to be able to determine where the empty slot is. If you have an empty tile named tile16, you'll never have to search for it, just check to see if someone is near it, and then switch places between them.

That's why sectors might be easier to deal with... each sector could have a tile (or not) defined for it as things move around (or vice versa with the tiles identifying their current sector).
• ###### 20. Re: Where am I going wrong?
Thanks Ned, you've given me something to think about and play with. It is so frustrating that it is the easier simple things you miss becasue it just has to be more complicated. Heck, people are just so complicating!

Cheers Ned, I will probably be back but hopefully not with this issue! Happy New Year!!
• ###### 21. Re: Where am I going wrong?
You're welcome. This sounds like a challenging introduction for you that you will learn some good stuff from. Before you come back, be sure to have struggled with trying to figure stuff out... it's the best way to learn what doesn't work as well as what does.