A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: hitTestPoint code fails when placed inside a movieclip

  1. #1
    Junior Member
    Join Date
    Jan 2004
    Location
    UK
    Posts
    10

    Question hitTestPoint code fails when placed inside a movieclip

    Hello, hope you can help.

    I'm doing a basic hitTestPoint which works fine when everything is placed on the root of the flash file. However it fails when all the elements are placed within a movieclip.

    I've a feeling the problem is due either to the registration of the objects changing once they are placed in the new movieclip symbol, the 'relativity/absolutness' of my referencing or the "hitTestPoint (dragable_object_mc.x, dragable_object_mc.y, true)" part of my code.

    I've been playing around with it all for hours now and am at a bit of a loss, so any help would be appreciated.

    My code is as follows:

    I have two movieclips:
    dragable_object_mc - which you can pick up and drag
    static_crashzone_mc - If you drag the other object into this a collision is 'traced' in the output window
    (it works fine until I place the _mcs and code into a new _mc symbol)

    Incidentally if I use the 'hitTestObject' code on the root or within a movieclip, it works fine.


    //Pickup with mouse - works fine
    dragable_object_mc.addEventListener(MouseEvent.MOU SE_DOWN, bracketDrag1);

    function bracketDrag1(myevent:MouseEvent):void
    {
    dragable_object_mc.startDrag(false/*, myBoundaries2*/);

    }

    ///////////////////////////////

    //PutDown with mouse - works fine
    dragable_object_mc.addEventListener(MouseEvent.MOU SE_UP, bracketstopDrag1);
    function bracketstopDrag1(myevent:MouseEvent):void
    {
    dragable_object_mc.stopDrag();
    }

    ///////////////////////////////


    //collision HITtestPOINT - This bit works fine when everything is on the root, however fails if all _mcs and code are placed in a new _mc
    addEventListener(Event.ENTER_FRAME, objectCollision);

    function objectCollision(myevent:Event):void {
    if (static_crashzone_mc.hitTestPoint (dragable_object_mc.x, dragable_object_mc.y, true)){
    trace ("collision");


    }
    }

    stop();
    I can email a FLA file if it helps?
    Many thanks in advance

    MF
    keep the krazykayak faith; play online
    now!
    http://www.krazykayak.co.uk

  2. #2
    Junior Member
    Join Date
    Jan 2004
    Location
    UK
    Posts
    10

    Question Attached FLA

    I've attached the FLA file, if it makes it all clearer.

    The red version is on the root, whereas the green version is exactly the same but within a movie clip. Just drag and drop the little boxes for the collisions . . .

    Hope you can help, and thanks in advance

    MF
    Attached Files Attached Files
    keep the krazykayak faith; play online
    now!
    http://www.krazykayak.co.uk

  3. #3
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Hey there metalfury!

    Well I played around with your .fla and your hunch is correct - for some reason hitTestPoint uses the global coordinates of the point as opposed to the local ones. This means that you need to compensate when you call hitTestPoint.

    So you had the right idea, but the question is, how do you deal with it?

    Now one thing you can do is this:

    PHP Code:
    static_crashzone_mc.hitTestPoint (dragable_object_mc.this.xdragable_object_mc.this.ytrue)==true
    the "this" refers to the parent movieclip - by adding it's x and y values to the dragable_object_mc you're compensating for the global coordinates.

    Now this is fine if this is far as you want to go. But what if you have an MC within and MC within yet another MC? You'd have to be adding all of their coordinates to each other, and it would be stupid.

    To avoid nonsense, you can use DisplayObject's localToGlobal function. It takes some coordinates, and returns a point in global coordinates. In the case of you file, this is what you need:

    PHP Code:
    var globalPos:Point = new Point(dragable_object_mc.xdragable_object_mc.y);
    globalPos this.localToGlobal(globalPos);
    if (
    static_crashzone_mc.hitTestPoint (globalPos.xglobalPos.ytrue)==true
    By the way, in your code you have:

    PHP Code:
    if (static_crashzone_mc.hitTestPoint (dragable_object_mc.xdragable_object_mc.ytrue)==true){
            
    trace ("collision");
            
    static_crashzone_mc.nextFrame();
        }

    if (
    static_crashzone_mc.hitTestPoint (dragable_object_mc.xdragable_object_mc.ytrue)==false){
            
            
    static_crashzone_mc.prevFrame();
                
        }

    this is highly inefficient because you're hit testing twice - once to see if it's true, and again to see if it's false. The thing is, you only need to test it once - if it isn't true, then you know it's false. just use an else statement:

    PHP Code:
    if (static_crashzone_mc.hitTestPoint (dragable_object_mc.xdragable_object_mc.ytrue)==true){
            
    trace ("collision");
            
    static_crashzone_mc.nextFrame();
        }
    else{
            
            
    static_crashzone_mc.prevFrame();
                
        }

    also, because the function returns a boolean, writing "==true" isn't necessary.

    PHP Code:
    if (static_crashzone_mc.hitTestPoint (dragable_object_mc.xdragable_object_mc.ytrue)) 
    is the same thing.
    Check out my blog showing the development of my flash game, the Dregs of War

  4. #4
    Junior Member
    Join Date
    Jan 2004
    Location
    UK
    Posts
    10

    Thanks So Much :-)

    Hi there Awoogamuffin ,

    Thank you so much for your time and effort on this. I’ve played with both options so that I understand them fully and have integrated the second method into my actual project successfully.

    My actual movie is quite complex, so as you described, the first method wasn’t really practical. However the second method has worked perfectly.

    Really appreciate your help as I would of never got anywhere near finding the localToGlobal function, instead I would of probably ended up producing something that works, but was very untidy! I’m new to AS3, so my skills are pretty limited.

    Thanks too for the other tips on my inefficient code. The script is part of a big e-learning project I’m working on and was heavily stripped down for the .FLA I uploaded. In the full project there is lots going on so I think it evolved into the script I had while I couldn’t see the ‘wood from the trees’. I’ve tidied it up now and it’s all a lot cleaner following your suggestions.

    BTW, like the game in your blog.

    VMT
    metalfury
    keep the krazykayak faith; play online
    now!
    http://www.krazykayak.co.uk

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