我正在开发Flash中的拼图游戏。我正在为拼图开发一个类。 PuzzlePiece类的代码如下。
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);*/
}
}
}
}
这是主时间轴中的代码。
//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);
}
}
}
位图操作类
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);
}
}
}
拼图动画片夹有两层
Mask Layer - Two masks. One rectangular and one triangular in frame 1 and 2.
Holder Layer - holder_mc
我正在尝试使用PuzzlePiece类中的代码在魔片中设置动画片段的尺寸。
但是我收到了这个错误。
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;
这样访问它是不对的。 PuzzlePiece是拼图剪辑的导出。
暂时我很难将维度编码为60.我已经aloso上传了fla和文件。
答案 0 :(得分:1)
这可能会让您了解http://www.developria.com/2010/04/combining-the-timeline-with-oo.html发生的事情。 (简而言之,在Flash播放器实际创建它们之前,您无法访问在时间轴上声明的对象。)
请注意,我不建议您使用框架脚本执行比stop()更复杂的操作,特别是如果您还要使用文档类。
答案 1 :(得分:0)
感谢Kodiak提出的这个问题,你引导我找到答案。我克服了这个问题。我在PuzzlePiece中添加了以下内容。我看到mask2_mc在第2帧之前不存在。
this.gotoAndStop(1);
this.holder_mc.width = this.holder_mc.height = 60;
this.mask1_mc.width = 60;
this.mask1_mc.height = 60;
this.gotoAndStop(2);
this.mask2_mc.width = 60;
this.mask2_mc.height = 60
但我现在有一个新问题。
我需要在第1帧上选择遮罩。我在PuzzlePiece
中写了这样的函数public function selectFrame(frameNo:uint) {
this.gotoAndStop(frameNo);
}
我通过在addChild
之后添加一行来调用fla中的函数puzzlePiece.selectFrame(1);
当我测试时,我没有收到错误,但是我没有看到加载为位图的图像和puzzlePieces闪烁可能是由于反复从1跳到2。
如何在第1帧上选择遮罩。
注意: - 在实际情况下,我可以在时间轴中有任意数量的面具,我应该可以从fla中选择它。