0 Replies Latest reply on Dec 4, 2007 8:42 AM by pointy_ears

    hitTest: trying desperately to overcome its eccentricities

    pointy_ears
      Okay, here's what I'm trying to accomplish. Quick version: I'd like to run "hitTest" on a dynamic reference, rather than on a set object name.
      Long version: I'm using tweens on my main timeline to move around a movie clip that looks like an arrow-style mouse cursor. I'd like to be able to have "hit areas" on the stage and make it so that when the "cursor" passes over them in the midst of its travels, it changes to a hand-style cursor, so that it looks like a real mouse cursor moving over real hit areas. The arrow image and the hand image, by the way, are simply two separate frames within my "cursor" movie clip, so it's a simple matter of gotoAndStop-ing between them.
      Now, I'd rather not have to scrub through the main timeline, determine when the tweens take the cursor over what I'm pretending are hit areas, and manually make control keyframes to tell the cursor movie clip to switch back and forth between its frames. I'd like to be able to put invisible movie clips on the stage to demark my "hit areas," and then write code in the cursor movie clip that will allow it to determine for itself when it is touching one of those areas.
      Using hitTest for a single, specific movie clip is easy. Unfortunately, I can't combine all my hit areas into a single movie clip, because when I go to a new screen in my animation, where the hit areas are arranged differently, I would need a whole new movie clip, so I'd end up making a whole slew of different "hit area" movie clips in my library. I'd rather have a single, rectangular movie clip symbol that I can position in as many places per screen as necessary, transforming each respective instance as needed to fit that particular hit area. Here's where the problems start.
      As we all know, I can't give all of the instances the same name and feed the "hitTest" code a single object name to work with; the actionscript will work with only the first of the instances that bear the provided name. I've named the hit area movie clips "hitBox1", "hitBox2", "hitBox3", and so on. From there, of course, I'd rather not have to create a separate hitTest for each "hitBox..." by name, so my idea was to use a loop. I tell the cursor movie clip to do an onEnterFrame check of the objects in this._parent ( for (var curObj in this._parent)). The first thing that the code does with each object ("curObj") is check to see if the first six letters of its name are "hitBox": if (substring(this._parent[curObj]._name, 1, 6) == "hitBox"). If so, then it proceeds to the next step, where it checks that particular object for collision: if (this.hitTest(this._parent[curObj])), where "this" is referring to the cursor movie clip itself -- that is where this code is embedded.
      Unfortunately, the "cursor" is changing to a hand only when it passes over the lowest-level instance named "hitBox...", even though the loop is feeding the hitTest references to each of the hitBoxes, one at a time: "hitBox1", "hitBox2", etc. I know that the test for names starting with "hitBox" is working, because I have a trace inside of that, not only telling me that it found a "hitBox...", but also tracing the full name of that object, so that I can verify that it's getting the name right. I also know that I'm coding correctly to reference that object, because I have a line within the "hitBox..." check to change the object from fully transparent (the hitBoxes' property on the timeline) to partially opaque: this._parent[curObj]._alpha = 50; -- all of the hitBoxes, and only the hitBoxes, change to 50% opacity. I can also get the cursor to change by manually referencing one of the hitBoxes: if (this.hitTest(hitBox4)), but like I said, if (this.hitTest(this._parent[curObj])) triggers with only the bottom-most "hitBox...".
      Any ideas? (Were you even able to follow my description?) I would consider myself only an intermediate actionscripter at best, so I'm hoping that I'm just missing something that is obvious to someone more advanced, or maybe there's even an easier approach to what I want to accomplish, so I'd greatly appreciate anything that anyone knows to make this happen.
      Btw, yes, I would like to run the test to find all of the "hitBox..." objects only once per screen, load them into an array, then cycle through that array to run hitTests each frame, rather than running the entire "for...in" process every frame, but first I need to get the hitTest to work.

      }:-|