A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: hitTests and getBounds... can't grasp it...

  1. #1
    Angkor-What? Gekke_Hollander's Avatar
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    234

    hitTests and getBounds... can't grasp it...

    Most of you probably have seen the code below. I just can't figure out what it does exactly... and that makes it kinda hard to implement...

    Code:
    if (walls.hitTest(getBounds(_root).xMax, _y, true)) {
    trace('hit');
    }
    if (walls.hitTest(getBounds(_root).xMin, _y, true)) {
    trace('hit');
    }
    if (walls.hitTest(_x, getBounds(_root).yMax, true)) {
    trace('hit');
    }
    if (walls.hitTest(_x, getBounds(_root).yMin, true)) {
    trace('hit');
    }
    Now what I understand is that it does a hitTest of something on a mc called walls. So if the something hits walls you'll get the alert 'hit'. I believe the _x and _y values are the ones of the something.... and true is set so that it tests on shape in stead of boundry, right? But what does: getBounds(_root).yMax and so on do? Please explain...

    Cheers
    MX 2004 Pro - Oops I did it again!!!
    SWF, it's a journey... not a destination

  2. #2
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    getBounds returns properties that are the minimum and maximum and coordinate values of the instance specified by MovieClip for the targetCoordinateSpace parameter.

    Sounds confusing huh?...... straight out of the AS help file.

    Anyway,......basically, it returns the property values of the movieclips coordinate space. This means that whereever the movieclip is on stage, it occupies a certain amount of coordinate space. This can be determined by the right edge of the clip(xMax) subtacted from the left edge of the clip(xMin). Same thing for height or yMax and yMin. To see an example of this, create a movieclip on stage named "clip"......

    code:

    //assign an identifier to the clip so
    //you can trace individual properties
    clipBounds = clip.getBounds(_root);
    //trace the horizontal coordinate space
    trace(clipBounds.xMax-clipBounds.xMin);
    //demo trace to show clip property matches clipBounds
    trace(clip._width);



    What is the advantage of this method? You can get the bounds of oddly shaped objects with the coordinate space they occupy. The following code tests the boundary of an oddly shaped lake movieclip and stops the boat motion if a hit is detected......

    code:

    onClipEvent (enterFrame) {
    with (_root.boat) {
    if (_root.map.hitTest(getBounds(_root).xMax, _y, true)) {
    _x -= 5;
    Speed = 0;
    }
    if (_root.map.hitTest(getBounds(_root).xMin, _y, true)) {
    _x += 5;
    Speed = 0;
    }
    if (_root.map.hitTest(_x, getBounds(_root).yMax, true)) {
    _y -= 5;
    Speed = 0;
    _root.character._y -= 5;
    }
    if (_root.map.hitTest(_x, getBounds(_root).yMin, true)) {
    _y += 5;
    }
    }
    }




    boat bounds demo
    http://ntdesigns.net/quickDemo/boatBounds.swf

  3. #3
    Angkor-What? Gekke_Hollander's Avatar
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    234
    Thanx for that... It was more or less what I thought...

    Found a good tutorial here as well:
    http://www.newgrounds.com/bbs/topic.php?id=293660

    The problem was, which resulted in me thinking I didn't get the whole concept, was that I was doing a hittest between a parent and child mc... It's getting late here! 4.47am... need to go to bed... it'll come to me tomorrow!

    Thanx again, and happy holidays!

    Cheers
    MX 2004 Pro - Oops I did it again!!!
    SWF, it's a journey... not a destination

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