dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: hitTest and a sack of woe

  1. #1
    Welcome to flavour country
    Join Date
    Sep 2000
    Posts
    662
    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.

    Thanks in advance,
    Mark


  2. #2
    Welcome to flavour country
    Join Date
    Sep 2000
    Posts
    662
    Obviously no one was going to answer this, but it doesn't matter if you were cause I figured it out.

  3. #3
    Aquarium drinker
    Join Date
    Nov 2000
    Location
    Greater than 45 degrees in Minkowski space
    Posts
    571
    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.squareMC1.duplicateMovieClip("squareMC" add number , number);
    					this["squareMC" add number]._x = spacing*x;
    					this["squareMC" add number]._y = spacing*y;
    				}
    				
    				this["squareMC" add number].caption = number;
    				this["square" add number] = new square(x, y);
    				this["colorMC" add number] = new Color(this["squareMC" add number].colorMC);
    			}
    		}
    	}
    
    	//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
    
    		oldSquareX = this["square" add which].x;
    		oldEmptyX  = emptySquare.x;
    		oldSquareY = this["square" add which].y;
    		oldEmptyY  = emptySquare.y;
    
    		this["square" add which].x = oldEmptyX;
    		emptySquare.x = oldSquareX;
    		this["square" add which].y = oldEmptyY;
    		emptySquare.y = oldSquareY;
    
    		chosenOne = which;
    		step = 1;
    		moving = 1;
    		
    		xDelta = spacing*(1/numSteps)*(oldEmptyX - oldSquareX);
    		yDelta = spacing*(1/numSteps)*(oldEmptyY - oldSquareY);
    
    	}
    
    	function move(){
    
    		this["squareMC" add chosenOne]._x += xDelta;
    		this["squareMC" add chosenOne]._y += yDelta;
    		
    		r = this["squareMC" add chosenOne]._x;
    		b = this["squareMC" add chosenOne]._y;
    		
    		this["colorMC" add chosenOne].setRGB(r << 16 | 127 << 8 | b);
    		
    		step++;
    		
    		if( step > numSteps ){
    		
    			//make up for lost space
    			
    			this["squareMC" add chosenOne]._x = spacing*oldEmptyX;
    			this["squareMC" add chosenOne]._y = spacing*oldEmptyY;
    		
    			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]

  4. #4
    Welcome to flavour country
    Join Date
    Sep 2000
    Posts
    662
    Damn that's cool.

    Not exactly the effect I was looking for, but the basic priciple is the same.

    Nice job.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center