11 Replies Latest reply on Mar 1, 2008 9:26 PM by kglad

# Multiple firings

Hi again!
Anyway, I can anyone help with a cannon game? I got it to fire off of the turret tutorial below:
http://www.foundation-flash.com/tutorials/turretpart1/
r:
attachMovie("turret", "turret", 1, {_x:275, _y:200});
turret.onEnterFrame = function() {
mousex = _xmouse-this._x;
mousey = (_ymouse-this._y)*-1;
angle = Math.atan(mousey/mousex)/(Math.PI/180);
if (mousex<0) {
angle += 180;
}
if (mousex>=0 && mousey<0) {
angle += 360;
}
this.cannon._rotation = (angle*-1)+90;
};
function onMouseDown() {
angle = turret.cannon._rotation-90;
start_ball_x = turret._x+30*Math.cos(angle*Math.PI/180);
start_ball_y = turret._y+30*Math.sin(angle*Math.PI/180);
cannonball_fired = attachMovie("cannonball", "cannonball"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*15;
cannonball_fired.diry = Math.sin(angle*Math.PI/180)*15;
cannonball_fired.onEnterFrame = function() {
this._x += this.dirx;
this._y += this.diry;
};
}

Only I changed the code so the cannon spins instead of following the mouse and used set interval to allow for random firing speed.
Now I was hoping to increase the danger by having the cannon shoot more than one cannonball at the time, except in this tutorial, shooting more than one causes the first "cannonball" to disappear. Is there a way to shoot multiple cannonballs at once. My guess would be a for next loop, but my experience is limited to a class in basic and a whole lot of tutorials and assumptions.
So thanks!
• ###### 1. Re: Multiple firings
use different names for you cannonballs.
• ###### 2. Multiple firings
Hi,
Still having some trouble, now, it fires both at the same time, though only one is affected by dirx and diry and the second does not move.
Is there any way to fix this with the code?
Thanks!

I tried using a for next loop, like this:
• ###### 3. Re: Multiple firings
that has more than a few problems. the biggest one is the logic.

what are you trying to do? fire several cannonballs sequentially? ie, with some time interval between the cannonballs? if so, use your original code. but place that mousedown code inside a function that you call sequentially (using an onEnterFrame or setInterval() ) when the mouse is down.

and you should add some code to remove your cannonball when it's off-stage and irrelevant.
• ###### 4. Re: Multiple firings
I'm afraid I'm still having trouble. I have tried adding the on EnterFrame, but now it won'y shoot at all.
Sorry for causing all the trouble.
What II did after adding the onEnterFrame:
• ###### 5. Re: Multiple firings
:

• ###### 6. Re: Multiple firings
Thanks for your help on all this, I've almost got it now. There's just one matter left, but if you don't want to fix it, don't bother, i don't want to keep bugging everyone.
I used kglad's last code and it didn't quite work(probably has something to do with the fact that mine didn't read getnexthighestdepth() ), so I added a counter and replaced getNextHighestDepth() with i and by some miracle it worked. So all thats left is the hittest. I tried a few forms with no luck. My final attempt can be found below. As with almost every attempt, the first cannonball works, but the rest don't. I'll be very thankful for any help offered.
• ###### 7. Re: Multiple firings
use trace(wall) just above your hitTest() to make sure that's the correct movieclip reference. you should see _level0.path.wall repeatedly when you fire a cannonball. if you see undefined repeatedly, you'll know you have an incorrect path/instance name for wall.
• ###### 8. Re: Multiple firings
Well,the path is correct but the "cannonballs" only effect the wall if the first cannonball hits it. If it does they can all hit it,, if it doesn't they, will all go right through him.
Here's the newer code, all thats really different is the gotoAndStop is changed to removemovieclip and "wall" is changed to my character, "Rover":
• ###### 9. Re: Multiple firings
you should increment your 'hit' variable BEFORE you remove cannonball_fired.
• ###### 10. Re: Multiple firings
Whoops, minor oversight, unfortunately, it didn't solve the problem of the cannonballs being removed and counted if the first one does not hit it's target.
• ###### 11. Re: Multiple firings
you changed my code. your cannonballs all have the same instance name. fix that.