Thursday, August 9, 2012

Creating a Pentomino game using AS3: Part 22

In today's part we create another dialog window that pops up when the user chooses to save the created level in the editor.

The new dialog window will have 3 options - play the level, save the level locally and cancel.

Go to your Flash project now and create a new MovieClip with class path "save_screen". Add 3 buttons inside, label them "Play level", "Save level locally" and "Cancel". Set their ids to "btn_play", "btn_save", "btn_cancel".

Go to pentomino_editor.as and find the doSave() function. Find the line where you call playLevel() method of Pentomino object, and instead of it add lines that add a save_screen object instance to the screen. Pass 4 values as parameters - Pentomino, mapGrid, shapes and alertClose. Set canDraw to false:

private function doSave(evt:MouseEvent):void {
if (checkSave()) {
var shapes:Array = [];
for (var i:int = 0; i < shapeButtons.length; i++) {
shapes.push(shapeButtons[i].count.value);
}
var saveScreen:MovieClip = new save_screen(Pentomino, mapGrid, shapes, alertClose);
addChild(saveScreen);
canDraw = false;
}
}

Now create a new script file save_screen.as. Declare 4 variables - Pentomino, currentGrid, currentShapes and closeHandler:

private static var Pentomino:MovieClip;
private var currentGrid:Array;
private var currentShapes:Array;
private var closeHandler:Function;

In the constructor we receive all the parameters and apply their values to the newly created variables. Add CLICK event listeners to all 3 buttons in the window:

public function save_screen(pentominoReference:MovieClip, getGrid:Array, getShapes:Array, onClose:Function) 
{
Pentomino = pentominoReference;
currentGrid = getGrid;
currentShapes = getShapes;
closeHandler = onClose;
btn_play.addEventListener(MouseEvent.CLICK, onPlay);
btn_save.addEventListener(MouseEvent.CLICK, onSave);
btn_cancel.addEventListener(MouseEvent.CLICK, onCancel);
}

Create a function removeEverything() that gets rid of the listeners and deletes the window:

private function removeEverything():void {
btn_play.removeEventListener(MouseEvent.CLICK, onPlay);
btn_save.removeEventListener(MouseEvent.CLICK, onSave);
btn_cancel.removeEventListener(MouseEvent.CLICK, onCancel);
this.parent.removeChild(this);
}

We're going to call removeEverything() in each of the event handler functions. In the "Play level" button event handler add a line that calls playLevel() method of Pentomino. In the handler of "Save level locally" button we call Pentomino's saveLevelLocal(). In "Cancel" event handler we call the closeHandler function that was passed as the parameter:

private function onPlay(evt:MouseEvent):void {
removeEverything();
Pentomino.playLevel(currentGrid, currentShapes);
}

private function onSave(evt:MouseEvent):void {
removeEverything();
Pentomino.saveLevelLocal(currentGrid, currentShapes);
}

private function onCancel(evt:MouseEvent):void {
closeHandler.call();
removeEverything();
}

Full save_screen.as:

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

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

public class save_screen extends MovieClip
{

private static var Pentomino:MovieClip;
private var currentGrid:Array;
private var currentShapes:Array;
private var closeHandler:Function;

public function save_screen(pentominoReference:MovieClip, getGrid:Array, getShapes:Array, onClose:Function) 
{
Pentomino = pentominoReference;
currentGrid = getGrid;
currentShapes = getShapes;
closeHandler = onClose;
btn_play.addEventListener(MouseEvent.CLICK, onPlay);
btn_save.addEventListener(MouseEvent.CLICK, onSave);
btn_cancel.addEventListener(MouseEvent.CLICK, onCancel);
}

private function onPlay(evt:MouseEvent):void {
removeEverything();
Pentomino.playLevel(currentGrid, currentShapes);
}

private function onSave(evt:MouseEvent):void {
removeEverything();
Pentomino.saveLevelLocal(currentGrid, currentShapes);
}

private function onCancel(evt:MouseEvent):void {
closeHandler.call();
removeEverything();
}

private function removeEverything():void {
btn_play.removeEventListener(MouseEvent.CLICK, onPlay);
btn_save.removeEventListener(MouseEvent.CLICK, onSave);
btn_cancel.removeEventListener(MouseEvent.CLICK, onCancel);
this.parent.removeChild(this);
}

}

}

We need to add a new saveLevelLocal() function in main.as. Let it trace a message for now:

public function saveLevelLocal(grid:Array, shapes:Array):void {
trace("Save level locally");
}

Full main.as code:

package  
{
import flash.display.MovieClip;

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

public class main extends MovieClip
{

public function main() 
{
}

public function playLevel(grid:Array, shapes:Array):void {
gotoAndStop(2);
game.playLevel(grid, shapes);
}

public function saveLevelLocal(grid:Array, shapes:Array):void {
trace("Save level locally");
}

}

}

Thanks for reading!

Here are the results so far:


No comments:

Post a Comment