I am developing a Jigsaw puzzle in Flash. I am developing a class for puzzle piece. The code of the PuzzlePiece class in given as follows.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class PuzzlePiece extends MovieClip
{
private var pieceX:Number;
private var pieceY:Number;
private var pieceXRandom:Number;
private var pieceYRandom:Number;
public function PuzzlePiece(pieceXRandom:Number,pieceYRandom:Number)
{
this.pieceXRandom = pieceXRandom;
this.pieceYRandom = pieceYRandom;
this.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
positionClips();
this.gotoAndStop(2);
this.holder_mc.width = this.holder_mc.height = 60;
this.mask1_mc.width = this.mask2_mc.width = 60;
this.mask1_mc.height = this.mask2_mc.height = 60;
}
private function positionClips():void
{
this.x = pieceXRandom;
this.y = pieceYRandom;
}
private function Drag(e:MouseEvent)
{
switch (e.type)
{
case 'mouseDown' :
this.startDrag();
this.addEventListener(MouseEvent.MOUSE_UP,Drag);
break;
case 'mouseUp' :
this.stopDrag();
this.removeEventListener(MouseEvent.MOUSE_UP,Drag);
/*var m:*=this.parent;
m.pos(this.x,this.y);*/
}
}
}
}
This is code in the main timeline.
//Global variables//
var imageDimension:Number = 360;
var gridType:Number = 6;
var puzzlePieceShape:String = "Sqaure";
var imageLoader:Loader = new Loader();
var bitmapArray:Array = [];
var puzzlePiece:PuzzlePiece;
var bitmapManip:BitmapManipulation;
loadImage();
function loadImage()
{
imageLoader.load(new URLRequest("Mohanlal.jpg"));//The image being loaded is of 360*360
imageHolder_mc.addChild(imageLoader);//imageHolder_mc is an empty MovieClip on stage
imageHolder_mc.visible = false;
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, layoutPieces);
}
function layoutPieces(evt:Event)
{
bitmapManip = new BitmapManipulation(imageDimension,gridType);
bitmapArray = bitmapManip.getBitmapImagePieces(imageHolder_mc);
for (var j:uint =0; j<bitmapArray.length; j++)
{
for (var k:uint=0; k<bitmapArray[j].length; k++)
{
var bitmap:Bitmap = new Bitmap(bitmapArray[j][k]);
puzzlePiece = new PuzzlePiece(400 * Math.random(),400 * Math.random());
addChild(puzzlePiece);
puzzlePiece.holder_mc.addChild(bitmap);
}
}
}
Bitmap Manipulation class
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
public class BitmapManipulation extends MovieClip
{
private var imageDimension:Number;
private var gridDimension:Number;
public function BitmapManipulation(imageDimension:Number,gridDimension:Number)
{
this.imageDimension = imageDimension;
this.gridDimension = gridDimension;
}
public function getBitmapImagePieces(imageMC:MovieClip):Array
{
var bitmapArray:Array = [];
var imageBitmapData:BitmapData = new BitmapData(imageMC.width,imageMC.height);
imageBitmapData.draw(imageMC);
var tileDimesion:Number = this.imageDimension / this.gridDimension;
for (var i:uint = 0; i<this.gridDimension; i++)
{
bitmapArray[i] = new Array();
for (var j:uint = 0; j<this.gridDimension; j++)
{
var tempData:BitmapData = new BitmapData(tileDimesion,tileDimesion);
var tempRect:Rectangle = new Rectangle(((tileDimesion) * i),((tileDimesion) * j),tileDimesion,tileDimesion);
tempData.copyPixels(imageBitmapData,tempRect,new Point(0,0));
bitmapArray[i][j] = tempData;
}
}
return(bitmapArray);
}
}
}
The puzzlepiece movieclip has two layers
Mask Layer - Two masks. One rectangular and one triangular in frame 1 and 2.
Holder Layer - holder_mc
I am trying to set the dimension of movieclips inside the puzzle piece using the code in PuzzlePiece class.
But I am getting this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PuzzlePiece()[C:\Users\Shabeeb\Desktop\Puzzle OOP\PuzzlePiece.as:26]
at PuzzlePiece_fla::MainTimeline/layoutPieces()[PuzzlePiece_fla.MainTimeline::frame1:33]
Line number 33 in main timeline class calls
this.holder_mc.width = this.holder_mc.height = 60;
this.mask1_mc.width = this.mask2_mc.width = 60;
this.mask1_mc.height = this.mask2_mc.height = 60;
Is it wrong to access it like that. The PuzzlePiece is the export for a puzzle clip.
For the time being I am hard coding the dimension as 60. I have aloso uploaded the fla and as files.
This may shed some insight for you on what is going on http://www.developria.com/2010/04/combining-the-timeline-with-oo.html . (In a nutshell, you can’t access objects that are declared on the timeline until the Flash player has actually created them.)
Note that I would not suggest that you use frame scripts that do anything more complicated than stop(), especially if you’re also going to use document classes.