Search code examples
actionscript-3drawingbitmapdata

as3 Drawing using BitmapData


I am extremely new to actionscript (or any code for that matter) and am having some trouble undertsanding why my code wont work. I am trying to create a small drawing application using BitmapData - there are no errors when I run the code but it doesn't do anything at all.

   import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.Sprite;
 import flash.display.Stage;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.text.TextField;
 import flash.text.TextFieldAutoSize;
 import flash.text.TextFormat;


 var canvas:BitmapData = new BitmapData(100, 100, false, 0x009900); 

 stage.addEventListener(MouseEvent.MOUSE_DOWN,draw)
 function draw(){ 

canvas.setPixel(mouseX, mouseY, 0x000000); 
} 

Hopefully this isn't too silly of a question - but if someone can guide me through why this wouldn't work or give me a working example (would be even better) I would be very grateful


Solution

  • You have to add the bitmapdata to the displayObject you are on (it should be a sprite)

    private var bitmap:Bitmap;
    
    //on your init function
    bitmap = new Bitmap(canvas);
    this.addchild(bitmap);
    
    stage.addEventListener(MouseEvent.MOUSE_DOWN,draw)
    
    function draw(e:MouseEvent)
    { 
        bitmap.bitmapdata.setPixel(e.localX, e.localY, 0x000000);
    }