2009-02-19 26 views
0

我收到一個答案,這是非常有幫助的,所以我修改了代碼,使其工作,但是,我仍然在方1外在形象不加載爲位圖

//load libs 
import flash.net.*; 
import flash.geom.Matrix; 
import flash.display.*; 
import flash.events.*; 
import com.adobe.images.JPGEncoder; 

function myLoader() { 
    var loader:Loader = new Loader(); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sketch); 

    var request:URLRequest = new URLRequest("http://www.sergiorodriguez.org/images/2008_5_FXD_VividBlack.jpg"); 
    loader.load(request); 
} 

//action for mouse 
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor); 
Mouse.hide(); 

function moveCursor(event:MouseEvent):void{ 
    pencil.x = event.stageX; 
    pencil.y = event.stageY; 
} 


var canvas_mc; 
    canvas_mc = new MovieClip() 

addChildAt(canvas_mc,0); 
canvas_mc.swapDepths 

//draw area to sketch 
function sketch(e:Event):void{ 
    //load bitmap and draw it to memory 
    var myBitmapData; 
     myBitmapData = new BitmapData(500, 500); 
     myBitmapData.draw(e); 

    //define matrix 
    var matrix; 
     matrix = new flash.geom.Matrix(); 

    //start canvas 
    canvas_mc.graphics.beginBitmapFill(myBitmapData,matrix, true, true); 
    canvas_mc.graphics.drawRect(0, 0, 500, 500); 
    canvas_mc.graphics.endFill(); 

    //listening events 
    canvas_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing); 
    canvas_mc.addEventListener(MouseEvent.MOUSE_UP, stopDrawing); 
    canvas_mc.addEventListener(MouseEvent.MOUSE_MOVE, makeLine);  
}  

//mouse draws on press 
function startDrawing(event:MouseEvent):void{ 
    canvas_mc.graphics.lineStyle(1, 0, 1); 
    canvas_mc.graphics.moveTo(mouseX, mouseY); 
    canvas_mc.addEventListener(MouseEvent.MOUSE_MOVE, makeLine); 
} 

//mouse stops drawing on realese 
function stopDrawing(event:MouseEvent):void{ 
    canvas_mc.removeEventListener(MouseEvent.MOUSE_MOVE, makeLine); 
} 

//creates lines 
function makeLine(event:MouseEvent):void{ 
    canvas_mc.graphics.lineTo(mouseX, mouseY); 
} 

//call function 
myLoader() 

//start to save onto server 
var serverPath:String = ""; 
function createJPG(m:MovieClip, q:Number, fileName:String){ 
    var jpgSource:BitmapData = new BitmapData (m.width, m.height); 
    jpgSource.draw(m); 
    var jpgEncoder:JPGEncoder = new JPGEncoder(q); 
    var jpgStream:ByteArray = jpgEncoder.encode(jpgSource); 
    var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream"); 
    var jpgURLRequest:URLRequest = new URLRequest (serverPath+"jpg_encoder_download.php?name=" + fileName + ".jpg");  
    jpgURLRequest.requestHeaders.push(header);    
    jpgURLRequest.method = URLRequestMethod.POST;    
    jpgURLRequest.data = jpgStream; 

    var jpgURLLoader:URLLoader = new URLLoader(); 
    //jpgURLLoader.load(jpgURLRequest);  
    navigateToURL(jpgURLRequest, "_blank"); 
} 

save_btn.addEventListener(MouseEvent.CLICK, saveBtnPress); 
save_btn.addEventListener(MouseEvent.ROLL_OVER, saveBtnOver); 
save_btn.addEventListener(MouseEvent.ROLL_OUT, saveBtnOut); 

function saveBtnPress(e:Event):void{  
createJPG(canvas_mc, 90, "sketch"); 
} 

function saveBtnOver(e:Event):void{ 
    Mouse.show(); 
    pencil.visible = false; 
} 

function saveBtnOut(e:Event):void{ 
    Mouse.hide(); 
    pencil.visible = true; 
} 

回答

1

我我不知道問題是什麼,但我假設你正在試圖在myLoader()函數加載的圖像上繪製圖像,並且沒有看到圖像。在這種情況下,問題是您沒有將Loader實例作爲子項添加到主精靈。這裏的修復:

function myLoader() { 
    var loader:Loader = new Loader(); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sketch); 

    var request:URLRequest = new URLRequest("http://www.sergiorodriguez.org/images/2008_5_FXD_VividBlack.jpg"); 
    loader.load(request); 
    addChild(loader); 
} 

說明:裝載機終將cointain加載的圖像,但如果你想讓它顯示出來,你需要顯示的裝載機本身。

+0

感謝您的答案,這裏是我想要實現的: 我正在創建一個繪圖板,我想要在背景上加載圖像。我應該能夠在圖像的頂部繪製。 – 2009-02-19 15:48:27

0

您不能將事件繪製到位圖。您需要繪製的圖像加載

改變您的線路:

myBitmapData.draw(e); 

myBitmapData.draw(loader); 

我懷疑你的代碼有一堆的其他問題,我實在看不出你加BitmapData到任何位圖任何地方,所以你永遠不會看到什麼繪製。

此外,請嘗試在您的問題中更具體一些,如果我們不必閱讀整個代碼,則更容易迴應。

+0

感謝您的幫助,我工作過。 :) – 2009-02-20 15:23:30