A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [AS2][HELP] getPixel() - Not getting pixel

  1. #1
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550

    [AS2][HELP] getPixel() - Not getting pixel

    Explanation:

    I was toying with the idea of using getPixel to refine my hitTests between the characters of my game and the ground; using different colors to garner different reactions from certain MC's. So I started playing around with it, and drawing vectors as bitmaps, but I've now hit a snag.

    I have this script located on ROOT:

    [g being the "ground" movieclip]

    code:
    import flash.display.BitmapData;
    var gBit:BitmapData = new BitmapData(_root.g._width,_root.g._height);
    gBit.draw(_root.g);



    And this script located on a section of the heros script:

    [i being a loop I'm using for one of the heros functions]

    code:
    _root.gPixelX = _root.hero._x-_root.g._x;
    _root.gPixelY = (_root.hero._y+i)-_root.g._y;
    trace("heros X pixel = "+_root.gPixelX);
    trace("heros Y pixel = "+_root.gPixelY);
    _root.gPixel = _root.gBit.getPixel(gPixelX,gPixelY).toString(16);
    trace("Pixel Color = "+_root.gPixel);



    I assumed this would work, but for some reason I cannot even trace my original bitmapData, "gBit", and it just shows up as [object Object] in a trace.


    Question:

    What am I doing wrong here? This is new territory for me and I don't know much about it, so any help is very much appreciated.
    Skribble

    Its not that im lazy, I just dont care.

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    What am I doing wrong here? ...
    overuse of _root,- seriously just write the variable as it is - no need to write each time _root.

    here is imo. a better structured way:
    PHP Code:
    //_root code part
    import flash.display.BitmapData;
    var 
    gBit:BitmapData = new BitmapData(g._width,g._height);
    gBit.draw(g);

    function 
    getMyPixel(x,y){
        return 
    gBit.getPixel(x-g._x,y-g._y).toString(16);
    }

    //movieClip onClipEvent code part...
    for (i...){
        var 
    color _root.getMyPixel(hero._x hero._y+);
        
    trace("color: "+color);


    I assumed this would work, but for some reason I cannot even trace my original bitmapData, "gBit", and it just shows up as [object Object] in a trace.
    of course it does,- gBit is a bitmapData Object and not a number or string. So if you trace it by default it will tell you that it is a complex object (thats why you see the [object Object]). You have to access at least a property of that Object like width or height for example.
    In that case a trace would look like this:
    trace("my bitmap width: "+gBit.width);
    //outputs... my bitmap width: 472
    where 472 is the width of your "g" movieClip
    Last edited by renderhjs; 02-01-2009 at 10:46 AM.

  3. #3
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Do you get anything traced for _root.gPixel? I also think since you use draw without tranformation matrix, you should not reduce coordinates of g:
    _root.gPixelX = _root.hero._x;
    _root.gPixelY = (_root.hero._y+i);
    And you could turn your gBit temporarily visible with attachBitmap to see what is actually being drawn there.

  4. #4
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Cheers for the help!

    I can't seem to get it to work properly though, it's like the gBit.draw(g) is drawing something else, not my ground.

    It only works around the first 300px of the level(white square), and only returns ffffff which is obviously not the color that is there, however if you stand on the first step it will return the right color, where outside of the initial 300pxs (white square) it returns nothing but 0.

    A little confused :S



    P.S. I have some bad habits with coding, but I never usually do projects this big so I've never bothered to ween myself out of them. Sorry if it puts you off.
    Last edited by Skribble_Style; 02-01-2009 at 12:15 PM.
    Skribble

    Its not that im lazy, I just dont care.

  5. #5
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    are your movieClips scaled,- or rotated - which might change the transformation and the way the getPixel(x,y) should be used. ?

    maybe the bitmapData needs to be updated because you changed frames of the movieClip causing a completely different shape. So in that case you would have to rewrite
    PHP Code:
    gBit.draw(g); 

  6. #6
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    No sir, they haven't been transformed in any way. There is 1 frame in the g movieclip and it contains red vector paint as the ground; no multiple frames, or clips within clips.

    Tracing the X and Y right before returning the color, gives a correct reading and should return the red value, but it just doesn't...
    Skribble

    Its not that im lazy, I just dont care.

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    ffffff is default fill colour for non-transparent bitmapdata. So nothing was drawn to those pixels.

  8. #8
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    tonypa is right in that case make sure you use a 32 bit bitmapData (8bit red, 8bit green,8bit blue,8bit alpha) like
    PHP Code:
    var gBit:BitmapData = new BitmapData(g._width,g._height,true,0x00000000); 
    with a default color of 0% alpha and red,green,blue set to 0 thus black

  9. #9
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    D'oh! I can't seem to get it working, sorry.

    I used tonypa's advice and used attachBitmap (thanks btw) to see what was being drawn, and as it turns out it's drawing the ground perfectly. But for some reason it just isn't returning anything but 0, UNLESS like before, you sit on that first step, in which case it returns the correct color code.

    I immediately thought it was my code, so I went through and removed the dynamic i variable and used a straight x,y position(+2 to compensate for sprite position) and it still returned the same weird values.

    I decided to find out exactly where it would give me a color code and found that you are confined to a 30px x 350px square.



    This is very strange
    Skribble

    Its not that im lazy, I just dont care.

  10. #10
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    maybe don't convert it to a string, just do:

    _root.gBit.getPixel(gPixelX, gPixelY)

    always worked for me. Also, have you tried using a non-transparent bitmap, use black for open areas and white for solid areas.
    lather yourself up with soap - soap arcade

  11. #11
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Yeah, I've tried replacing my ground movieclip with a completely new mc with a smaller ground area, made it white and then tried, and it still wont display a color unless you are in that damn spot; anywhere else returns a 0.

    Also not converting with toString just makes the number different, it doesn't effect the actual process of getting it.
    Skribble

    Its not that im lazy, I just dont care.

  12. #12
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    terrainBitmap.getPixel(_x,_y); works fine for a game I'm currently doing. Wheres the registration point on the character?
    World Arcade | Facebook | Twitter - Follow MEEEE! or not......

  13. #13
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    Actually try the opposite, use setPixel to set the pixel its checking to a colour that stands out. That way you can tell where its looking. That should give you a good indication of what the problem is. I had a similar problem originally. I didn't take the mc's height into consideration.
    World Arcade | Facebook | Twitter - Follow MEEEE! or not......

  14. #14
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Thanks for all of your help guys!!!

    I mean that, I never would have figured it out otherwise.

    After over an hour of trying out a few different methods I went through every bit of code relating to getPixel that I have and found that I had put y instead of _y in one instance, and it was messing absolutely everything up.

    I've rectified the problem and it's working great!

    Again, many many kudos from me to you, sirs.
    Skribble

    Its not that im lazy, I just dont care.

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