What is the best way of dealing with an "implicit coercion" of an array to a sprite?
Hello everyone!
With continued help from this forum I am getting closer to having a working program. I look forward to being able to help others like myself once I finish learning the AS3 ropes.
I will briefly explain what I am trying to achieve and then follow it up with my question.
Background
I have created a 12 x 9 random number grid that populates each cell with a corresponding image based on each cell's numeric value. I have also created a shuffle button that randomizes the numbers in the grid. The problem I am running into is getting my button-click event to clear the current images off the grid in order to assign new ones (i.e. deleting the display stack objects in order to place news ones in the same locations).
Question
My question is this: what is the best way to handle an implicit coercion from an array to a sprite? I have pasted my entire code below so that you can see how the functions are supposed to work together. My trouble apparently lies with not being able to use an array value with a sprite (the sprite represents the actual arrangement of the grid on the display stack while the array starts out as a number than gets assigned an image which should be passed to the sprite).
============================================================================
package
{
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getDefinitionByName;
public class Blanko extends MovieClip
{
// Holds 12*9 grid of cells.
var grid:Sprite;
// Holds the shuffle button.
var shuffleButton:Sprite;
// Equals 12 columns, 9 rows.
var cols:int = 12;
var rows:int = 9;
// Equals number of cells in grid (108).
var cells:int = cols * rows;
// Sets cell width and height to 40 pixels.
var cellW:int = 40;
var cellH:int = 40;
// Holds 108 cell images.
var imageArray:Array = [];
// Holds 108 numerical values for the cells in the grid.
var cellNumbers:Array = [];
// Constructor calls "generateGrid" and "makeShuffleButton" functions.
public function Blanko()
{
generateGrid();
makeShuffleButton();
}
// Creates and displays the 12*9 grid.
private function generateGrid():void
{
grid = new Sprite;
var i:int = 0;
for (i = 0; i < cells; i++)
{
cellNumbers.push(i % 9 + 1);
}
trace("Before shuffle: ", cellNumbers);
shuffleCells(cellNumbers);
trace("After shuffle: ", cellNumbers);
var _cell:Sprite;
for (i = 0; i < cells; i++)
{
// This next line is where the implicit coercion occurs. "_cell" is a sprite that tries
to temporarily equal an array value.
_cell = drawCells(cellNumbers);
_cell.x = (i % cols) * cellW;
_cell.y = (i / cols) * cellH;
grid.addChild(_cell);
}
}
// Creates a "shuffle" button and adds an on-click mouse event.
private function makeShuffleButton():void
{
var _label:TextField = new TextField();
_label.autoSize = "center";
TextField(_label).multiline = TextField(_label).wordWrap = false;
TextField(_label).defaultTextFormat = new TextFormat("Arial", 11, 0xFFFFFF, "bold");
_label.text = "SHUFFLE";
_label.x = 4;
_label.y = 2;
shuffleButton = new Sprite();
shuffleButton.graphics.beginFill(0x484848);
shuffleButton.graphics.drawRoundRect(0, 0, _label.width + _label.x * 2, _label.height +
_label.y * 2, 10);
shuffleButton.addChild(_label);
shuffleButton.buttonMode = shuffleButton.useHandCursor = true;
shuffleButton.mouseChildren = false;
shuffleButton.x = grid.x + 30 + grid.width - shuffleButton.width;
shuffleButton.y = grid.y + grid.height + 10;
this.addChild(shuffleButton);
shuffleButton.addEventListener(MouseEvent.CLICK, onShuffleButtonClick);
}
// Clears cell images, shuffles their numbers and then assigns them new images.
private function onShuffleButtonClick():void
{
eraseCells();
shuffleCells(cellNumbers);
trace("After shuffle: ", cellNumbers);
for (var i:int = 0; i < cells; i++)
{
drawCells(cellNumbers);
}
}
// Removes any existing cell images from the display stack.
private function eraseCells(): void
{
while (imageArray.numChildren > 0)
{
imageArray.removeChildAt(0);
}
}
// Shuffles cell numbers (randomizes array).
private function shuffleCells(_array:Array):void
{
var _number:int = 0;
var _a:int = 0;
var _b:int = 0;
var _rand:int = 0;
for (var i:int = _array.length - 1; i > 0; i--)
{
_rand = Math.random() * (i - 1);
_a = _array;
_b = _array[_rand];
_array = _b;
_array[_rand] = _a;
}
}
// Retrieves and assigns a custom image to a cell based on its numerical value.
private function drawCells(_numeral:int):Array
{
var _classRef:Class = Class(getDefinitionByName("skin" + _numeral));
_classRef.x = 30;
imageArray.push(_classRef);
imageArray.addChild(_classRef);
return imageArray;
}
}
}
===========================================================================
Any help with this is greatly appreciated. Thanks!