# \[FMX\] Hittest and functions

**URL:** <https://forum.kirupa.com/t/fmx-hittest-and-functions/18611>\
**Category:** flash\
**Created:** [April 18, 2003, 4:41pm UTC](https://forum.kirupa.com/t/fmx-hittest-and-functions/18611 "2003-04-18T16:41:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgelston](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tgelston/32/54_2.png) [@tgelston](https://forum.kirupa.com/u/tgelston)\
**Post date:** [April 18, 2003, 4:41pm UTC](https://forum.kirupa.com/t/fmx-hittest-and-functions/18611/1 "2003-04-18T16:41:42Z")

</div>

I have a problem running a hitTest on a duplicated object - it doesnt work.

Here is what I have so far, all of this code is in the onload of a movie that is just holding my code-  
[AS]  
//Makes the rocks   
function dupRockFunction() {  
var chance = Math.round(random(25));  
if (chance == 11) {  
rockdepth++;  
\_root.spaceRock = this.duplicateMovieClip("\_root.rock"+rockdepth, rockdepth);  
\_root.spaceRock.\_y = random(400);  
\_root.spaceRock.\_xscale = random(50)+30;  
\_root.spaceRock.\_yscale = \_root.spaceRock.\_xscale;  
\_root.spaceRock.\_rotation = random(360);  
}  
//Moves the rocks  
function moveRemoveRockFunction() {  
//moves clip to left  
this.\_x -= rockSpeed;  
//removes clip when off stage  
if (this.\_x\<20) {  
this.removeMovieClip();  
}  
if (this.hitTest(\_root.ship)) {  
trace(“Hello”);  
}  
}  
[/AS]

I call both functions in the enterframe section of the same MC  
[AS]  
\_root.spaceRock.onEnterFrame = moveRemoveRockFunction;  
\_root.rock.onEnterFrame = dupRockFunction;  
[/AS]

Why doesnt that hittest work? if “this” works to move them across the stage why doesnt it also see the contact of the two MCs??

I am new to functions - slowly learning so if you see anything else major I should change please let me know.

Thanks,  
T

---

<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 18, 2003, 4:48pm UTC](https://forum.kirupa.com/t/fmx-hittest-and-functions/18611/2 "2003-04-18T16:48:29Z")

</div>

… is that moveRemoveRockFunction inside dupRockFunction? is that what you wanted? and where are  
\_root.spaceRock.onEnterFrame = moveRemoveRockFunction;  
\_root.rock.onEnterFrame = dupRockFunction;  
?  
They arent in the onClipEvent(enterframe) are they?  
And do you know duplicated clips keep all functions attached?  
also in the name part of duplicate movie, you dont need to specify \_root. - just the name

At first glance Im having a little trouble seeing the setup of this. a file would be nice 🙂

---

<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 18, 2003, 5:23pm UTC](https://forum.kirupa.com/t/fmx-hittest-and-functions/18611/3 "2003-04-18T17:23:59Z")

</div>

> … is that moveRemoveRockFunction inside dupRockFunction? is that what you wanted?

NO they should not be inside each other - I dont think they are.

> 

and where are  
\_root.spaceRock.onEnterFrame = moveRemoveRockFunction;  
\_root.rock.onEnterFrame = dupRockFunction;?  
They arent in the onClipEvent(enterframe) are they?

all of the code I pasted is sitting on a MC - the function section is in the onload and the function calling: [AS]  
\_root.spaceRock.onEnterFrame = moveRemoveRockFunction;  
\_root.rock.onEnterFrame = dupRockFunction;  
[/AS] is in the enterframe section of the same clip - is that bad? I could not get it to work if I stuck it in the first frame of the game scene

> And do you know duplicated clips keep all functions attached?  
> Yes. . . I think so - does my code not reflect that? That is why I expect the hitTest to work - using this. moves the object but it wont see the hitTest.

I need to add: all the code referenced is on the blue circle left of stage. Thanks  
[Download the Fla](http://www.mcps.k12.md.us/schools/watkinsmillhs/rock_attack_scroller.fla) (sorry it 330Kb)

Thanks for looking,  
Tobias

---

<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 19, 2003, 11:36am UTC](https://forum.kirupa.com/t/fmx-hittest-and-functions/18611/4 "2003-04-19T11:36:08Z")

</div>

this was my problem, senocular, pointed it out but I ignored him becuase everything else worked. I would love it if someone could explain why it dupicates correctly with \_root. there but will not do the hitTest correctly?

[AS]\_root.spaceRock = this.duplicateMovieClip("\_root.rock"+rockdepth, rockdepth);  
[/AS]

should be

[AS]\_root.spaceRock = this.duplicateMovieClip(“rock”+rockdepth, rockdepth);  
[/AS]

no \_root in front of “rock”

just sharing!  
Tobias

---

<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 19, 2003, 12:54pm UTC](https://forum.kirupa.com/t/fmx-hittest-and-functions/18611/5 "2003-04-19T12:54:29Z")

</div>

for function dupRockFunction the closing } is missing, that’s why sen asked about the nesting, 1st } closes the if only…  
“\_root.rock” or “rock” +rockdepth will be the instance name of the duplicated clip, so it’s not the same…
