A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: hitTest

  1. #1
    Senior Member
    Join Date
    May 2005
    Posts
    324

    hitTest

    can i use hitTest to return the absolute address of every movieclip, including nested ones, which a movieclip passes over?
    "great scott!"

    "I know, this is heavy!"

  2. #2
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    Heres a function for you

    code:
    function checkHits(obj) {
    var hitArr=new Array();
    for(i in _root) {
    if(_root[i].hitTest(obj) && _root[i]!=obj) {
    hitArr.push(String(_root[i]));
    }
    for(j in _root[i]) {
    if(_root[i][j].hitTest(obj) && _root[i][j]!=obj) {
    hitArr.push(String(_root[i][j]));
    }
    }
    }
    }



    It returns the absoloute address of everything touching object obj either in root or in an MC in root. im not sure how to do it so it does it with infinate stages of nesting though.
    -luke.slashmedia.co.uk(my site)
    (^.^)

  3. #3
    Senior Member
    Join Date
    May 2005
    Posts
    324
    could you talk me through this a little? a few items i dont regogonise

    for example the 'in', what does that do?

    and how can i implement it so it continually checks?
    "great scott!"

    "I know, this is heavy!"

  4. #4
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    for .... in .... basically goes through each object in another object.

    for..in
    Availability
    Flash Player 5.

    Usage
    for(variableIterant in object){
    statement(s);
    }

    Parameters
    variableIterant The name of a variable to act as the iterant, referencing each property of an object or element in an array.

    object The name of an object to be repeated.

    statement(s) An instruction to execute for each iteration.

    Returns
    Nothing.

    Description
    Statement; loops through the properties of an object or element in an array, and executes the statement for each property of an object.

    Some properties cannot be enumerated by the for or for..in actions. For example, the built-in methods of the Array class (such as Array.sort() and Array.reverse()) are not included in the enumeration of an Array object, and movie clip properties, such as _x and _y, are not enumerated. In external class files, instance members are not enumerable; only dynamic and static members are enumerable.

    The for..in statement iterates over properties of objects in the iterated object's prototype chain. If the child's prototype is parent, iterating over the properties of the child with for..in, will also iterate over the properties of parent.

    The for..in action enumerates all objects in the prototype chain of an object. Properties of the object are enumerated first, then properties of its immediate prototype, then properties of the prototype's prototype, and so on. The for..in action does not enumerate the same property name twice. If the object child has prototype parent and both contain the property prop, the for..in action called on child enumerates prop from child but ignores the one in parent.

    Example

    The following is an example of using for..in to iterate over the properties of an object:

    myObject = { name:'Tara', age:27, city:'San Francisco' };
    for (name in myObject) {
    trace ("myObject." + name + " = " + myObject[name]);
    }

    The output of this example is as follows:

    myObject.name = Tara
    myObject.age = 27
    myObject.city = San Francisco

    The following is an example of using the typeof operator with for..in to iterate over a particular type of child:

    for (name in my_mc) {
    if (typeof (my_mc[name]) = "movieclip") {
    trace ("I have a movie clip child named " + name);
    }
    }

    The following example enumerates the children of a movie clip and sends each to Frame 2 in their respective Timelines. The RadioButtonGroup movie clip is a parent with several children, _RedRadioButton_, _GreenRadioButton_ and _BlueRadioButton.

    for (var name in RadioButtonGroup) {
    RadioButtonGroup[name].gotoAndStop(2);
    }

    Also, in the function i wrote, i forgot to include the line:
    return hitArr;

    So add that to the end of it.

    But what do you want it to do every frame. This function just returns the addresses of MCs. It doesn't actually do anything with them.
    -luke.slashmedia.co.uk(my site)
    (^.^)

  5. #5
    Senior Member
    Join Date
    May 2005
    Posts
    324
    could you have a look at what i have?

    in the included .fla, press new and launch a new 'striker' and a new 'sound'.

    it is set up to trace all mc address that hit the sound, so if you move it about, you'll see the effect in the output window.

    you can see the names for _level0.striker1.sp1 , sp2, sp3 and sp4

    this doesnt work properly, as it should output those values whenever the sound is above a spoke of the striker wheel

    also, because the strikers are dynamically labeled (striker1, striker2, striker3, etc)
    i need it to identify when it has been struck by an embedded spoke (sp1, sp2 etc) and then return its full address, so that i can retreive a variable embedded within that specific spoke object (click and release on the striker to bring up the settings page, you'll see what i mean)

    this is for a uni project, and implementing this has been a royal pain!!

    i dont know if this would help, but all strikers address's are kept in a global array, _root.strikers. could i use that to help identify which striker is being used, and then which spoke within that?
    "great scott!"

    "I know, this is heavy!"

  6. #6
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    ...can you include the fla then?
    -luke.slashmedia.co.uk(my site)
    (^.^)

  7. #7
    Senior Member
    Join Date
    May 2005
    Posts
    324
    ooops

    here it is

    all relevant code appears within the 'sound object' MC. on the actions layer
    Last edited by 88MPH; 09-30-2007 at 03:16 PM.
    "great scott!"

    "I know, this is heavy!"

  8. #8
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    Wow, this is a very cool idea. Can you send me the final version when its done cos i'd quite like to see how it turns out .

    I've done the code as if the sound things were rectangular. I know its not perfect but due to the number of nested MCs with different rotations it would be really hard to get a good hittest and im just too tired at the moment. If you need it to hittest the circles and not just the circles bounding boxes then hassle me some more.

    What i've done is put 10 points on each spoke and it hittests the sound object against each point in each spoke in each striker. Man those nested for statements are confusing. I used i, j and k!

    But yeah, heres the source. This returns the address of each spoke touching the sound object.

    Tell me if you need some more help, or if you want me to run through the code or whatever.

    And good luck with it

    btw - you should probably make a statement on the first frame of the hitTestPoint MC that says _visible=false.
    Attached Files Attached Files
    Last edited by DrDoom; 04-30-2006 at 11:23 AM.
    -luke.slashmedia.co.uk(my site)
    (^.^)

  9. #9
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    Well, i didn't want to appear slovenly, so i made it do perfect hitTesting anyway . Thats one long line of code:


    code:
    if(obj.hitTest(_root.strikers[i]._x+Math.sin((_root.strikers[i]._rotation+spokes[j]._rotation)*Math.PI/180)*-spokes[j].points[k]._y, _root.strikers[i]._y+Math.cos((_root.strikers[i]._rotation+spokes[j]._rotation)*Math.PI/180)*spokes[j].points[k]._y, true)) {


    Here's the source:
    Attached Files Attached Files
    -luke.slashmedia.co.uk(my site)
    (^.^)

  10. #10
    Senior Member
    Join Date
    May 2005
    Posts
    324
    thats wicked! one step closer to actually working!!

    could you talk me through the workings of the above code? the thing i find difficult at the moment is i need to use the results to control the output of a sound. when the sound gets struck by 2 spokes at the same time, it outputs an array list, which i am not sure how to break down.

    to avoid retriggering of sounds, i need it to only output once each hit. i have an idea of how to do this, but i dont fully grasp your code

    also, do you think it might be best to have each spoke assess the impact of a sound, rather than the way round we have it now?

    cheers for your help, and the finished product will be online when finished, again, as part of the project!

    cheers
    "great scott!"

    "I know, this is heavy!"

  11. #11
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    Are different sound objects going to make different sounds?

    (ill go through the code later. promise)
    -luke.slashmedia.co.uk(my site)
    (^.^)

  12. #12
    Senior Member
    Join Date
    May 2005
    Posts
    324
    yeah, and each sound might (if possible) be triggered mulitiple times at mulitple velocities
    "great scott!"

    "I know, this is heavy!"

  13. #13
    Senior Member DrDoom's Avatar
    Join Date
    Apr 2006
    Posts
    144
    Are the sounds going to be attached via the library or downloaded as an mp3?
    -luke.slashmedia.co.uk(my site)
    (^.^)

  14. #14
    Senior Member
    Join Date
    May 2005
    Posts
    324
    downloaded, this is the current plan, but as yet not actioned.

    i am looking into way to implement sound, but at present just getting the hitTests,a nd the corresponding variables to work right

    as an aside, do you use messanger? because it seems going through this forum is somewhat counter-productive

    psc.recordings@gmail.com is my MSN handle
    "great scott!"

    "I know, this is heavy!"

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