A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Same entitled movieclips hittesting each other?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    11

    Same entitled movieclips hittesting each other?

    Sort of a hard thing to explain, but i have a database that holds the enemies that are generated within the game.

    Code:
    for(var i = 0; i < meepsArray.length; i++)
    	{
    if(["meep"+i].hitTest(["meep"+i])){
        trace("success")
        }
    }
    This is the only way i can think of doing it. If meep(any number) hits another one, execute code. This, however, does not work. I don't see why because if i use a different array, say:
    Code:
    if(["blah"+i].hitTest(["meep"+i])){
        trace("success")
        }
    That seems to work. How can i make it so they can hittest each other? In the long wrong, i'm just going to use a distance formula to execute the code since hitTest isn't the most accurate thing in the world. But for testing purposes i've been using hittest.

    -Holsey

  2. #2
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    Hi.

    You wrote this code:

    for(var i = 0; i < meepsArray.length; i++)
    {
    if(["meep"+i].hitTest(["meep"+i])){
    trace("success")
    }
    }
    Let's assume the array length is 3. Then your code will do exactly this:

    if (meep0.hitTest(meep0)){trace('success');}
    if (meep1.hitTest(meep1)){trace('success');}
    if (meep2.hitTest(meep2)){trace('success');}
    if (meep3.hitTest(meep3)){trace('success');}

    But that is not what you want. What you want is this:

    if (meep0.hitTest(meep1)){trace('success');}
    if (meep0.hitTest(meep2)){trace('success');}
    if (meep0.hitTest(meep3)){trace('success');}

    if (meep1.hitTest(meep0)){trace('success');}
    if (meep1.hitTest(meep2)){trace('success');}
    if (meep1.hitTest(meep3)){trace('success');}

    if (meep2.hitTest(meep0)){trace('success');}
    if (meep2.hitTest(meep1)){trace('success');}
    if (meep2.hitTest(meep3)){trace('success');}

    if (meep3.hitTest(meep0)){trace('success');}
    if (meep3.hitTest(meep1)){trace('success');}
    if (meep3.hitTest(meep2)){trace('success');}


    Am I right? Then you need this:


    for(var i = 0; i < meepsArray.length; i++){
    for(var j = 0; j < meepsArray.length; j++){
    if(["meep"+i].hitTest(["meep"+j])){
    trace("success")
    }
    }
    }

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Quote Originally Posted by koenahn View Post
    Hi.

    You wrote this code:



    Let's assume the array length is 3. Then your code will do exactly this:

    if (meep0.hitTest(meep0)){trace('success');}
    if (meep1.hitTest(meep1)){trace('success');}
    if (meep2.hitTest(meep2)){trace('success');}
    if (meep3.hitTest(meep3)){trace('success');}

    But that is not what you want. What you want is this:

    if (meep0.hitTest(meep1)){trace('success');}
    if (meep0.hitTest(meep2)){trace('success');}
    if (meep0.hitTest(meep3)){trace('success');}

    if (meep1.hitTest(meep0)){trace('success');}
    if (meep1.hitTest(meep2)){trace('success');}
    if (meep1.hitTest(meep3)){trace('success');}

    if (meep2.hitTest(meep0)){trace('success');}
    if (meep2.hitTest(meep1)){trace('success');}
    if (meep2.hitTest(meep3)){trace('success');}

    if (meep3.hitTest(meep0)){trace('success');}
    if (meep3.hitTest(meep1)){trace('success');}
    if (meep3.hitTest(meep2)){trace('success');}


    Am I right? Then you need this:
    My code, nor yours, works at all. I don't understand why. The one you've presented to me should actually work, but i'm having no luck.

  4. #4
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    for(var i = 0; i < meepsArray.length; i++){
    trace(["meep"+i]._x);
    }

    Try running this. If it keeps saying undefined, maybe the names of your meep instances aren't "meep0" up to and including "meep"+meepsArray.length. In that case, I think the array stores the names of the meeps, and you should replace ["meep"+i] with ["meep"+meepsarray[i]] (supposing the array contains their serial numbers, not their names).

    If the _x values all return numbers, then I don't see why it would not work. You are running your code onenterframe, right? Or in a set interval? Either is good, as long as the code keeps looping.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Thank you very much koenahn! You set me in the right direction. What i ended up doing is what i was going to do any way, i was just using hittest for testing.

    I instead used a distance formula to calculate the distance between each individual meep:
    Code:
    		
    		for(var j = 0; j < meepsArray.length; j++){
    		Meep.distanceTMX = _level0["meep"+i]._x-_level0["meep"+j]._x;
    		Meep.distanceTMY = _level0["meep"+i]._y-_level0["meep"+j]._y;
    		Meep.distanceTotTM = Math.sqrt(Meep.distanceTMX * Meep.distanceTMX + Meep.distanceTMY * Meep.distanceTMY);
    	
    	if(Meep.distanceTotTM < 30 && Meep.distanceTotTM != 0){
    trace("success")
    }
    }
    If they're close enough, trace success. This code isn't perfect, in-fact, far from it. But it'll get me started on what i want in the end result. This was the part i was stuck on. Thanks a lot again! You helped me tremendously!

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    This is so similar to what you have ended up with (perhaps even is), but its clever stuff Here

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    11
    Quote Originally Posted by fruitbeard View Post
    Hi,

    This is so similar to what you have ended up with (perhaps even is), but its clever stuff Here
    Oh wow! That helps even more! That's exactly the type of thing i was looking for. Instead what my plan was, if the meeps got close enough, one would just slow down enough so they wouldn't be overlapping. However, this works better. Thank you fruitbeard! Happy holidays and merry christmas all!

  8. #8
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    Glad things are workin out for you. Thank you for showing your appreciation.

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