PDA

Click to See Complete Forum and Search --> : expanding a grid and maintaining proportions (MX &oop)



mbannonb
08-29-2002, 05:14 PM
Okay, this works only for numbers that are even and perfect squares (16, 36, etc.).

How might I modify it so that it can be done with any even number and still only require the number of pieces as the only thing that needs changing?

http://www.inflash.com/pattern_match_final.swf

Everything hinges on the number of pieces (num_pieces):




function GameTable(){
}
//set up the tiles with proper size and position on the stage
GameTable.prototype.createTable=function(){
num_rows=Math.sqrt(num_pieces);
size_piece=(Stage.width/num_pieces)*num_rows;
for(i=0; i<num_pieces; i++){
depth++;
if (i%num_rows==0){
x_pos=0;
row++;
}else{
x_pos+=size_piece;
}
y_pos=(row-1)*size_piece;
attachMovie("square", "square"+i, depth,{_x:x_pos, _y:y_pos, piece_num:i});
_root["square"+i]._height=size_piece;
_root["square"+i]._width=size_piece;
}
attachMovie("feedback", "playerFeedback_mc", 1000, {_x:150,_y:470});
}
//assign a match for each tile and let each tile know who it is match to
GameTable.prototype.setPattern=function(){
is_set=[];
for(i=0; i<(num_pieces/2); i++){//loop through the bottom pieces and assign them a random matching upper piece
do{
this.piece_matches=random(num_pieces/2)+(num_pieces/2);
}while(is_set[this.piece_matches]==1);
is_set[this.piece_matches]=1;
_root["square"+this.piece_matches].matched_to=i;
_root["square"+i].matched_to=this.piece_matches;
_root["square"+this.piece_matches].display=i;
_root["square"+i].display=i;
}
}

function GamePiece(){
}
GamePiece.prototype=new MovieClip();
//for every 2 clicks, check to see if a match is found
GamePiece.prototype.onRelease=function(){
if (clicked=="A"){
clicked=this.piece_num;
this.gotoAndStop(2);
}else{
if (clicked!=this.matched_to){
this.gotoAndPlay(2);
_root["square"+clicked].gotoAndPlay(2);
clicked="A";
misses++;
}else{
matches++;
this.matched="yes";
this.gotoAndPlay(2);
_root["square"+this.matched_to].gotoAndPlay(2);
_root["square"+this.matched_to].matched="yes";
clicked="A";
}
if (matches==num_pieces/2){
playerFeedback_mc.gotoAndStop(2);
}
}
}
Object.registerClass("square", GamePiece);

function Frame(){
}
Frame.prototype.onLoad=function(){
_global.num_pieces=16;
_global.char_name="chia";
_global.clicked="A";
_global.depth=100;
var game_table=new GameTable();
game_table.createTable();
game_table.setPattern();
}
Object.registerClass("frame", Frame);
attachMovie("frame", "stageFrame_mc", 1, {_x:250, _y:250});
stop();

dorque
08-29-2002, 05:52 PM
a simple but ugly solution might be to figure out what the next highest square is (not necessarily an even square in this case), and add empty movies unrelated to the outcome ... so that the picture would start with some chunks already showing:

num_rows=Math.ceil( Math.sqrt(num_pieces) );

if you can somehow toss those empty movieclips into the mix so that they get randomly shuffled around that wouldn't be too bad. trying to organize them in a geometric fashion in the center might look nicer, but would be a royal pain in the arse.

b!

mbannonb
08-29-2002, 05:57 PM
good idea