# hitTest() giving weird results

**URL:** <https://forum.kirupa.com/t/hittest-giving-weird-results/106597>\
**Category:** Uncategorized\
**Created:** [April 7, 2004, 7:12am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597 "2004-04-07T07:12:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyko](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pyko/32/502_2.png) [@pyko](https://forum.kirupa.com/u/pyko)\
**Post date:** [April 7, 2004, 7:12am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/1 "2004-04-07T07:12:34Z")

</div>

hi, im just starting to learn some action script in flash mx…and im a bit stumped and i was wondering if i could get some help.  
I have this bit of code on the movie clip of a bird

```auto
onClipEvent(load)
{
	this._x = Math.round(random(400));
	this._y = Math.round(random(300));
	this._xscale = random(100)+60;
	this._yscale = random(100)+60;
}
 
onClipEvent(enterFrame)
{
	if(this, hitTest(_root.Aim))
	 {
		 _root.Aim._rotation = -22.5;
		 _root.txtStatus = "hit!"
		 this._alpha = 20;
	 }
	else
	 {
		 _root.txtStatus = "miss!";
	 }	
}

```

Aim - is just a custom cursor i drew  
txtStatus - a dynamic text box

my aim was very simple, to duplicate several copies of the movie clip 'Bird" and get the text box to show ‘hit’ when the cursor is over the bird and ‘miss’ when the cursor is anywhere other than over the bird(s).

the thing i’m confused about is ALL the copies of ‘Bird’ respond to the ‘alpha’ change, but only the original\* will respond to the change of text. And as for the rotation, i have in the Aim cursor **this.\_rotation += 22.5** , so i thought, well, if i minus 22.5 it will make the cursor ‘upright’ (or no rotation), this worked for the copies, but as for the original\* it is ‘tilted’ by 22.5 degrees (to the left).

[size=1]\*\*i’m quite sure its the original - even though i placed the original off stage, it still seems to appear when i play the movie.\*[/size]  
[size=1]  
[/size]  
i can’t seem to find any errors in my coding/syntax, and what i don’t understand is why the copies respond differently (or don’t respond at all) to each command.

any help would be greatly appreciated!  
pyko

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 7:26am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/2 "2004-04-07T07:26:56Z")

</div>

Can you send us your FLA so we could see it happening ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 7:33am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/3 "2004-04-07T07:33:05Z")

</div>

ok, ill attach it here

if there’s anything else (other than the lil problem i can’t seem to figure out) that anyone sees, please point it out! always willing to learn!

thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 7:58am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/4 "2004-04-07T07:58:08Z")

</div>

I see the problem. You have 11 birds on your screen, and they all check if they are hit by Aim. If so, then they change the text to Hit, and if not, they change the text to Miss. But if one of them is hit, that one tells to show Hit, but all the others at the same time tell to show Miss. I’ve written an if statement that is a bit more advanced and does the trick.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 8:04am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/5 "2004-04-07T08:04:13Z")

</div>

> 

i’m quite sure its the original - even though i placed the original off stage, it still seems to appear when i play the movie

Sure the orginal appears on stage to… it has the on load random function as well.  
But indeed very weird. I don’t think that is a problem of hitTest. But still I’m can’t get to the bottom of this :|.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 8:06am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/6 "2004-04-07T08:06:23Z")

</div>

yeahaa… great voetsjoeba…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 8:34am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/7 "2004-04-07T08:34:06Z")

</div>

wow…so much more script.  
being new to ActionScript…id just like to confirm a few little things

```auto
Aim.onMouseMove = function() {
	this.hitstat = 0;
	for (i=0; i<=10; i++) {
		if (!this.hitTest(this._parent["Bird"+i])) {
			this.hitstat = 0;
		} else {
			this.hitstat = 1;
			this._parent["Bird"+i]._alpha = 20;
			break;
		}
	}
	this.hitstat ? this._parent.txtStatus.text="hit!" : this._parent.txtStatus.text="miss!";
};

```

i understand how the code runs, but just a few syntax queries…  
\*\*if (!this.hitTest(this.\_parent[“Bird”+i])) \*\*  
what is the significance of the ‘!’ i this if statment…

**this.hitstat ? this.\_parent.txtStatus.text=“hit!” : this.\_parent.txtStatus.text=“miss!”;**  
i don’t quite understand the syntax here…is it a shorthand for an IF statment?

is \_parent the same as \_root ?

and one last nagging question…when duplicating the mc ‘bird’ is there any advantage in placing the code in the Frame and using:

```auto

curBird = Bird.duplicateMovieClip("Bird"+i, i);
curBird._x = random(400);
etc

```

compared to just putting this script on the actual MC Bird?

THANK YOU all very much!  
pyko

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 7, 2004, 9:31am UTC](https://forum.kirupa.com/t/hittest-giving-weird-results/106597/8 "2004-04-07T09:31:10Z")

</div>

The **!** is a logical NOT. You can read more about it [here](http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary007.html).

The form

```auto
condition ? statement1 : statement2;

```

is indeed a shortened version of

```auto
if(condition){
  statement1;
  } else {
  statement2;
  }

```

I prefer to use this form if only one statement has to be executed if true or false. You can’t use this form with multiple statements. Example: this doesn’t exist:

condition ? statement11, statement12 : statement21, statement 22;

About \_parent and \_root - those are a very important aspect of ActionScript, and understanding Flash in general. They have to do with the hierarchical structure of a Flash movie. This structure isn’t the easiest to understand for people who are new to Flash. \_parent goes one level up in the hierarchical structure, while \_root is the top of the hierarchical structure. There are lots of tutorials out there, and I strongly suggest reading them because this is quite important if you want to get into ActionScript 🙂

About the code on the frame - I prefer to do it that way. There are several advantages to this method, not in the least the code standing all together instead of divided to several movieclips here and there. This creates a big advantage, especially when debugging. A second advantage has to do with the scope, but you don’t have to worry about that just yet.

Also, if you’d have placed the duplicating code on the movieclip itself, you’ll create an infinite duplicating loop and crash your Flash movie. That’s because when you place the duplicating code on the movieclip itself and it gets duplicated 10 times for example, then every duplicated movieclip will also execute the code it has and therefore duplicate itself again 10 times. And each duplicate will then again duplicate itself again 10 times. You understand the situation 😉
