dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Detect width at specific y value

  1. #1

    Detect width at specific y value

    The title is basically what I am trying to do.

    I have a MovieClip that has a bunch of MovieClips inside it. I am trying to figure out how wide the MC is at a specific y value. Not sure if there is an easy way to do this.

    For an example.
    Code:
    OOOOOOOO
    O      O
    O      O
    OOOOOOOOOOOO
      O        O
      O        O
      OOOOOOOOOO
    This is the shape of the MC. It is not solid all the way through, it has MCs strewn about inside this outline. I am trying to figure out how wide it is from, say the second 'O' from the bottom. Left to right. The actual MC.width would be 12 'O's wide. But at that bottom portion, it would be 10 'O's wide. I'm not sure how I can do this.

    Anyone have an idea to throw out there?

    Thanks,
    ~Bill L.
    So many Ideas

    So little Time

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You could draw it to a bitmapdata using a 1px tall clipping rectangle, then use getColorBoundsRect() to figure out the area that isn't transparent.

  3. #3
    I'm not quite following you. How does this actually work?

    For something a little different, someone had suggested using a point to count the number of pixels were in a gap using hitTestPoint. I'm not sure if this is something like that or not, but I'd love to hear how that works if you have a way to explain it a little better.

    If you could that would be very helpful!
    Thanks, ~Bill L.
    So many Ideas

    So little Time

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Alright - had to dust off my matrix skills for this one - attached is a little proof of concept - here's what the working parts look like:

    PHP Code:
    //  create the 1px tall bitmap - this can be a class constant
    var BMD:BitmapData = new BitmapData(content.width1true);

    //  ...

    //  empty the last results out
    BMD.fillRect(BMD.rect0x00000000);

    //  flatten the content and extract the row at scanline.y
    BMD.draw(content, new Matrix(10010content.scanline.y));

    //  capture the dimensions of the filled part of the bitmap
    var bounds:Rectangle BMD.getColorBoundsRect(0xFF0000000x00000000false);

    trace(bounds.width "px wide"); 
    Attached Files Attached Files

  5. #5
    Hey, thanks alot for the help!

    I was having trouble opening the fla you sent. "unexpected file format". I'm using CS3 if it might be a compatibility issue.

    Also, I've been trying to get it to work with the code you posted here but I could only get it to work once. I'll attach the fla so you can see what I'm working with. It's a little messy, but I was basically trying to have it recognize how wide it is at the stages 200th pixel or so. Around the center.

    I have it tracing back the width whenever it changes floors, but I couldn't get it to work on the next floor up for some reason. And once you are up there, when you go back down it isn't right either.

    I'm pretty sure it has something to do with the "walls" clip's x value but not certain.

    Hopefully this will help. (Arrows to move, Space to Jump, Orange bars are Ladders)

    Thanks!,
    ~Bill L.
    Attached Files Attached Files
    So many Ideas

    So little Time

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Here's a cs3 version- I'll try and look into your code more later today. (It's amazing that this is running on a couple hundred lines of code!!)
    Attached Files Attached Files

  7. #7
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Ok - took a little digging but I found the source of the errors...you're using a set of Points to keep track of your floor positions and the third floor was never defined - so when you tried to go up, the level had no idea where the point of reference was.

    PHP Code:
    var floor3:Point = new Point(-420, -960); 
    Moving forward, I recommend you put those points into an array instead of seperate vars - that way you can keep it all together and look up the values like so:

    PHP Code:
    var floor:Array = [];
    floor[1] = new Point(0,0);
    floor[2] = new Point(-420, -480);

    //  ...

    //trace(MovieClip(root)["floor"+currentFloor]);
    tracefloor[currentFloor] ); 

    Also you might turn on bitmap caching on your 'walls' object - I think that will give you a noticeable performance boost when moving around.

  8. #8
    I'm not quite sure what you mean by turning on bitmap caching the walls. And I was having a problem in my file with the bitmaps themselves.

    I tried to trace the "width" that was being pulled from the bitmap thing, and the first floor worked fine telling me the correct width. But the second floor was coming up with a very low number.

    Is there any suggestions for that? Cause I'm pretty sure it has something to do with the walls object actually moving around the stage.

    Thanks alot!
    ~Bill
    So many Ideas

    So little Time

  9. #9
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Throw this into your initialization:

    PHP Code:
    walls.cacheAsBitmap true;
    guy.cacheAsBitmap true
    That lets Flash render it once and re-use the graphics instead of re-rendering every frame.

    For the scanline thing - you need to calculate the line independently of the stage - try this:

    PHP Code:
        BMD.draw(walls, new Matrix(1001, -walls.xwalls.y-scanline.y)); 
    Although I'm still not really sure why you need that width...seems like everything is workinf alright without it?

  10. #10
    Thanks alot!

    I'll put in those cache codes.

    Also, I decided to manually declare the widths along with the x and y of each floor so I just bypassed the whole width detection thing.

    Thanks again,
    ~Bill
    So many Ideas

    So little Time

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