HitTest Instances of same movie clip

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

just a quick tutorial to show how to test if a dragged movie collides with the others of it's kind.
For this, you will need to have in the library a movieclip named: slug ,exported for actionscript.

Code: [ Select ]
// create the slugs in the given holder
function CreateSlugs(mcHolder)
{
    var nX:Number = 10;
    var nY:Number = 10;
    var ID:String;
    var nSlugIndex:Number;
    for(nSlugIndex=0; nSlugIndex < mcHolder.nCount; nSlugIndex++)
    {
        ID = "slug"+nSlugIndex;
        this.Slug = mcHolder.attachMovie("slug", ID, nSlugIndex);
        this.Slug._x = nX;
        this.Slug._y = nY;
        this.Slug.ID = nSlugIndex; // give an id to the slug
        nX += 50;
        nY += 50;
        
        this.Slug.onPress=function()
        {
            
            this._parent.crtSlug = this; // set the current slug
            this._parent.bSelected = true; // signal a slug was selected
            this.startDrag(true); // start drag
        }
        
        this.Slug.onRelease = this.Slug.onReleaseOutside = function()
        {
            this._parent.bSelected = false; // signal slug was deselected
            this.stopDrag(); // stop drag
        }
        
        // never do this; uncomment to see why
        //this.Slug.onMouseMove = function()
        //{
        //    if (this._parent.bSelected)
        //    {
        //        trace(this);
        //    }
        //}
    }
}

// always create only one onMouseMove function, since it's a general function
// and it doesn't matter if you set it for different movie clips
// If you create it let's say 3 times and place
// in the first one: trace("1");
// in the second one: trace("2");
// in the third one: trace("3");
// you'll see that it will display 1  2  3

// uncomment the function this.Slug.onMouseMove, and test to see what happens
this.onMouseMove=function()
{    
    if (track_mc.bSelected)
    {
        updateAfterEvent();
        TestCollide(track_mc.crtSlug);
    }
}

// check if the current slug hittest the others
function TestCollide(mcTarget:MovieClip) // pass the current slug as parameter
{
    var nSlugIndex:Number;
    var mcSlug:MovieClip;
    
    // loop through all the created slugs
    for (nSlugIndex = 0; nSlugIndex < track_mc.nCount; nSlugIndex++)
    {
        // if not current slug ID
        if (nSlugIndex != mcTarget.ID)
        {
            mcSlug = track_mc["slug"+nSlugIndex]; // get slug reference            
            // if current slug hitTest the slug reference, display this
            if (mcTarget.hitTest(mcSlug))
            {
                trace("slug"+mcTarget.ID+" colides with slug"+mcSlug.ID);
            }
        }
    }
}

