I have amended edmacks flash file which enables a cannon to follow the mouse pointer and shoot bullets at varying speeds dependant on how long the mouse click is held for.
I have 3 problems that need addressing :-
1) CONTINUAL HIT TEST
I have placed a circle on screen (as an MC) and have named it "hitarea".
The hit test works, because I have traced "hit" - but it only works on the first fire. Not the second or third or whatever.
I need some sort of amendment to ensure that no matter how many times the bullet is fired, the hittest will take place.
2) DISAPPEARING BULLET
Once the hittest is complete and the test returns true, I need the bullet to disappear. How is that achieved ?
3) PLACING A SPLASH AT THE HIT AREA
If the hittest returns true, I also want another MC to play at EXACTLY the position where the "hit" occurred.
I have created 2 animation MC's - Splash 1 & Splash 2. How do I go about placing those animations randomly (So either 1 or 2 appears) at the co-ordinates of the hit ?!?!
Any help will be most appreciated. Even if you can only answer 1, it will certainly alleviate my frustration.
How can I restrict the angle of fire so that the turret only points certain places.
Imagine 0 degrees is the turret pointing straight up, I want the turret to go from 10 degrees to around 110 degrees. So in essence, from almost up to pointing at an angle downwards.....
On hittest, where you currently have trace ("hit"), add a removeMovieClip.
As for why the hittest only works once, make the hittest use the instance of the movieclip, instead of just the first movie clip.
how about just posting the code that is relevant? I rather avoid downloading and compiling other´s files. Only if there is no other way i´d give it a look eventually
onClipEvent (enterFrame) {
//calculate mouse point and adjust the angle of the mc to match.
angle = (Math.atan2(_root._ymouse-_y,_root._xmouse-_x)*180/Math.PI)+90;
_rotation = angle;
idle;
if (this.hitTest(_root.Bullet._x, _root.Bullet._y, true)) {
_root.Bullet is the name of movie clip that will be used for hitTesting. Since it is the name of first bullet, obviously all other bullets that have different names are not being tested.
You will need to set up loop to test all bullets, something like this:
PHP Code:
for(n = 0; n < 100; n++){
if (this.hitTest(_root["Bullet"+n]._x, _root["Bullet"+n]._y, true)) {
//trace, remove etc here
}
}
Here the example expects you to reset bullet counter after it reaches 100, if you dont reset it the loop will slow down the game with more and more bullets. Other ways to test multiple bullets would be using array and object where each bullet is added when created and removed when it leaves the game.
This kind of collisions have been discussed many times before, you may want to try searching the board.
BUT, my first bullet no longer works. I assume that this is something to do with swapdepths ?
I have spent the last few hours trawling this site for answers without having to resort to more questions......but here I find myself once again.
So :-
A) How can I get my first bullet to work again (in a hit test type of way)
B) How can I remove these bullets ?
Once the hit test is complete (and I've traced it so I know they work) the removeMovieClip(); doesn't work.
Here is my amended code :
--------------------------------
How do I place a movie clip at the hit point ? - I have a movie clip in my library called "hit", how do I get that placed at every hit point from the bullet. They are essentially small explosion animations.
1. Your variable bc is undefined so first bullet gets the name "Bullet", after its created bc gets value of 0 and is also increased by 1 so next bullet is Bullet1. Give default value 0 to the bc if its undefined:
Code:
if(bc == undefined){
bc = 0;
}
var n = "Bullet" + bc;
Thanks for this tonypa - I thank you for the explanation too, that helps more than the code.
Much obliged. The hit animations now play for every hit, which is nice.
The removeClip also works but the very first bullet does not get removed on a hit. The rest do......
Any thoughts ?
Is there also a way to get my hit animation (explosion) play exactly at the hit point, rather than the registration point of the hit tested MC ?
Imagine a large area to be tested for a hit, I could hit it at the top, middle and bottom and the hit explosion would always play in the middle. Anyway I can get it to play at the EXACT hit co-ordinates ?
Try tracing the names of bullets or check them in preview window (list objects...).
To make the animation appear at exact point of collision you would first need to find that collision point. Hittest function only says if 2 objects are covering each other, it will not give any information about the point of collision. Finding real collision point can be done using math but its much more complicated. You could look at my vector tutorials to see what kind of math gets involved: