A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: How to Make MC HitTest MC Within MC?

  1. #1
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38

    How to Make MC HitTest MC Within MC?

    I'm having trouble finding out how to get an MC within another MC to hitTestObject another MC. I've looked it up on different sites but every time I just get an error doing what they say I should.

    The problem is, the MC within the MC isn't instantly on the stage. It appears later on within it, and AS3 doesn't seem to think it exists, so I guess i have to do some overly complicated thing to get it to work. Anyone know how to do this?

  2. #2
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    I don't know if this will work, but you can make an ENTER_FRAME, to keep listening for the movieclip that appears later, and then make the hitTestObject:

    var NotYetVisibleMovieClip:MovieClip;

    NotYetVisibleMovieClip.addEventListener(Event.ENTE R_FRAME, check);
    function check(e:Event):void
    {
    if(e.currentTarget != null)
    {
    if(e.currentTarget.hitTestObject(thisOtherMovieCli p))
    {
    //do this
    }
    }
    }
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  3. #3
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    Sorry for such a late response, I had to solve another AS3 issue I've been having... now that that's solved, I'm back to this one.

    I tried it out, but the problem is, in the var, I don't know how to set up the MC that's within another MC. I have it as "hitbox", but it's within 2 MC's, "p2" and "jab". So it ends up being "p2.jab.hitbox".

    When I try to set that into the var, what you have named "NotYetVisibleMovieClip", it won't let me since there are periods in the way. How do I add a var for an MC that is within MC's?

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    var p2:MovieClip;

    p2.jab.hitbox.addEventListener(Event.ENTE R_FRAME, check);
    function check(e:Event):void
    {
    if(e.currentTarget != null)
    {
    if(e.currentTarget.hitTestObject(thisOtherMovieCli p))
    {
    //do this
    }
    }
    }
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  5. #5
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    I tried it, it's still giving me an error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Game_fla::MainTimeline/p1keys()

    I ran it in debug mode and it is saying it is this line:

    p2.jab.hitbox.addEventListener(Event.ENTER_FRAME, check);

    I'm a little confused about the var you have showed me... I'm supposed to keep it as NotYetVisibleMovieClip? Does AS3 know what that is, or am I supposed to change that? If so, what do I change it to?

  6. #6
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    What is p1keys?

    NotYetVisibleMovieClip is the name of the movieclip that you want to trigger, but that isn't visible yet. I think you should post your .fla.
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  7. #7
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    p1keys is the function event for the p1 MC. Basically, it's all of the controls of the specific character that I have named p1.

    The problem is, I have "NotYetVisibleMovieClip" changed to "hitbox", because that's the MC I want to trigger, but that MC is within MC's, as I have stated. So I don't think the var is able to set it up, unless I'm wrong? But that's what seems like is happening...

    var hitbox:MovieClip;

    p2.jab.hitbox.addEventListener(Event.ENTER_FRAME, check);
    function check(e:Event):void
    {
    if(e.currentTarget != null)
    {
    if(e.currentTarget.hitTestObject(p1))
    {
    //do this
    }
    }
    }

    I feel like I'm doing something wrong, can you tell what the issue is exactly? If not, I could post the fla soon if I'm still having problems in a bit, but there's a whole lot of script you'd have to dig through...

  8. #8
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Try this:

    var hitbox:MovieClip;
    var p2:MovieClip;

    p2.jab.hitbox.addEventListener(Event.ENTER_FRAME, check);
    function check(e:Event):void
    {
    if(e.currentTarget != null)
    {
    if(e.currentTarget.hitTestObject(p1))
    {
    //do this
    }
    }
    }
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  9. #9
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    Hm I'm still getting the error, it's like actionscript still doesn't think that the MC's exist...

  10. #10
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    var hitbox:MovieClip;
    var p2:MovieClip;

    p2.jab.hitbox.addEventListener(Event.ENTER_FRAME, check);
    function check(e:Event):void
    {
    if(hitbox != null)
    {
    if(hitbox.hitTestObject(p1))
    {
    trace("HIT TEST!");
    }
    }
    }
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  11. #11
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    Same error.

  12. #12
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Post .fla
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  13. #13
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    http://www.newgrounds.com/dump/downl...1d8481c8f2e293

    The hitbox is located in the second frame of the jab MC, which is in the second frame of the p1 MC, which is located on the left of the screen.

  14. #14
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    Did you get anywhere with it yet? Is what I'm trying to do even possible?

  15. #15
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    You just need to figure a few things. Post your .fla somewhere you don't have to sign up to download it. I signed up and the file isn't there.

  16. #16
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    Quote Originally Posted by moot View Post
    You just need to figure a few things. Post your .fla somewhere you don't have to sign up to download it. I signed up and the file isn't there.
    Oh, my apologies, I didn't know you needed to sign up to view that... I figured after a day or so you downloaded it, so I took it down.

    I guess the .fla isn't even really necessary though, I'll probably make a new file and do a test there...

  17. #17
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    That's a good start. Take small parts of your code apart and put it in a new .fla and test. I downloaded your .fla, and tried to fix it, but there's a LOT of code in there. I changed all the if (key = (Keboard.LEFT)); with if(e.charCode == Keyboard.LEFT) but there are still issues.
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  18. #18
    Member
    Join Date
    Dec 2008
    Location
    Colorado
    Posts
    38
    See, that's one of the reasons I was hesitant to upload it... so much code.

    I will make a new fla when I can and retry the test.

Tags for this Thread

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