this.createEmptyMovieClip("track_mc", 0);
track_mc.bSelected = false; // signal no slag is currently selected
track_mc.nCount = 4; // number of slugs to create
CreateSlugs(track_mc);
stop();
  1. // create the slugs in the given holder
  2. function CreateSlugs(mcHolder)
  3. {
  4.     var nX:Number = 10;
  5.     var nY:Number = 10;
  6.     var ID:String;
  7.     var nSlugIndex:Number;
  8.     for(nSlugIndex=0; nSlugIndex < mcHolder.nCount; nSlugIndex++)
  9.     {
  10.         ID = "slug"+nSlugIndex;
  11.         this.Slug = mcHolder.attachMovie("slug", ID, nSlugIndex);
  12.         this.Slug._x = nX;
  13.         this.Slug._y = nY;
  14.         this.Slug.ID = nSlugIndex; // give an id to the slug
  15.         nX += 50;
  16.         nY += 50;
  17.         
  18.         this.Slug.onPress=function()
  19.         {
  20.             
  21.             this._parent.crtSlug = this; // set the current slug
  22.             this._parent.bSelected = true; // signal a slug was selected
  23.             this.startDrag(true); // start drag
  24.         }
  25.         
  26.         this.Slug.onRelease = this.Slug.onReleaseOutside = function()
  27.         {
  28.             this._parent.bSelected = false; // signal slug was deselected
  29.             this.stopDrag(); // stop drag
  30.         }
  31.         
  32.         // never do this; uncomment to see why
  33.         //this.Slug.onMouseMove = function()
  34.         //{
  35.         //    if (this._parent.bSelected)
  36.         //    {
  37.         //        trace(this);
  38.         //    }
  39.         //}
  40.     }
  41. }
  42. // always create only one onMouseMove function, since it's a general function
  43. // and it doesn't matter if you set it for different movie clips
  44. // If you create it let's say 3 times and place
  45. // in the first one: trace("1");
  46. // in the second one: trace("2");
  47. // in the third one: trace("3");
  48. // you'll see that it will display 1  2  3
  49. // uncomment the function this.Slug.onMouseMove, and test to see what happens
  50. this.onMouseMove=function()
  51. {    
  52.     if (track_mc.bSelected)
  53.     {
  54.         updateAfterEvent();
  55.         TestCollide(track_mc.crtSlug);
  56.     }
  57. }
  58. // check if the current slug hittest the others
  59. function TestCollide(mcTarget:MovieClip) // pass the current slug as parameter
  60. {
  61.     var nSlugIndex:Number;
  62.     var mcSlug:MovieClip;
  63.     
  64.     // loop through all the created slugs
  65.     for (nSlugIndex = 0; nSlugIndex < track_mc.nCount; nSlugIndex++)
  66.     {
  67.         // if not current slug ID
  68.         if (nSlugIndex != mcTarget.ID)
  69.         {
  70.             mcSlug = track_mc["slug"+nSlugIndex]; // get slug reference            
  71.             // if current slug hitTest the slug reference, display this
  72.             if (mcTarget.hitTest(mcSlug))
  73.             {
  74.                 trace("slug"+mcTarget.ID+" colides with slug"+mcSlug.ID);
  75.             }
  76.         }
  77.     }
  78. }
  79. this.createEmptyMovieClip("track_mc", 0);
  80. track_mc.bSelected = false; // signal no slag is currently selected
  81. track_mc.nCount = 4; // number of slugs to create
  82. CreateSlugs(track_mc);
  83. stop();
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

Thank you professor,

I am actually unfamiliar with the forum scene, and thought I had to be special to post publically. I very much appreciate your response and for future question I will post publically (but what if I am only interested in your response?)

thank you again

My current projects are found at

http://cluttered.flvservers.com/
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

Professor,

As I progress in Actionscripting I still find find I have to go back to the basics and reconsider some of the fundamentals - a couple of questions on the above script:

you begin by passing mcHolder to the function which attaches the slug - is mcHolder an Object, variable, something else ? I adding type :Object, :MovieClip and tested it, but it also works just as well with :Array,

second question -is the above code creating an mcHolder for each Slug, i.e. 4 EmptyMovie Clips ? and if so why do we not include createEmptyMovieClip inside the createSlug function this.mcHolder= createEmptyMovieClip(); and then this.Slug=this.mcHolder.attachMovie etc - is this the same thing? is this wrong?

and lastly - why do we make the track_mc selected onPress and not the Slug - or should we always put a Clip inside a holder and select the holder?

oh and also - should onMouseMoves always be outside of functions?
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

4) keep onMouseMove outside of functions, since it's general for all movieclips.
3) this._parent.crtSlug = this; // set the current slug

this means it will set the variable named crtSlug with the pressed slug.
It is just a variable that points to the pressed slug. You don't make track_mc selected, you just set a variable inside track_mc.
You should get familiar with object oriented programming.
If you would've set a variable inside the slug, then how would you knew which slug is selected? You would've had to go through all the slugs you created and check if they're the selected one .... why would you do that when you can simply know by only checking one variable?

2) CreateSlugs(track_mc); -> passes the created movieclip track_mc as a variable to the function CreateSlugs. Since it is already created, what reason would be for me to create it N times?
Again, get familiar with object oriented programming.

1) mcHolder is exactly the type of the variable that i'm sending. Flash knows to recognize the variables types, so it can use any variable type, even if it is not specified. In this case is a MovieClip.


small sample:

this.createEmptyMovieClip("mc1", 0); // create a movieclip named mc1
mc1.var1 = 30; // creates a variable called var1 and set's it's value to 30
mc1.var1 = "sd"; // overwrites the variable called var1 and set's it's value to "sd
mc1.nCount = 5; // creates a variable called nCount and set's it's value to 5

mc1.createEmptyMovieClip("mc1a", 0); // create a movieclip named mc1, inside mc1

Please get accustomed to the object oriented programming first, in order to understand the way you can use actionscript.
  • johnnygod
  • Novice
  • Novice
  • johnnygod
  • Posts: 25

Post 3+ Months Ago

thanks again professor,

I'm studying oop.

all the best -

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 6 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.