dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: I miss the old getBounds...

  1. #1

    I miss the old getBounds...

    Hello,

    I am trying to familiarize myself with the new actionscript.

    Specifically I was trying to use the getBounds like actionscript 2.0. Then looked into it and found that like many other things, its different.

    I've been looking for a way to detect walls. I don't know if any of you remember, but one of the older flash versions had a maze game in some sample files. It used the getBounds to determine xMin, xMax, yMin, and yMax, at certain values of x and y.

    Here is how I have my movie set up. I have a square MC named "guy". I have an MC named "walls". The walls MC has a long rectangle going across the bottom of the stage and another long rectangle about a 4th the size halfway up the stage. Think sidescrolling platformer, mario.

    Is there a way I can make this statement into a code. "What is the yMin of the MC walls(the top y value), on the MC guy's x value?"

    I've been trying to figure it out for a little. Looked into hitTest a little, but it bases it off a rectangle or the MC. If my walls MC has different "platforms" in it at different levels, hitTest will come up true unless "guy" is above the top y value of the entire walls MC.

    Hopefully some bright people out there can help me like I've been helped before. :P

    Thanks,
    ~Bill
    So many Ideas

    So little Time

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    getBounds is still there, it returns a Rectangle describing the bounding box relative to a particular target displayObject. I'm not familiar with the as2 getBounds. What did it provide that as3 does not?

    In order to find out the ymin of the walls at a particular x value, you'll have to be a little more clever, unfortunately. You could use getBounds to get an absolute minimum, then iterate with hitTestPoint at different y values while keeping x constant. Don't forget to set shapeflag to true.

  3. #3
    Thanks, that worked to a certain extent.

    I'll be trying to see if I can make that work, but does anyone else know a different way to do this?

    Thanks again!
    So many Ideas

    So little Time

  4. #4
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    It sounds like you are testing against the container object for the walls/platforms, which is why you are getting collision = true if your guy is anywhere below the highest point of all your walls. It would be easier if you tested for collision against each object within your wall container. Using getBounds should work fine if all your walls and platforms are rectangular and non-rotated. If they aren't then using hitTestPoint would work. If you want to go a step further, you could use a physics engine like box2d which effectively does the collision detection and the collision response for you.

  5. #5
    Hmmm. I'm not quite following you with the objects in my wall container. I have an MC named wall, and inside that MC are different rectangular boxes. Except they are just fills, not objects or anything.

    And the other idea I'm unsure about as well. Basically I was looking for a physics engine. I was just trying to steer towards creating one myself. Not too involved mind you. Just basic 2d platformer physics, like a mario or megaman game. Jumping from platform to platform.

    I started making one in AS2.0, but decided it would be cleaner and more organized to start over in AS3. I can't remember how many times I've searched all the code to find out where a specific set of coding was, lol.

    At any rate, if you might be able to give me a more descriptive explanation to the objects in the wall container method, that would be great! Oh, also, the way I was making it in AS2, all of the wall MCs were single rectangles with collision detection code on them. It worked ok, but it was way to messy. :/.

    Well, thanks again for everyone's help so far!
    ~Bill L.
    So many Ideas

    So little Time

  6. #6
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    I have an MC named wall, and inside that MC are different rectangular boxes. Except they are just fills, not objects or anything.
    Ok - I thought that might be the case. There probably is a way of doing collision detection on those fills by detecting the colour of pixels at a particular point, although I've not used that technique in Flash so I'm not sure how quick it is.

    To use hitTestPoint of getBounds, you will need to turn all of those fills into objects, so that they are children of your wallMC (which will be the container). If they are fills created in Flash, that should be quite easy - just right click them and select 'Convert to Symbol', and select sprite or movieclip. If they are fills in an image you created in a paint program, you would have to draw boxes in flash over them (you can always set their alpha to zero so they don't show up), and then covert them to symbols.

    Then in your code, you can iterate through each of the boxes in your wallMC container and check for collisino on each one ... something like this:

    Code:
                              
    //find out which box is touching your guy
    for(var n:uint = 0; n < wallMC.numChildren; n++){
        var obj:MovieClip = wallMC.getChildAt(n) as MovieClip;
        var collision:Boolean = obj.hitTestPoint(guy.X,guy.Y);
        if(collision == true){
            //guy is touching box
        }
    }
    What you do then, if you detect that your guy is touching one of the boxes, would be to prevent him from moving down any further.

    I agree that using a physics engine for this may be overkill - although if it were me I'd use box2d for this as it gives a lot of other possibilities, like throwing objects that bounce of walls and good stuff like that. It does have a bit of a learning curve though.

  7. #7
    Wow, ok.

    That actually helps alot! It makes sense. It wasn't the original way I was planning on doing it, but it seems to be a step in the right direction. And possibly easier, lol.

    I'll try and set that up. One more question though. :P It seems the more you help the more questions I have, but. If I were to make those boxes objects within my walls MC, should I give them instance names? Would it matter? and whether I do or I don't, does it automatically set them up as child instances in the walls MC? Like how you called them as "getChildAt()"

    Possibly automatically setting them from 0 to whatever? Also, that numChildren. is that something I should set up beforehand, or may that be an actual property of a container?

    Thanks for all the help!
    ~Bill L.
    So many Ideas

    So little Time

  8. #8
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    No, you don't need to give them instance names - just converting them to symbols will automatically make them children of the wallsMC movieclip, and wallsMC.numchildren and wallsMC.getChildAt() will work automatically.

    If you want to know which child (ie which wall in your case) gets assigned which index (eg getChildAt(0), or getChildAt(1), etc.), flash will assign them automatically depending on the layer they are on in your container movieclip. (At the left of the movieclip's timeline where you can add layers). The layer closest to the bottom will get index 0. This can be useful - I sometimes set up the levels of a game in this way, with each level on a separate layer of a container MC, and then call levelContainerMC.getChildAt(level-1) to get that level up and running. (The -1 because child indexes start at 0). The advantage of this is that if you have lots of levels and want to fiddle with the order, you can just drag the layers up and down in the container MC - which is much quicker than if you gave each level an instance name like level1, level2 - as you'd have to keep changing their names when you fiddled with the level order.

    If you happen to put all the walls on one single layer, then the order they were created will be the effective layer and hence the child index.

    Of course, you can still give instance names to your walls if you want - which would allow you to reference them by name in your code. Say if you wanted a wall which moves up or down - give that particular wall an instance name of 'movingWall', so that in your code you could say wallsMC.movingWall.y += 1. Again - only possible if the wall is a symbol.

    Hope that helps.
    Last edited by _Ric_; 04-16-2009 at 06:27 AM.

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