I am pretty new to actionscript3, and I am currently working on a sliding puzzle game. The game is like, you generate 8 boxes in a matrix, each of which has a number, you slide the boxes to arrange them in a certain order. Well I have finished most of the required features, but there is one thing-if you press the ‘next’ button, the boxes cannot be moved any longer.
I’ll paste my codes here, including the main class and the box class.
Puzzlegame.as:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class PuzzleGame extends Sprite
{
public var whiteBox:Box;
public var boxes:Array=new Array();
public static const GAP:uint = 5;
public var timerText:TextField= new TextField();
public var timer:Timer;
public var stepCounter:TextField=new TextField();
public var steps:Number = 0;
public var endGame:TextField=new TextField();
public var endGameFormat:TextFormat=new TextFormat();
public function PuzzleGame()
{
var background:Background;
background= new Background();
addChild(background);
var nextbutton:nextButton;
nextbutton=new nextButton();
nextbutton.x = 100;
nextbutton.y = 350;
addChild(nextbutton);
buildMatrix();
stepCounter.x = 220;
stepCounter.y = 370;
stepCounter.text = "Steps: " + steps;
addChild(stepCounter);
timerText.border = true;
timerText.height = 20;
timerText.x = 10;
timerText.y = 10;
timerText.width = 150;
timerText.text = "start";
addChild(timerText);
nextbutton.addEventListener(MouseEvent.CLICK, nextMatrix);
stage.addEventListener(Event.ENTER_FRAME, checkAllBoxes);
}
public function nextMatrix(event:MouseEvent)
{
timer.stop();
boxes[0].removeEventListener(MouseEvent.CLICK, box0Clicked);
boxes[1].removeEventListener(MouseEvent.CLICK, box1Clicked);
boxes[2].removeEventListener(MouseEvent.CLICK, box2Clicked);
boxes[3].removeEventListener(MouseEvent.CLICK, box3Clicked);
boxes[4].removeEventListener(MouseEvent.CLICK, box4Clicked);
boxes[5].removeEventListener(MouseEvent.CLICK, box5Clicked);
boxes[6].removeEventListener(MouseEvent.CLICK, box6Clicked);
boxes[7].removeEventListener(MouseEvent.CLICK, box7Clicked);
buildMatrix();
steps = 0;
stepCounter.text = "Steps: " + steps;
}
public function buildMatrix()
{
whiteBox = new Box(0);
addChild(whiteBox);
whiteBox.x = 10 + Box.BOX_SIZE * 2 + GAP * 3;
whiteBox.y = 60 + Box.BOX_SIZE * 2 + GAP * 3;
var i:uint;
var rnd:Array = randomUnique.between(1,8);
var box:Box;
for (i=1; i<=8; i++)
{
if (i>=1&&i<=3)
{
box = new Box(rnd[i]);
box.x = 10 + (i - 1) * Box.BOX_SIZE + GAP * i;
box.y = 60 + GAP;
addChild(box);
boxes.push(box);
}
if (i>=4&&i<=6)
{
box = new Box(rnd[i]);
box.x = 10 + (i - 4) * Box.BOX_SIZE + GAP * (i - 3);
box.y = 60 + Box.BOX_SIZE + GAP * 2;
addChild(box);
boxes.push(box);
}
if (i>=7&&i<=8)
{
box = new Box(rnd[i]);
box.x = 10 + (i - 7) * Box.BOX_SIZE + GAP * (i - 6);
box.y = 60 + Box.BOX_SIZE * 2 + GAP * 3;
addChild(box);
boxes.push(box);
}
}
boxes[0].addEventListener(MouseEvent.CLICK, box0Clicked);
boxes[1].addEventListener(MouseEvent.CLICK, box1Clicked);
boxes[2].addEventListener(MouseEvent.CLICK, box2Clicked);
boxes[3].addEventListener(MouseEvent.CLICK, box3Clicked);
boxes[4].addEventListener(MouseEvent.CLICK, box4Clicked);
boxes[5].addEventListener(MouseEvent.CLICK, box5Clicked);
boxes[6].addEventListener(MouseEvent.CLICK, box6Clicked);
boxes[7].addEventListener(MouseEvent.CLICK, box7Clicked);
timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,onTimerTic);
timer.start();
}
public function addWhiteBox()
{
whiteBox = new Box(0);
addChild(whiteBox);
whiteBox.x = 10 + Box.BOX_SIZE * 2 + GAP * 3;
whiteBox.y = 60 + Box.BOX_SIZE * 2 + GAP * 3;
}
public function switchPlaces(box:Box,box2:Box)
{
var point:Point = new Point(box.x,box.y);
box.x = box2.x;
box.y = box2.y;
box2.x = point.x;
box2.y = point.y;
}
public function box0Clicked(event:MouseEvent)
{
if (((boxes[0].x==whiteBox.x && (Math.abs(boxes[0].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[0].y==whiteBox.y && (Math.abs(boxes[0].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[0],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box1Clicked(event:MouseEvent)
{
if (((boxes[1].x==whiteBox.x && (Math.abs(boxes[1].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[1].y==whiteBox.y && (Math.abs(boxes[1].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[1],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box2Clicked(event:MouseEvent)
{
if (((boxes[2].x==whiteBox.x && (Math.abs(boxes[2].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[2].y==whiteBox.y && (Math.abs(boxes[2].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[2],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box3Clicked(event:MouseEvent)
{
if (((boxes[3].x==whiteBox.x && (Math.abs(boxes[3].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[3].y==whiteBox.y && (Math.abs(boxes[3].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[3],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box4Clicked(event:MouseEvent)
{
if (((boxes[4].x==whiteBox.x && (Math.abs(boxes[4].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[4].y==whiteBox.y && (Math.abs(boxes[4].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[4],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box5Clicked(event:MouseEvent)
{
if (((boxes[5].x==whiteBox.x && (Math.abs(boxes[5].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[5].y==whiteBox.y && (Math.abs(boxes[5].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[5],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box6Clicked(event:MouseEvent)
{
if (((boxes[6].x==whiteBox.x && (Math.abs(boxes[6].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[6].y==whiteBox.y && (Math.abs(boxes[6].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[6],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function box7Clicked(event:MouseEvent)
{
if (((boxes[7].x==whiteBox.x && (Math.abs(boxes[7].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[7].y==whiteBox.y && (Math.abs(boxes[7].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
{
switchPlaces(boxes[7],whiteBox);
steps++;
stepCounter.text = "Steps: " + steps;
}
}
public function checkAllBoxes(event:Event)
{
if ((boxes[0].isInOriginalPos())&&(boxes[1].isInOriginalPos())&&(boxes[2].isInOriginalPos())&&(boxes[3].isInOriginalPos())&&(boxes[4].isInOriginalPos())&&(boxes[5].isInOriginalPos())&&(boxes[6].isInOriginalPos())&&(boxes[7].isInOriginalPos()))
{
endGame.height = 100;
endGame.width = 300;
endGame.x = 100;
endGame.y = 180;
endGame.text = "You win!";
endGameFormat.color = 0x000000;
endGameFormat.font = "Arial";
endGameFormat.size = 15;
endGameFormat.bold = true;
endGame.setTextFormat(endGameFormat);
addChild(endGame);
onGameEnd();
}
}
public function onGameEnd()
{
timer.stop();
timerText.text = "DONE >> " + numberToTime(timer.currentCount);
}
public function onTimerTic(event:TimerEvent):void
{
timerText.text = numberToTime(Number(Timer(event.target).currentCount));
}
public function numberToTime(n:uint):String
{
return String(n/100);
}
}
}
box.as:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.events.Event;
public class Box extends MovieClip
{
private var originalXPosition:Number;
private var originalYPosition:Number;
private var _color:uint;
private var _borderAlpha:Number;
private var _borderWidth:Number;
private var numText:TextField = new TextField ;
private var numFormat:TextFormat = new TextFormat ;
public static const GAP:uint = 5;
public static const BOX_SIZE:uint = 80;
public static const BOX_CLICKED:String = "clicked";
public function Box(boxNum:int)
{
var boxColor:uint;
var numColor:uint;
if ((boxNum == 0))
{
boxColor = 0xCCCCCC;
numColor = 0xCCCCCC;
originalXPosition = 10 + 2 * BOX_SIZE + 3 * GAP;
originalYPosition = 60 + 2 * BOX_SIZE + 3 * GAP;
}
if ((boxNum == 1))
{
boxColor = 0xFF9900;
numColor = 0xFFFFFF;
originalXPosition = 10 + GAP;
originalYPosition = 60 + GAP;
}
if ((boxNum == 2))
{
boxColor = 0xFFFFFF;
numColor = 0xFF9900;
originalXPosition = 10 + BOX_SIZE + 2 * GAP;
originalYPosition = 60 + GAP;
}
if ((boxNum == 3))
{
boxColor = 0xFF9900;
numColor = 0xFFFFFF;
originalXPosition = 10 + 2 * BOX_SIZE + 3 * GAP;
originalYPosition = 60 + GAP;
}
if ((boxNum == 4))
{
boxColor = 0xFFFFFF;
numColor = 0xFF9900;
originalXPosition = 10 + GAP;
originalYPosition = 60 + BOX_SIZE + 2 * GAP;
}
if ((boxNum == 5))
{
boxColor = 0xFF9900;
numColor = 0xFFFFFF;
originalXPosition = 10 + BOX_SIZE + 2 * GAP;
originalYPosition = 60 + BOX_SIZE + 2 * GAP;
}
if ((boxNum == 6))
{
boxColor = 0xFFFFFF;
numColor = 0xFF9900;
originalXPosition = 10 + 2 * BOX_SIZE + 3 * GAP;
originalYPosition = 60 + BOX_SIZE + 2 * GAP;
}
if ((boxNum == 7))
{
boxColor = 0xFF9900;
numColor = 0xFFFFFF;
originalXPosition = 10 + GAP;
originalYPosition = 60 + 2 * BOX_SIZE + 3 * GAP;
}
if ((boxNum == 8))
{
boxColor = 0xFFFFFF;
numColor = 0xFF9900;
originalXPosition = 10 + BOX_SIZE + 2 * GAP;
originalYPosition = 60 + 2 * BOX_SIZE + 3 * GAP;
}
this.graphics.lineStyle(0,boxColor);
this.graphics.beginFill(boxColor,1);
this.graphics.drawRect(0,0,BOX_SIZE,BOX_SIZE);
this.graphics.endFill();
numText.x = this.x;
numText.y = this.y;
numText.height = 40;
numText.width = 30;
numText.text = boxNum.toString();
numFormat.color = numColor;
numFormat.font = "Arial";
numFormat.size = 30;
numFormat.bold = true;
numText.setTextFormat(numFormat);
addChild(numText);
}
public function isInOriginalPos():Boolean
{
if (this.x == originalXPosition && this.y == originalYPosition)
{
return true;
}
return false;
}
}
}
Well the problem is, a new game is supposed to be started by clicking on the ‘next’ button, but when you do this, the boxes cannot be moved any longer.
It’s not 100% clear to me, but in your
nextMatrix()method, you are removing the event listeners. But you never remove the boxes from the stage (or whatever container they are being added to).Then in
buildMatrix()you add new boxes and add event listeners to them.What I believe is happening is that the original boxes remain, but there are no event listeners attached to them. Your
nextMatrix()function should have a loop in it that uses theremoveChild()method to remove the original boxes.Even if this doesn’t fix your current problem it will remove these objects from the stage and free up the memory they are consuming.
What I don’t understand is that generally when you do
addChild()it should add the object on top of whatever may have been there already. So it’s possible that something else is causing the problem. But as i mentioned, w/out doingremoveChild()on the old boxes, they are going to persist and consume more memory each time you start a new game.[Edit]
The other thing that should be happening is that you need to clear out the
boxesarray in yournextMatrix()method. Otherwise, the code in yourbuildMatrix()method will add the event listeners to the original boxes, not the new ones.Two quick ways to reset that array:
or