 A Flash Developer Resource Site

# Thread: hitTest and a sack of woe

1. Ok, I have a 3x3 grid of squares which are overlaid by another 3x3 grid of squares. There are only eight ovelaid squares though so there is one blank spot at all times. At certain points in the movie the overlaid squares change position. I've got a function which will determine which of the spots is blank, but what I need is to be able to determine which overlapping square is over which underlying square.

The overlying squares are named: tile1, tile2.......tile8

The underlying squares (which are there for the purposes of determining the blank spot using hitTest) are named: hit1, hit2....hit9

Here's the function I've used to find the blank square.

Code:
```function findblank () {
for (x=1; x<=9; x++) {
for (y=1; y<=9; y++) {
if (_root["tile"+y].hitTest(_root["hit"+x])) {
_root["hit"+x].taken = true;
} else {
}
}
}
for (i=1; i<=9; i++) {
if (_root["hit"+i].taken != true) {
_root.oneleft = i;
}
}
}```
So I need to be able to find which overlying square is over which underlying square and return that target in some form that I can then target the overlying mc.

Mark  Reply With Quote

2. Obviously no one was going to answer this, but it doesn't matter if you were cause I figured it out.  Reply With Quote

3. Hehe, too bad for me I guess, I spent a good part of the afternoon trying to do something like this. The result works great. Here's the swf:

[swf height="400" width="400" background="#ffffff"]http://www.rprog.f2s.com/squares.swf[/swf].

And the actionscript (only 164 lines!!!)

Code:
```onClipEvent( load ){

numSteps = 5;
numXSquares = 9;
numYSquares = 9;
spacing = 29.5;

//Set up the empty square object

emptySquare = new Object();
emptySquare.x = numXSquares - 1;
emptysquare.y = numYSquares - 1;

totalSquares = numXSquares*numYSquares - 1;

//Just a shortcut for the for for loops

function getNumber(x,y){

return (x*numXSquares + 1 + y);

}

//set up of the square object

function square(x, y){

this.x = x;
this.y = y;

}

for( x = 0; x < numXSquares; x++){
for ( y = 0; y < numYSquares; y++){

number = getNumber(x,y);
if( number <= totalSquares){

if( number != 1){

}

this["square" add number] = new square(x, y);
}
}
}

//The relevant array holds the numbers of the squares that have an adjacent empty Square

relevant = new Array();
chosenOne = 0;

// **********************************************************************************
//                           End init \ Start the "real" script
// **********************************************************************************

function cleanRelevantArray(){

//for( i = 0; i < relevant.length; i++ ){

//	relevant.pop();

//}
relevant = new Array();
}

function findRelevant(){

i = 0;
for( x = 0; x < numXSquares; x++){
for ( y = 0; y < numYSquares; y++){

number = getNumber(x,y);

//This condition checks for the number of squares between the square and the empty square
//If it is 1, then the square is adjacent to the empty square.
//The condition also bans the square that was chosen on the previous turn (the «chosen one»)

if( number <= totalSquares && chosenOne != number && (Math.abs( this["square" add number].x - emptySquare.x) + Math.abs( this["square" add number].y - emptySquare.y)) == 1){

relevant[ i ] = number;
i++;

}
}
}
}

function randomRelevantSquare(){

return relevant[ random( relevant.length ) ];

}

function getNewPosition( which ){

//Make the switch in the variables

oldEmptyX  = emptySquare.x;
oldEmptyY  = emptySquare.y;

emptySquare.x = oldSquareX;
emptySquare.y = oldSquareY;

chosenOne = which;
step = 1;
moving = 1;

xDelta = spacing*(1/numSteps)*(oldEmptyX - oldSquareX);
yDelta = spacing*(1/numSteps)*(oldEmptyY - oldSquareY);

}

function move(){

this["colorMC" add chosenOne].setRGB(r << 16 | 127 << 8 | b);

step++;

if( step > numSteps ){

//make up for lost space

moving = 0;

}

}
}

onClipEvent( enterFrame ){

if( !moving ){

cleanRelevantArray();
findRelevant();
getNewPosition( randomRelevantSquare() );

}

else{

move();

}
}```
Maybe I should get a life and stuff... [Edited by pmineault on 05-31-2001 at 01:24 AM]  Reply With Quote

4. Damn that's cool. Not exactly the effect I was looking for, but the basic priciple is the same.

Nice job.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 