我正在尝试执行以下操作: 我想加载一个外部图像(dest)并在舞台上显示它,我想加载另一个不可见的图像(src)。当我在舞台上出现的图像上按住鼠标左键时,将调用开始将src图像复制到dest的功能。实际上我想在隐藏另一个下面的图像上重现划痕效果。 这是我的代码[用于调试目的的mouse_move事件触发了copypixels函数]
package
{
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width='400', height='300', backgroundColor='#ffffff', frameRate='24')]
public class CopyDemo extends Sprite
{
private const BLADE_WIDTH:Number = 5;
private const BLADE_HEIGHT:Number = 5;
private var loadedSurpriseAssetContainer:Sprite;
private var loadedSurpriseBitmapData:BitmapData;
private var loadedFrontAssetContainer:Sprite;
private var loadedFrontBitmapData:BitmapData;
private var blade:Sprite;
private var xmouse:Number;
private var ymouse:Number;
public function CopyDemo()
{
loadSurpriseImage("pic1.jpg");
}
//-------- loadFrontImage ------------
private function loadFrontImage(url:String):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFrontLoaderComplete);
loader.load(new URLRequest(url));
}
//------- loadSurpriseImage -------------
private function loadSurpriseImage(url:String):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSurpriseLoaderComplete);
loader.load(new URLRequest(url));
}
private function onFrontLoaderComplete(event:Event):void
{
loadedFrontAssetContainer = new Sprite();
loadedFrontAssetContainer.addChild(LoaderInfo(event.target).content);
loadedFrontBitmapData = new BitmapData(loadedFrontAssetContainer.width, loadedFrontAssetContainer.height, true, 0x00000000);
loadedFrontBitmapData.draw(loadedFrontAssetContainer);
addChild(loadedFrontAssetContainer);
loadedFrontAssetContainer.addEventListener(MouseEvent.MOUSE_MOVE, startcopy);
}
private function onSurpriseLoaderComplete(event:Event):void
{
loadedSurpriseAssetContainer = new Sprite();
loadedSurpriseAssetContainer.addChild(LoaderInfo(event.target).content);
loadedSurpriseBitmapData = new BitmapData(loadedSurpriseAssetContainer.width,
loadedSurpriseAssetContainer.height,
true,
0x00000000);
loadedSurpriseBitmapData.draw(loadedSurpriseAssetContainer);
loadedSurpriseAssetContainer.addEventListener(MouseEvent.MOUSE_MOVE, saveCoords);
loadFrontImage("pic2.jpg");
}
private function saveCoords(event:MouseEvent):void
{
//
}
private function startcopy(event:MouseEvent):void
{
xmouse = mouseX;
ymouse = mouseY;
trace("x=" + xmouse + ", y=" + ymouse);
loadedFrontBitmapData.copyPixels(loadedSurpriseBitmapData,
new Rectangle(0,0,10,10),
new Point(0,0));
}
}
[SWF(width='400', height='300', backgroundColor='#ffffff', frameRate='24')]
public class CopyDemo extends Sprite
{
private const BLADE_WIDTH:Number = 5;
private const BLADE_HEIGHT:Number = 5;
private var loadedSurpriseAssetContainer:Sprite;
private var loadedSurpriseBitmapData:BitmapData;
private var loadedFrontAssetContainer:Sprite;
private var loadedFrontBitmapData:BitmapData;
private var blade:Sprite;
private var xmouse:Number;
private var ymouse:Number;
public function CopyDemo()
{
loadSurpriseImage("pic1.jpg");
}
//-------- loadFrontImage ------------
private function loadFrontImage(url:String):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFrontLoaderComplete);
loader.load(new URLRequest(url));
}
//------- loadSurpriseImage -------------
private function loadSurpriseImage(url:String):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSurpriseLoaderComplete);
loader.load(new URLRequest(url));
}
private function onFrontLoaderComplete(event:Event):void
{
loadedFrontAssetContainer = new Sprite();
loadedFrontAssetContainer.addChild(LoaderInfo(event.target).content);
loadedFrontBitmapData = new BitmapData(loadedFrontAssetContainer.width, loadedFrontAssetContainer.height, true, 0x00000000);
loadedFrontBitmapData.draw(loadedFrontAssetContainer);
addChild(loadedFrontAssetContainer);
loadedFrontAssetContainer.addEventListener(MouseEvent.MOUSE_MOVE, startcopy);
}
private function onSurpriseLoaderComplete(event:Event):void
{
loadedSurpriseAssetContainer = new Sprite();
loadedSurpriseAssetContainer.addChild(LoaderInfo(event.target).content);
loadedSurpriseBitmapData = new BitmapData(loadedSurpriseAssetContainer.width,
loadedSurpriseAssetContainer.height,
true,
0x00000000);
loadedSurpriseBitmapData.draw(loadedSurpriseAssetContainer);
loadedSurpriseAssetContainer.addEventListener(MouseEvent.MOUSE_MOVE, saveCoords);
loadFrontImage("pic2.jpg");
}
private function saveCoords(event:MouseEvent):void
{
//
}
private function startcopy(event:MouseEvent):void
{
xmouse = mouseX;
ymouse = mouseY;
trace("x=" + xmouse + ", y=" + ymouse);
loadedFrontBitmapData.copyPixels(loadedSurpriseBitmapData,
new Rectangle(0,0,10,10),
new Point(0,0));
}
}
虽然这两个图像被加载到内存中并且第一个图像显示在舞台上,但当mouse_move事件触发相应的处理程序时,副本不起作用。有什么想法吗?
答案 0 :(得分:1)
好的,我不确定这是否正是你想要做的,但我认为这至少会让你走上正确的道路来解决你的问题。复制到新版本的startCopy中,我将其粘贴到您的代码中。哦,还要导入flash.display。*;所以你可以访问Bitmap。
private function startcopy(event:MouseEvent):void
{
xmouse = mouseX;
ymouse = mouseY;
trace("x=" + xmouse + ", y=" + ymouse);
loadedFrontBitmapData.copyPixels(loadedSurpriseBitmapData,
new Rectangle(mouseX,mouseY,10,10),
new Point(mouseX,mouseY));
var newBitmap:Bitmap = new Bitmap(loadedFrontBitmapData);
newBitmap.x = 0;
newBitmap.y = 0;
addChild(newBitmap);
}
我所做的就是更改你传递给copyPixel函数的Rectangle和Point值,这样当你将鼠标移到前面的图像上时,你会看到背景图像出现;然后我使用你从copyPixel获得的更新数据创建了一个新的Bitmap对象,并将其放在舞台上。
希望有所帮助:)