Search code examples
imageactionscript-3flashdevelopbitmapdata

Apply some picture to the circle graphic in the as3


So, I already have the circle in my screen and showed it up, but I wanted to change the graphic from the "default circle", which is created by using the code like so:

circle.graphic.BeginFill();
circle.graphic.DrawCircle(10,10,10);
circle.graphic.EndFill();
addChild(circle);

I wanted to change that to my desired image like so:

enter image description here

How do I do that?


Solution

  • Use BitmapData and beginBitmapFill with circle like so:

    var myBitmap:BitmapData;
    
    var imgLoader:Loader = new Loader();
    imgLoader.load(new URLRequest("myImage.png"));
    imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, drawImage);
    
    
    function drawImage(e:Event):void
    {
        myBitmap = new BitmapData(imgLoader.width, imgLoader.height, false);
        myBitmap.draw(imgLoader);
    
        var circle:Sprite = new Sprite();
        circle.graphics.beginBitmapFill(myBitmap, null, true);
        circle.graphics.drawCircle(50,50,100);
        circle.graphics.endFill();
        addChild(circle);
    }
    

    For more info see beginBitmapFill()