normally this works or at least i have had other situations where it did.
i have attached a movie clip to the root level with another movieclip. when the attached movie clip hits another movieclip on stage, the movieclip should unload, but the hitTest never works.
my code:
this is in the attacher movieclip:
Here's why: Look at the last thing that your onRelease handler does. It increments the depthcounter variable. This means that now depthcounter is one value more than the one in the name of the object you just created.
So when you run the hitTest, there is no such object out there with that name.
Try taking away the line where you increment the depthcounter variable, and see if it works.
How are you creating your character movie clip? At authoring time? If so, you can't remove it without swapping it's depth to a positive number. Still, you should be getting the trace action.
To tell you the truth, you should first abandon object code (and the "on" clip events) in favor of frame code. Put all your code on frame 1 of the main timeline in an actions layer. There is no downside to this; it's just so much easier to work with. The only requirement to it is that you have to give all your objects instance names.
By doing this, you can now set and remove onEnterFrame functions, which you cannot do using object code. There are many other advantages as well, the main one being that your code is all in one place, and not scattered around the file.
I'm attaching a file that will help you. I made a ball and a bullet movie clip. The bullet gets attached to a random y location at the right side of the screen. If a bullet hits the ball, it gets deleted and the bullet gets deleted.
I found that introducing a new bullet using onEnterFrame gave me too many too fast. setInterval is a function that lets you call another function repeatedly at whatever rate you want, independent of the framerate. But I'll take it apart for you:
theInterval is just a variable name I made up. It identifies the setInterval, and if you trace it out, you'll find it's just a number. Using it later with clearInterval turns the interval off again.
And here are the parameters:
this -- I've found that when you use setInterval to call any function that uses the keyword "this" inside of it, you have to use this longer form of setInterval:
setInterval(scope, function name in quotes, milliseconds);
function name -- the name of the function to call on an interval. In this case, createBullet.
500 -- the function will be called every 500 milliseconds, or every half second.
I realize that this may seem incredibly technical to you, it did to me, too. But I've done a ton of tinkering with it, and it gets easier and more familiar and soon you find you can make it do exactly what you want.
The ordinary form of setInterval is easier:
intervalName (you get to make this up) = setInterval(function, milliseconds);
In this form, the function name should NOT be in quotes. But if the function being called uses the keyword "this" anywhere inside it, use the longer form instead.
And anyway, you could get rid of the interval, and make a shooter game out of it instead, which is what I suspect you really want to do anyway. Just make some other event introduce a new bullet.
I realize this is bare-bones, and has a lot of room for improvement to make it interesting. But here is the possible beginning of a left-right shooter game (attached). Use left and right arrow keys to move, spacebar to shoot. Hope that helps a bit.
heh. the game i am making is going to have a rotating circular enemy that bounces off the sides of the screen. the enemy is lined with different colored targets on the edges. there are 4 different colored guns on the corners of the screen, the bullets must match the color of the targets to hit them, to gain ammo for each gun, you must click and drag that color ammo icon that appears randomly
ok, this deals with the topic at hand, sorta lol.
um what if there are multiple targets like 20 on the stage. how would you code the hitTest to hit all of the 20 targets?