将自定义Sprite保存为本地磁盘的映像

时间:2016-12-04 19:15:45

标签: actionscript-3 flash actionscript

我的Action Script 3代码存在问题。 我要让用户在spBoard中绘制他/她想要的任何东西然后我想让他/她能够将它保存在本地磁盘中。 这是代码,但是这段代码只能保存spBoard而不会被用户淹没。

import com.adobe.images.JPGEncoder;
import com.adobe.images.PNGEncoder;

 var lineSize: Number = 7;
 var currentColor: Number;

 var spBoard: Sprite = new Sprite();
 this.addChild(spBoard);
 spBoard.x = 280;
 spBoard.y = 23;
 drawBoard();

 var shDrawing: Shape = new Shape();
 this.addChild(shDrawing);
 shDrawing.x = 280;
 shDrawing.y = 23;

 var shMask: Shape = new Shape();
 this.addChild(shMask);
 shMask.x = 280;
 shMask.y = 23;
 drawMask();
 shDrawing.mask = shMask;
 var doDraw: Boolean = false;

 function drawBoard(): void {
 spBoard.graphics.lineStyle(1, 0x000000);
 spBoard.graphics.beginFill(0xFFFFFF);
 spBoard.graphics.drawRect(0, 0, 250, 250);
 spBoard.graphics.endFill();
 spBoard.filters = [new DropShadowFilter()];
 }

function drawMask(): void {
shMask.graphics.lineStyle(1, 0x000000);
shMask.graphics.beginFill(0xFFFFFF);
shMask.graphics.drawRect(1, 1, 249, 249);
shMask.graphics.endFill();
}


spBoard.addEventListener(MouseEvent.MOUSE_DOWN, boardDown);
spBoard.addEventListener(MouseEvent.MOUSE_UP, boardUp);

function boardDown(e: MouseEvent): void {
var curX: Number = shDrawing.mouseX;
var curY: Number = shDrawing.mouseY;
doDraw = true;
shDrawing.graphics.lineStyle(5, 0x000066);

shDrawing.graphics.lineStyle(lineSize, currentColor);
shDrawing.graphics.moveTo(curX, curY);
}

function boardUp(e: MouseEvent): void {
doDraw = false;
}

spBoard.addEventListener(MouseEvent.MOUSE_MOVE, boardMove);

function boardMove(e: MouseEvent): void {
var curX: Number = shDrawing.mouseX;
var curY: Number = shDrawing.mouseY;
if (doDraw) {
    shDrawing.graphics.lineTo(curX, curY);
}
e.updateAfterEvent();
}


save_image.addEventListener(MouseEvent.CLICK, save_image_function);
function save_image_function(event:MouseEvent):void
{
var bmd:BitmapData = new BitmapData(50,50);
bmd.draw(spBoard);
var encorder:PNGEncoder   = new PNGEncoder();
var bytes:ByteArray = PNGEncoder.encode( bmd );
var file:FileReference    = new FileReference();
file.save( bytes, "Image.png" );
}

1 个答案:

答案 0 :(得分:2)

您正在吸引shDrawing,然后保存spBoard !! spBoard不包含任何绘制的grapgic! 在保存之前,你必须将绘制图形复制到spBoard精灵中。
我还在保存功能中使用250x250大小的bitmapdata来覆盖整个绘图板。

function save_image_function(event:MouseEvent):void
{
    var _filters:Array = spBoard.filters;
    var defaultHolder:Sprite = new Sprite();
    defaultHolder.graphics.copyFrom(spBoard.graphics);
    spBoard.filters = [];
    spBoard.graphics.copyFrom(shDrawing.graphics);
    // above lines just for getting a back up of spBoard


    var bmd:BitmapData = new BitmapData(250, 250);
    bmd.draw(spBoard);
    var encorder:PNGEncoder   = new PNGEncoder();
    var bytes:ByteArray = PNGEncoder.encode( bmd );
    var file:FileReference    = new FileReference();
    file.save( bytes, "Image.png" );

    // now retrieve default spBoard to what that was before the save process
    spBoard.filters = _filters;
    spBoard.graphics.clear();
    spBoard.graphics.copyFrom(defaultHolder.graphics);
}