Bejeweled AS3 engine with array rotation
I wanted to have a look at a Bejeweled game featuring the 90 degrees array rotation I explained yesterday.
So I took the script made by Brook Jordan in the complete Bejeweled game in less than 2KB – legible version and added some quick and dirty code to make it work:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.Event; import flash.text.TextField; public class Main extends Sprite { private var gems_array:Array=new Array(); private var aGem:Sprite; private var selectorBox:Sprite=new Sprite(); private var selectorRow:int=-10; private var selectorColumn:int=-10; private var red:uint = 0xFF0000; private var green:uint = 0xFF00; private var blue:uint = 0xFF; private var yellow:uint = 0xFFFF00; private var cyan:uint = 0xFFFF; private var magenta:uint = 0xFF00FF; private var white:uint = 0xFFFFFF; private var colours_array:Array=new Array(red,green,blue,yellow,cyan,magenta,white); private var clickPossible:Boolean=false; private var score_txt:TextField=new TextField(); private var hint_txt:TextField=new TextField(); private var score:uint=0; private var inaRow:uint=0; private var match:Boolean = true; private var rotate:Boolean=false; private var gemCanvas:Sprite=new Sprite(); public function Main() { // Game initiation // Create and style score text addChild(score_txt); score_txt.textColor=0xFFFFFF; score_txt.x=500; // Create and style hint text addChild(hint_txt); hint_txt.textColor=0xFFFFFF; hint_txt.x=550; // Create Gems in rows and columns addChild(gemCanvas); gemCanvas.x=240; gemCanvas.y=240; for (var i:uint=0; i<8; i++) { gems_array[i]=new Array(); for (var j:uint=0; j<8; j++) { do { gems_array[i][j]=Math.floor(Math.random()*7); } while (rowLineLength(i,j)>2 || columnLineLength(i,j)>2); aGem=new Sprite(); aGem.graphics.beginFill(colours_array[gems_array[i][j]]); aGem.graphics.drawCircle(30,30,29); aGem.graphics.endFill(); aGem.name=i+"_"+j; aGem.x=j*60-240; aGem.y=i*60-240; gemCanvas.addChild(aGem); } } // Create and style selector box addChild(selectorBox); selectorBox.graphics.lineStyle(2,red,1); selectorBox.graphics.drawRect(0,0,60,60); selectorBox.visible=false; // Listen for user input stage.addEventListener(MouseEvent.CLICK,onClick); addEventListener(Event.ENTER_FRAME,everyFrame); } // Every frame... private function everyFrame(e:Event):void { //Assume that gems are not falling var gemsAreFalling:Boolean=false; // Check each gem for space below it for (var i:int=6; i>=0; i--) { for (var j:uint=0; j<8; j++) { // If a spot contains a gem, and has an empty space below... if (gems_array[i][j] != -1 && gems_array[i+1][j]==-1) { // Set gems falling gemsAreFalling=true; gems_array[i+1][j]=gems_array[i][j]; gems_array[i][j]=-1; gemCanvas.getChildByName(i+"_"+j).y+=60; gemCanvas.getChildByName(i+"_"+j).name=(i+1)+"_"+j; break; } } // If a gem is falling if (gemsAreFalling) { // don't allow any more to start falling break; } } // If no gems are falling if (! gemsAreFalling) { // Assume no new gems are needed var needNewGem:Boolean=false; // but check all spaces... for (i=7; i>=0; i--) { for (j=0; j<8; j++) { // and if a spot is empty if (gems_array[i][j]==-1) { // now we know we need a new gem needNewGem=true; // pick a random color for the gem gems_array[0][j]=Math.floor(Math.random()*7); // create the gem aGem=new Sprite(); aGem.graphics.beginFill(colours_array[gems_array[0][j]]); aGem.graphics.drawCircle(30,30,29); aGem.graphics.endFill(); // ID it aGem.name="0_"+j; // position it aGem.x=j*60-240; aGem.y=-240; // show it gemCanvas.addChild(aGem); // stop creating new gems break; } } // if a new gem was created, stop checking if (needNewGem) { break; } } // If no new gems were needed... if (! needNewGem) { // assume no more/new lines are on the board var moreLinesAvailable:Boolean=false; // check all gems for (i=7; i>=0; i--) { for (j=0; j<8; j++) { // if a line is found if (rowLineLength(i,j)>2 || columnLineLength(i,j)>2) { // then we know more lines are available moreLinesAvailable=true; // creat a new array, set the gem type of the line, and where it is var lineGems:Array=[i+"_"+j]; var gemType:uint=gems_array[i][j]; var linePosition:int; // check t's a horizontal line... if (rowLineLength(i,j)>2) { // if so, find our how long it is and put all the line's gems into the array linePosition=j; while (sameGemIsHere(gemType,i,linePosition-1)) { linePosition--; lineGems.push(i+"_"+linePosition); } linePosition=j; while (sameGemIsHere(gemType,i,linePosition+1)) { linePosition++; lineGems.push(i+"_"+linePosition); } } // check t's a vertical line... if (columnLineLength(i,j)>2) { // if so, find our how long it is and put all the line's gems into the array linePosition=i; while (sameGemIsHere(gemType,linePosition-1,j)) { linePosition--; lineGems.push(linePosition+"_"+j); } linePosition=i; while (sameGemIsHere(gemType,linePosition+1,j)) { linePosition++; lineGems.push(linePosition+"_"+j); } } // for all gems in the line... for (i=0; i<lineGems.length; i++) { // remove it from the program gemCanvas.removeChild(gemCanvas.getChildByName(lineGems[i])); // find where it was in the array var cd:Array=lineGems[i].split("_"); // set it to an empty gem space gems_array[cd[0]][cd[1]]=-1; // set the new score score+=inaRow; // set the score setter up inaRow++; } // if a row was made, stop the loop break; } } // if a line was made, stop making more lines if (moreLinesAvailable) { break; } } // if no more lines were available... if (! moreLinesAvailable) { if(rotate){ gemCanvas.rotation+=5; if(gemCanvas.rotation%90==0){ rotate=false; gemCanvas.rotation=0; rotateClockwise(gems_array); while(gemCanvas.numChildren>0){ gemCanvas.removeChildAt(0); } for (i=0; i<8; i++) { for (j=0; j<8; j++) { aGem=new Sprite(); aGem.graphics.beginFill(colours_array[gems_array[i][j]]); aGem.graphics.drawCircle(30,30,29); aGem.graphics.endFill(); aGem.name=i+"_"+j; aGem.x=j*60-240; aGem.y=i*60-240; gemCanvas.addChild(aGem); } } } } else{ // allow new moves to be made clickPossible=true; // remove score multiplier inaRow=0; } } } } // display new score score_txt.text=score.toString(); } // When the user clicks private function onClick(e:MouseEvent):void { // If a click is allowed if (clickPossible) { // If the click is within the game area... if (mouseX<480&&mouseX>0&&mouseY<480&&mouseY>0) { // Find which row and column were clicked var clickedRow:uint=Math.floor(mouseY/60); var clickedColumn:uint=Math.floor(mouseX/60); // Check if the clicked gem is adjacent to the selector // If not... if (!(((clickedRow==selectorRow+1 || clickedRow==selectorRow-1)&&clickedColumn==selectorColumn)||((clickedColumn==selectorColumn+1 || clickedColumn==selectorColumn-1) && clickedRow==selectorRow))) { // Find row and colum the selector should move to selectorRow=clickedRow; selectorColumn=clickedColumn; // Move it to the chosen position selectorBox.x=60*selectorColumn; selectorBox.y=60*selectorRow; // If hidden, show it. selectorBox.visible=true; } // If it is not next to it... else { // Swap the gems; swapGems(selectorRow,selectorColumn,clickedRow,clickedColumn); // If they make a line... if (rowLineLength(selectorRow,selectorColumn)>2 || columnLineLength(selectorRow,selectorColumn)>2||rowLineLength(clickedRow,clickedColumn)>2 || columnLineLength(clickedRow,clickedColumn)>2) { // remove the hint text hint_txt.text=""; // dis-allow a new move until cascade has ended (removes glitches) clickPossible=false; // move and rename the gems gemCanvas.getChildByName(selectorRow+"_"+selectorColumn).x=clickedColumn*60-240; gemCanvas.getChildByName(selectorRow+"_"+selectorColumn).y=clickedRow*60-240; gemCanvas.getChildByName(selectorRow+"_"+selectorColumn).name="t"; gemCanvas.getChildByName(clickedRow+"_"+clickedColumn).x=selectorColumn*60-240; gemCanvas.getChildByName(clickedRow+"_"+clickedColumn).y=selectorRow*60-240; gemCanvas.getChildByName(clickedRow+"_"+clickedColumn).name=selectorRow+"_"+selectorColumn; gemCanvas.getChildByName("t").name=clickedRow+"_"+clickedColumn; match = true; rotate = true; } // If not... else { // Switch them back swapGems(selectorRow,selectorColumn,clickedRow,clickedColumn); match = false; } if (match) { // Move the selector position to default selectorRow=-10; selectorColumn=-10; // and hide it selectorBox.visible=false; } else { // Set the selector position selectorRow=clickedRow; selectorColumn=clickedColumn; // Move the box into position selectorBox.x=60*selectorColumn; selectorBox.y=60*selectorRow; match = false; // If hidden, show it. selectorBox.visible=true; } } } // If the click is outside the game area else { // For gems in all rows... for (var i:uint=0; i<8; i++) { // and columns... for (var j:uint=0; j<8; j++) { // if they're not too close to the side... if (i<7) { // swap them horizontally swapGems(i,j,i+1,j); // check if they form a line if ((rowLineLength(i,j)>2||columnLineLength(i,j)>2||rowLineLength(i+1,j)>2||columnLineLength(i+1,j)>2)) { // if so, name the move made selectorBox.x = j*60; selectorBox.y = i*60; selectorBox.visible = true; hint_txt.text = (i+1).toString()+","+(j+1).toString()+"->"+(i+2).toString()+","+(j+1).toString(); } // swap the gems back swapGems(i,j,i+1,j); } // then if they're not to close to the bottom... if (j<7) { // swap it vertically swapGems(i,j,i,j+1); // check if it forms a line if ((rowLineLength(i,j)>2||columnLineLength(i,j)>2||rowLineLength(i,j+1)>2||columnLineLength(i,j+1)>2) ) { // if so, name it selectorBox.x = j*60; selectorBox.y = i*60; selectorBox.visible = true; hint_txt.text = (i+1).toString()+","+(j+1).toString()+"->"+(i+1).toString()+","+(j+2).toString(); } // swap the gems back swapGems(i,j,i,j+1); } } } } } } //Swap given gems private function swapGems(fromRow:uint,fromColumn:uint,toRow:uint,toColumn:uint):void { //Save the original position var originalPosition:uint=gems_array[fromRow][fromColumn]; //Move original gem to new position gems_array[fromRow][fromColumn]=gems_array[toRow][toColumn]; //move second gem to saved, original gem's position gems_array[toRow][toColumn]=originalPosition; } //Find out if there us a horizontal line private function rowLineLength(row:uint,column:uint):uint { var gemType:uint=gems_array[row][column]; var lineLength:uint=1; var checkColumn:int=column; //check how far left it extends while (sameGemIsHere(gemType,row,checkColumn-1)) { checkColumn--; lineLength++; } checkColumn=column; //check how far right it extends while (sameGemIsHere(gemType,row,checkColumn+1)) { checkColumn++; lineLength++; } // return total line length return (lineLength); } //Find out if there us a vertical line private function columnLineLength(row:uint,column:uint):uint { var gemType:uint=gems_array[row][column]; var lineLength:uint=1; var checkRow:int=row; //check how low it extends while (sameGemIsHere(gemType,checkRow-1,column)) { checkRow--; lineLength++; } //check how high it extends checkRow=row; while (sameGemIsHere(gemType,checkRow+1,column)) { checkRow++; lineLength++; } // return total line length return (lineLength); } private function sameGemIsHere(gemType:uint,row:int,column:int):Boolean { //Check there are gems in the chosen row if (gems_array[row]==null) { return false; } //If there are, check if there is a gem in the chosen slot if (gems_array[row][column]==null) { return false; } //If there is, check if it's the same as the chosen gem type return gemType==gems_array[row][column]; } private function rotateClockwise(a:Array):void { var n:int=a.length; for (var i:int=0; i<n/2; i++) { for (var j:int=i; j<n-i-1; j++) { var tmp:String=a[i][j]; a[i][j]=a[n-j-1][i]; a[n-j-1][i]=a[n-i-1][n-j-1]; a[n-i-1][n-j-1]=a[j][n-i-1]; a[j][n-i-1]=tmp; } } } } } |
My code is written mainly at lines 197-218
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
gemCanvas.rotation+=5; if(gemCanvas.rotation%90==0){ rotate=false; gemCanvas.rotation=0; rotateClockwise(gems_array); while(gemCanvas.numChildren>0){ gemCanvas.removeChildAt(0); } for (i=0; i<8; i++) { for (j=0; j<8; j++) { aGem=new Sprite(); aGem.graphics.beginFill(colours_array[gems_array[i][j]]); aGem.graphics.drawCircle(30,30,29); aGem.graphics.endFill(); aGem.name=i+"_"+j; aGem.x=j*60-240; aGem.y=i*60-240; gemCanvas.addChild(aGem); } } } |
To save coding time, I only rotate the two-dimensional array, then remove all jewels and recreate them in their new position.
This is the result:
I wonder if it could be interesting to create a real game out of it. Any idea?
No need to download anything, replace the source code of the original prototype with the one on this page.
They can be easily customized to meet the unique requirements of your project.






(7 votes, average: 3.86 out of 5)








This post has 10 comments
baudaffi
Try to change the direction of the traspose and try to make the traspose of a minor whit a particolar bonus.
baudaffi
Ehm not traspose (my foult) rotation :P
junus
I follow your blog more than 2 years, this blog inspire me for many games. I want to say just “thank you” :)
Also, this game needs smooth collapse effect.
Jae
you color scheme is still terribly color blind hating.
alfonsofonso
what about a rotate button?
Anup
Nice tutorial
Billy Bateman
I was thinking about maybe doing a twister version of bejeweled. It would be something close to this, I like the concept of rotating the entire board, how about rotating rotating 4 gems to make a match, kinda what I was thinking. Love your site and love the demos.
Bejeweled AS3 engine with array rotation – Emanuele Feronato « eaflash
[...] on http://www.emanueleferonato.com Share this:TwitterFacebookLike this:LikeBe the first to like [...]
gssgames
Nice game but it never ends :)
saratogacoach
Brilliant tutorial!
Is there any way to center the gemCanvas (on a bigger,1024X768 stage) and have the selectorBox still work in the new location?