dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: getPixel [help]

  1. #1

    getPixel [help]

    hey, I need to get some color info for where a user clicks on the screen.
    can i use getPixel with an imported image file ? or does it need to be converted to a bitmap? Can someone give me some info on what/how im supposed do that ?

    here is the code im using to import my image
    PHP Code:
    function loaderComplete(myEvent:Event)
    {
      var 
    myQueryStrings=this.loaderInfo.parameters;
      
    //Live URL
      
    loader.load(new URLRequest(myQueryStrings.ImageFile));

    }

    this.loaderInfo.addEventListener(Event.COMPLETE,loaderComplete);

    var 
    loader:Loader = new Loader();
    addChild(loader); 
    FuzzGtp

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You'll need to draw your loader (or loader's content) into a bitmapdata, then use that bitmapdata's getPixel method.

  3. #3
    Quote Originally Posted by 5TonsOfFlax View Post
    You'll need to draw your loader (or loader's content) into a bitmapdata, then use that bitmapdata's getPixel method.
    kinda what i thought, but no idea what im doing right/wrong here. I tried calling on pixeldat2.getPixel and it errors out.

    TypeError: Error #1009: Cannot access a property or method of a null object reference. at js_interface_fla::MainTimeline/mouseClickListener()

    If i try calling on pixeldat.getPixel it comes up with the same number each time : PIXEL DATA: 16777215

    Code:
    function loaderComplete(myEvent:Event)
    {
      var myQueryStrings=this.loaderInfo.parameters;
      //TEST URL
      pixeldat2 = BitmapData(loader.load(new URLRequest("http://www.fuzzthed.com/applications/uploads/adomsshot.jpg")));
      
    }
    import flash.display.BitmapData;
    this.loaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
    
    var loader:Loader = new Loader();
    addChild(loader);
    stop()
    
    var stopping_point = 0;
    var old_x = 0;
    var old_y = 0;
    var pixeldata = 0;
    var pixeldat:BitmapData = new BitmapData(621,466);
    var pixeldat2;
    											  
    addEventListener(MouseEvent.CLICK, mouseClickListener);
    
    function mouseClickListener(event:MouseEvent):void
    {   
        stopping_point = stopping_point +5;
    	old_x = stage.mouseX;
    	old_y = stage.mouseY;
    	trace("PIXEL DATA: " + pixeldat.getPixel(stage.mouseX, stage.mouseY))
        
    	gotoAndStop(stopping_point)
    	  
    }
    FuzzGtp

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The way you're trying to instantiate pixeldat2 makes absolutely no sense, and shouldn't even compile. You also never actually call load to trigger loaderComplete.
    Code:
    import flash.display.BitmapData;
    
    var stopping_point:int = 0;
    var old_x:Number = 0;
    var old_y:Number = 0;
    var pixeldata:uint = 0;
    var pixeldat2:BitmapData;
    
    function loaderComplete(myEvent:Event)
    {
      pixeldat2 = new BitmapData(loader.width, loader.height);
      pixeldat2.draw(loader);
      addEventListener(MouseEvent.CLICK, mouseClickListener);
    }
    
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
    addChild(loader);
    loader.load(new URLRequest("http://www.fuzzthed.com/applications/uploads/adomsshot.jpg"))
    stop();
    											  
    function mouseClickListener(event:MouseEvent):void
    {   
        stopping_point = stopping_point +5;
        old_x = stage.mouseX;
        old_y = stage.mouseY;
        trace("PIXEL DATA: " + pixeldat2.getPixel(stage.mouseX, stage.mouseY))
        gotoAndStop(stopping_point)
    }

  5. #5
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    The BitmapData constructor can't take a Display Object (i.e. a Loader) as an argument:

    BitmapData(width:int, height:int, transparent:Boolean = true, fillColor:uint = 0xFFFFFFFF)

    Even if it did, you still have to use the "new" keyword to create the BitmapData object anyway.

    Instead, you have to draw the Loader to your BitmapData, and then use getPixel().

    Also, I think you'll want to use contentLoaderInfo rather than loaderInfo, since loaderInfo is more generic and is a property of all DisplayObjects, not just Loaders (I don't think you'll get the right events if you use loaderInfo).

    Anyway, try this, it works when I do it:

    PHP Code:
    import flash.display.BitmapData;
    stop();

    function 
    loaderComplete(myEvent:Event) {
        var 
    myQueryStrings=this.loaderInfo.parameters;
        
    pixeldat2.draw(loader);
    }

    var 
    loader:Loader = new Loader();
    addChild(loader);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
    loader.load(new URLRequest("http://www.fuzzthed.com/applications/uploads/adomsshot.jpg"));

    var 
    stopping_point:Number 0;
    var 
    old_x:Number 0;
    var 
    old_y:Number 0;
    var 
    pixeldata:int 0;
    var 
    pixeldat:BitmapData = new BitmapData(621,466);
    var 
    pixeldat2:BitmapData = new BitmapData(621,466);

    stage.addEventListener(MouseEvent.CLICKmouseClickListener);

    function 
    mouseClickListener(event:MouseEvent):void {
        
    stopping_point stopping_point +5;
        
    old_x stage.mouseX;
        
    old_y stage.mouseY;
        
    trace("PIXEL DATA: 0x" pixeldat2.getPixel(stage.mouseXstage.mouseY).toString(16));
        
    gotoAndStop(stopping_point);

    Edit: I guess 5tons beat me to it; it probably makes more sense to do "pixeldat2 = new BitmapData(loader.width, loader.height);" like that anyway.

  6. #6
    WohOoooooo zompo's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    138
    I'm to slow, 5TonsOfFlax works, this is something simular

    Code:
    		var loader:Loader = new Loader();
    		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
    		loader.load(new URLRequest("http://www.fuzzthed.com/applications/uploads/adomsshot.jpg"));
    		
    		function complete($e:Event):void
    		{
    			var loadedBitmap:Bitmap = $e.target.content as Bitmap;
    			this.addChild(loadedBitmap);
    			trace(loadedBitmap.bitmapData.getPixel(100,150));
    			trace(loadedBitmap.bitmapData.getPixel(100,161));
    			trace(loadedBitmap.bitmapData.getPixel(100,172));
    		}
    that traces

    1644563
    1644815
    1710608

    Edit: oh.. Schfifty Five also beat me to it :P
    Last edited by zompo; 07-23-2009 at 07:18 PM.


    Try out my game http://alpha.bombwar.com - Alphakey: fkaplha

  7. #7
    thanks a bunch guys. Been a couple years since I touched flash for anything, and as3 is really wierd ><
    FuzzGtp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center