A Flash Developer Resource Site

Page 2 of 11 FirstFirst 123456 ... LastLast
Results 21 to 40 of 215

Thread: The elusive hitTest explained

  1. #21
    Member
    Join Date
    Oct 2002
    Location
    England, UK
    Posts
    62
    Hey, does this work with Flash 4? If not, is there an alternative?
    britishperson.co.uk

  2. #22
    SaphuA mosterdfles_flash's Avatar
    Join Date
    Jan 2002
    Location
    Tha Couch
    Posts
    935
    Originally posted by Ed Mack
    For this there are two fixes:

    Define the walls as sets of points (a bent line) and the balls movement as a vector. Then just test the vector against the line(s). This requires a good bit of algebra knowledge, but can be a better system in the long run for collisions (just find the gradient of the line) and mass hitTesting.

    OR

    Do a set of checks, ie. one for every 10px the ball will travel that frame.
    And... how should I do that ?
    'How Art Is The Visual While We’re Artificial…'

    Mail & MSN
    My Not Finisched Page!
    Nick: SaphuA

  3. #23
    Junior Member
    Join Date
    Nov 2002
    Posts
    13

    oops.

    Originally posted by Psytrix
    Could you explain that code a lil better, im not sure exactly how it works...?
    There seems to be a mistake....I used this link as an example. It is not my work. Sorry if I made it sound like it is. I am working on a game similar to this (creating my on custom sprites...zelda ones are too old) but the example I showed was used to illustrate what I'm trying to achieve: Map scrolling- when your character moves a certain distance and the screen begins to scroll.

    This is my script in the "hero" mc using ed mack's tutorials with some adjustments:

    onClipEvent(load){
    this.sendmessage("I need to get to the village square.");
    this.liftItem("map1");
    s = 5;
    b = this.getBounds(this);
    movespeed = 5
    function move(x,y){
    if(!_root.hyrossia.hitTest(_x+x+b.xmin+1,_y+y+b.ym in+1,true)){
    if(!_root.hyrossia.hitTest(_x+x+b.xmax-1,_y+y+b.ymin+1,true)){
    if(!_root.hyrossia.hitTest(_x+x+b.xmin+1,_y+y+b.ym ax-1,true)){
    if(!_root.hyrossia.hitTest(_x+x+b.xmax-1,_y+y+b.ymax-1,true)){
    _x += x;
    _y += y;
    }
    }
    }
    }
    }
    }

    onClipEvent(enterFrame){
    if(Key.isDown(Key.UP)){
    if(_root.hero._y<180){
    _root.hyrossia._y+=movespeed
    _root.road._y+=movespeed
    }else if{
    _root.hyrossia.hitTest
    }else{
    move(0,-s);
    }
    }
    if(Key.isDown(Key.DOWN)){
    if(_root.hero._y>220){
    _root.hyrossia._y-=movespeed
    _root.road._y-=movespeed
    }else{
    move(0,s);
    }
    }
    if(Key.isDown(Key.LEFT)){
    if(_root.hero._x<330){
    _root.hyrossia._x+=movespeed
    _root.road._x+=movespeed
    }else{
    move(-s,0);
    }
    }
    if(Key.isDown(Key.RIGHT)){
    if(_root.hero._x>330){
    _root.hyrossia._x-=movespeed
    _root.road._x-=movespeed
    }else{
    move(s,0);
    }
    }
    }

    Now, "hyrossia" is the map mc. I have the map scrolling the way I want it now....but the problem now is the collision detection is out of whack (if the map is scrolling while you hit a wall you begin to go through it). Any solutions for this?
    -NeotiK

  4. #24
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Originally posted by SMcClure34
    Hey, does this work with Flash 4? If not, is there an alternative?
    Not in flash 4, with that I think the only way to check collisions is to set up some variables representing the edges of the target, a left edge, right edge top and bottom, then check if another point falls within this area.
    That is, the x position of the point you test is greater than the leftedge variable and less than the rightedge variable. The y position of the point is greater than the topedge variable and less than the bottomedge variable.

  5. #25
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Well, there is "_droptarget", look at this:

    http://www.moock.org/webdesign/books...dragndrop.html

    It basically tells you what MC the movie hit when dropped last

  6. #26
    SaphuA mosterdfles_flash's Avatar
    Join Date
    Jan 2002
    Location
    Tha Couch
    Posts
    935

    Uuhhmm!

    Originally posted by Ed Mack
    For this there are two fixes:

    Define the walls as sets of points (a bent line) and the balls movement as a vector. Then just test the vector against the line(s). This requires a good bit of algebra knowledge, but can be a better system in the long run for collisions (just find the gradient of the line) and mass hitTesting.

    OR

    Do a set of checks, ie. one for every 10px the ball will travel that frame.
    Theres still no answer on the most importand question in the d*mn hitTest thing!!! It's just the question of the quesions!!!

    Plz help me for my dog's sake!!!
    'How Art Is The Visual While We’re Artificial…'

    Mail & MSN
    My Not Finisched Page!
    Nick: SaphuA

  7. #27
    Junior Member
    Join Date
    Nov 2000
    Posts
    17
    I have a question. Maybe this is simple. How about performing hitTest on multiple objects on multiple levels?
    Dionysys

  8. #28
    Actionscript Nerd
    Join Date
    Apr 2002
    Location
    USA
    Posts
    47
    mulitple objects just needs a test for each one. maybe a for loop with an array of your objects like:

    for (var i=0; i<numObjects;i++)
    if (this.hitTest(hitObject[i]){
    ...

    levels are easy too, just specify the level:

    if (this.hitTest(_level1.object))
    ••••••••••••••••••••••••••••••••••••••••••••••••••
    Try out the all new Ball Drop 2.0!
    http://www.mtsu.edu/~jwt2h/balldrop/

  9. #29
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    For the lines, having a solid knowledge of Algebra would be best... if I get time later I'll do an example of two lines and their gradients, then work out the intersection (through direct substitution).

  10. #30
    Junior Member
    Join Date
    Nov 2000
    Posts
    17
    Thanks for the insight. I have a good understanding of Algebra but I'm not sure about the hitTest. So far I have a gun:

    Code:
     
    if(_root.gun = "machine"){
    counter++;
    _root.bullet.duplicateMovieClip("bullet"+counter,counter);
    _root["bullet" + _level0.counter]._visible = true;
    _root["bullet" + _level0.counter]._x = this._x;
    _root["bullet" + _level0.counter]._y = this._y;
    _root.machineammo = _root.machineammo - 1;
    }
    so each bullet is incremented to a new level to prevent a new bullet from overwriting an old bullet. Is there a way around this? Then in my hit detection:

    Code:
    n = 1;
    if (n < 20){
    if (this.hitTest(eval("_root.person"+n))){
                if (_root.gun == "machine"){
                    eval("_root.person"+n).hitpoint = eval("_root.person"+n).hitpoint+1
                }
    }
    this.removeMovieClip();
    n++
    }
    Works fine for the first object I'm shooting but doesn't detect a collision for any object after the first. I've tried different ways of loading the objects into levels but I'm stumped on this one. How can I track the levels and make the hitTest detect them dynamically, i.e. I shoot person1 first and it takes damage but is not destroyed then I shoot person2 and it takes damage and both objects are still on stage but moving. Am I going in the right direction? Is there a better method for doing this in flash?

    I'm thinking something like:

    Code:
    if(eval("_root.bullet"+n).hitTest(eval("_root.person"+n)._x+n,eval("_root.person"+n)._y+n,true){
    //statement;
    }
    Any suggestions? My brain is about to run out of my ear.
    Last edited by Dionysys; 12-04-2002 at 06:32 PM.
    Dionysys

  11. #31
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Well, I would do it somthing like this:

    To add bullets:

    bc++; // bullet count
    _root.attachMovie("bullet","bullet"+bc,bc); // make sure you goto the bullet clip in the library, right click on it, goto linkage and set the linkage name as "bullet"
    bull = _root["bullet"+bc]; // save typing
    bull._x = _x; // we are here
    bull._y = _y;
    _root.machineammo--;

    For hitTesting:

    for(i in _root){
    if(i.substring(0, 2) == "per"){ // must be person clip
    if(this.hitTest(_root[i])){
    if(_root.gun == "machine"){
    _root[i].hitpoint++;
    }
    }
    }
    }

    Much the same as you...

    In the people clips, put enterframe loops (onclipevent kind), and just make them test if(hitpoint < 10){ //keep walking }else{ //splutter and die

    You have to place all the clips on different depths to keep everything working smoothly.

  12. #32
    SaphuA mosterdfles_flash's Avatar
    Join Date
    Jan 2002
    Location
    Tha Couch
    Posts
    935

    uhm

    What are levels used for anyway?
    'How Art Is The Visual While We’re Artificial…'

    Mail & MSN
    My Not Finisched Page!
    Nick: SaphuA

  13. #33
    Junior Member
    Join Date
    Nov 2000
    Posts
    17
    This is from the ActionScript dictionary.
    //start
    The initial movie loaded into an instance of the Flash Player is automatically loaded into _level0. The movie in _level0 sets the frame rate, background color, and frame size for all subsequently loaded movies. Movies are then stacked in higher-numbered levels above the movie in _level0.

    You must assign a level to each movie that you load into the Flash Player using the loadMovieNum action. You can assign levels in any order. If you assign a level that already contains a SWF file (including _level0) the movie at that level is unloaded and replaced by the new movie.
    //end

    Everything in Flash resides on a level. You can think of it as sheets of paper stacked on top of each other. Hope that helps.
    Dionysys

  14. #34
    aka Arcanimus
    Join Date
    Dec 2002
    Posts
    247

    if you don't understand this

    If you don't understand how to make solid objects (stuff that your character can't walk through) then do this: Go here: HERE . I guarantee that you won't be dissapointed. Look at the "lastish" post, there I explain it pretty well - I think.

  15. #35
    Senior Member
    Join Date
    May 2001
    Posts
    198

    I don't like hitTest() :)

    It's one of those pointless functions in Flash that need not exist.

    this is how I do collision detection:


    if((_x+_width/2>_root.object2._x-_root.object2._width/2 && _x-_width/2<_root.object2._x+_root.object2._width/2) && _root.object2.dead == false){
    _root.object2.dead = true
    }


    that way you don't have to bother with stupid things like drawing boxs for hitTest and crap.

    the only thing I've seen hitTest do that impressed me was "contour collision" in other words shapeflag.

    but still you don't really need that for 90% of the games you will create.
    Bring Back The Flash Footers Goddammit.

  16. #36
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    congratulations segavenger, you just rewrote hitTest... well a simplified version of it with no shapeflag and which only works left and right on clips with centered registration points (and which takes longer to type out when used in your code).

    yeah hitTest is like one of those other pointless functions, Math.cos. Id much rather use

    Math.sqrt((Math.pow(b,2)+Math.pow(a,2)-Math.pow(a,2))/2*b*c)

  17. #37
    Junior Member
    Join Date
    Jun 2002
    Posts
    21
    I dont have Flash MX, so please dont punch me if this has already been fixed. i hope that macromedia can make the .hitTest() only work for the actual shape, not the bounding area(was that right?). another thing i noticed was that squaring a negative number produced a negative number. thats wrong. -2 squared is 4. another useful thing would be the inverse of sin, cos and tan, so that i can find the angel between two lines. if there is a formula for this already, can anyone tell me.

    thnx

  18. #38
    Senior Member
    Join Date
    May 2001
    Posts
    198
    well yeah that was just a basic explanation. I didn't add the Y collision detection. I guess the reason I like to use this form of hitTest() is because I don't have to bother with boxes and things...
    plus it makes me feel more like a 1337 coder
    Bring Back The Flash Footers Goddammit.

  19. #39
    Member
    Join Date
    Sep 2002
    Posts
    32
    I have three questions:

    1. How can I have a shield meter above the health meter, and have it so that once my shield meter reaches 0, the health starts to go down?

    2. How can I have enemies shoot lasers that hurt the player?

    3. How can I have enemies with hit points?
    Last edited by Master Toddy; 12-09-2002 at 05:48 PM.

  20. #40
    Senior Member g_M_e's Avatar
    Join Date
    Jun 2002
    Posts
    147

    hitTest for Pacman

    I'm having trouble with my pacman clone, see it here.

    My problem is that pacman and my ghost are getting caught up on the rounded corners. Anyone have any suggestions?

    Thanks in advance
    Last edited by g_M_e; 12-10-2002 at 01:15 PM.
    g_M_e

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