Wednesday, August 8, 2012

Creating a Pentomino game using AS3: Part 21

In today's part we will add buttons to the winning screen that allow the player to replay the level or return to the main menu.

Go to the win_screen MovieClip in Flash and add 2 buttons to the window - give them ids of "btn_again" and "btn_back". Label them something like "Play again" and "Return to main menu".

When that's done, create a new as3 script file called win_screen.as.

Declare a private static variable "game" typed MovieClip. This will be a reference to the parent pentomino_game object. In the constructor receive a gameReference parameter typed MovieClip and apply this value to "game". Add click event listeners for btn_again and btn_back buttons.

private static var game:MovieClip;

public function win_screen(gameReference:MovieClip) 
{
game = gameReference;
btn_again.addEventListener(MouseEvent.CLICK, onAgain);
btn_back.addEventListener(MouseEvent.CLICK, onBack);
}

The onAgain() function calls resetLevel() function of "game", onBack simply tells the root to go to 1st frame. Both functions call removeEverything() in the end. The removeEverything() function removes the listeners and the object itself.

package  
{
import flash.display.MovieClip;
import flash.events.MouseEvent;

/**
 * Open-source pentomino game engine
 * @author Kirill Poletaev
 */

public class win_screen extends MovieClip
{

private static var game:MovieClip;

public function win_screen(gameReference:MovieClip) 
{
game = gameReference;
btn_again.addEventListener(MouseEvent.CLICK, onAgain);
btn_back.addEventListener(MouseEvent.CLICK, onBack);
}

private function onAgain(evt:MouseEvent):void {
game.resetLevel();
removeEverything();
}

private function onBack(evt:MouseEvent):void {
(root as MovieClip).gotoAndStop(1);
removeEverything();
}

private function removeEverything():void {
btn_again.removeEventListener(MouseEvent.CLICK, onAgain);
btn_back.removeEventListener(MouseEvent.CLICK, onBack);
this.parent.removeChild(this);
}

}

}

Now go to pentomino_game.as file and update the doReset() function. Make it call resetLevel() instead of executing all the current code. The resetLevel() is a new public function that contains all the previous code. I made it public so that I can call it from win_screen instances:

private function doReset(evt:MouseEvent):void {
resetLevel();
}

public function resetLevel():void {
var i:int;
var u:int;
var width:int = mapGrid[0].length;
var height:int = mapGrid.length;

// change values in array
for (i = 0; i < height; i++) {
for (u = 0; u < width; u++) {
if (mapGrid[i][u] == 2) mapGrid[i][u] = 1;
}
}

// delete each shape
for (i = placedShapes.length-1; i >= 0; i--) {
selectedShape = placedShapes[i].currentFrame;
availableShapes[selectedShape-1]++;
shapeButtons[selectedShape-1].count.text = availableShapes[selectedShape-1];
placedShapes[i].parent.removeChild(placedShapes[i]);
placedShapes.pop();
}
placedShapes = [];
selectedShape = 0;
}

And that's it for today, thanks for reading and check out the results:


No comments:

Post a Comment