[FMX] Hittest and functions

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

… 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 :slight_smile:

… 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 (sorry it 330Kb)

Thanks for looking,
Tobias

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

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…