2013-02-04 19 views
0

我正在嘗試爲使用Flash CS6的iOS開發應用程序。我使用加載器導入了一個圖像。我現在希望能夠創建裝載機位圖數據的副本實例,並一直在努力:從加載器複製位圖數據

var my_loader:Loader = new Loader(); 
     my_loader.load(new URLRequest("cats.jpg")); 
     my_loader.scaleX = 0.2; 
     my_loader.scaleY = 0.2; 
     addChild(my_loader); 

     var duplicationBitmap:Bitmap = new Bitmap(Bitmap(my_loader.content).bitmapData); 
     duplicationBitmap.x = 300; 
     duplicationBitmap.y = 300; 
     addChild(duplicationBitmap); 

不幸的是,當我測試的代碼它不工作。我得到初始加載的圖像,但不是重複的,我也得到一個輸出錯誤:

TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 at Main()

任何想法將不勝感激。

回答

0

加載程序初始化時,您可以將加載程序繪製到BitmapData對象上,然後只需使用它在加載程序完成時創建儘可能多的對象。

import flash.display.Bitmap; 
import flash.display.BitmapData; 
import flash.display.Loader; 
import flash.events.Event; 

var loaderBitmapData:BitmapData; 

var loader:Loader = new Loader(); 
loader.contentLoaderInfo.addEventListener(Event.INIT, loaderInitEventHandler); 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler); 
loader.load(new URLRequest("eXO-01.png")); 

function loaderInitEventHandler(event:Event):void 
{ 
    loader.contentLoaderInfo.removeEventListener(Event.INIT, loaderInitEventHandler); 

    loaderBitmapData = new BitmapData(event.target.width, event.target.height); 
    loaderBitmapData.draw(event.target.loader as Loader); 
} 

function loaderCompleteEventHandler(event:Event):void 
{ 
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler); 

    createBitmaps(); 
} 

function createBitmaps():void 
{ 
    var image1:Bitmap = new Bitmap(loaderBitmapData); 
    image1.scaleX = image1.scaleY = 0.2; 

    var image2:Bitmap = new Bitmap(loaderBitmapData); 
    image2.scaleX = image2.scaleY = 0.4; 
    image2.x = image2.y = 100; 

    addChild(image1); 
    addChild(image2); 
} 
+0

謝謝,那工作得很好。 –

+0

不客氣。 – TheDarkIn1978

2

位圖(my_loader.content)是一個DisplayObject,不需要一個位圖,它給你一個空指針錯誤。 對於複製bitmapData,您應該使用BitmapData。 clone()。

+0

謝謝csomakk,我該如何使用它?我嘗試了幾種方法,但我扔了回來:1119:通過引用與靜態類型flash.display:Loader訪問可能未定義的屬性BitmapData。 –