2011-08-29 58 views
-1

我正在開發Flash中的拼圖遊戲。我正在開發一款拼圖遊戲。給出的PuzzlePiece類的代碼如下。TypeError:在Actionscript 3中出現錯誤#1009

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); 

      } 
     } 
    } 

的puzzlepiece的movieclip具有兩層

Mask Layer - Two masks. One rectangular and one triangular in frame 1 and 2. 
Holder Layer - holder_mc 

我試圖設置使用PuzzlePiece類的代碼拼圖內的movieclip的尺寸。

但我得到這個錯誤。

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和作爲文件。

https://rapidshare.com/files/4248268633/Puzzle_OOP.zip

+0

你的口罩是否存在於第1幀? – Kodiak

+0

編號holder_mc和mask1_mc在frame1上。 mask2_mc在frame2上。 –

+0

在另一個論壇中發佈了相同的問題。這是發生在那裏的討論。 http://www.kirupa.com/forum/showthread.php?367987 - 從關聯文檔類父級設置嵌套剪輯的屬性 –

回答

1

這可能會爲您瞭解http://www.developria.com/2010/04/combining-the-timeline-with-oo.html正在進行的操作。 (簡而言之,您不能訪問在時間軸上聲明的對象,直到Flash播放器實際創建它們。)

請注意,我不會建議您使用幀腳本來執行比停止更復雜的任何操作),特別是如果你也打算使用文檔類。

0

感謝您的問題,您科迪亞克導致我的答案。我克服了這個問題。我給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); 
} 

寫了一個函數這樣我稱爲從FLA的功能與添加一行此之後的addChild

puzzlePiece.selectFrame(1); 

當我測試我沒有得到一個錯誤,但我沒有看到圖像加載爲位圖和puzzlePieces閃爍可能是由於反覆從1跳到2。

我如何選擇面膜框架1.

注: - 在實際情況下,我可以有任意數量的時間表面具,我應該能夠從FLA選擇它。