Ponty
Ponty

Reputation: 645

Copy to Bitmap loaded in sprite from Loader

i am trying to do the following: I want to load an external image (dest) and display it on the stage, and i want to load another image (src) which will not be visible. When i hold the left mouse button on the image that appears on the stage, then a function that start copies the src image to the dest will be invoked. Actually i want to reproduce the scratch effect on an image that hides another underneath. here is my code [ the copypixels function is triggered on mouse_move event for debug purposes ]

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

}

Although the two images are loaded into memory and the first one is shown on the stage, when the mouse_move events triggers the corresponding handlers the copy does not work. Any ideas?

Upvotes: 0

Views: 1175

Answers (1)

debu
debu

Reputation: 910

Ok, I'm not sure if this is exactly what you're trying to do, but I think this will at least get you on the right path to sorting out your problem. Copy in the new version of startCopy I've pasted below, into your code. Oh, and also import flash.display.*; so you have access to 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);
}

All I've done is change the Rectangle and Point values you're passing into the copyPixel function, so that you'll see the background image appear as you move the mouse over the front image; then I've created a new Bitmap object using the updated data you're getting from copyPixel, and put that on the stage.

Hope it helps :)

Upvotes: 1

Related Questions