View Full Version : bullet handling problem
suvenier
May 22nd, 2007, 02:26 PM
hI,
I have a problem in my games's code.
As it is a space game, the user controls a ship and fires bullets.
So, every time you fire a bullet:
if (Key.isDown(Key.SPACE) && okShoot) {
okShoot = false;
var newDepth = depth++;
var name = "bullet_"+newDepth;
var clip = _root.attachMovie("bullet", name, newDepth);
clip._x = ship._x;
clip._y = ship._y;
//(plus rest of the code that enables the bullet to move)
}
its ok for now(this code works well).
But when I need to check if that bullet has collided with a target(wich is random generated each time you change level), i canīt get the bullets name to pass to the colision function(because I may have already fired hundreds of bullets).
I donīt want to use a "FOR LOOP", because it slows down the game when you have already fired a lot of bullets.
does any one knows a better way for correcting this code?
thanks for reading.:book:
Warheart
May 22nd, 2007, 10:02 PM
hI,
I have a problem in my games's code.
As it is a space game, the user controls a ship and fires bullets.
So, every time you fire a bullet:
if (Key.isDown(Key.SPACE) && okShoot) {
okShoot = false;
var newDepth = depth++;
var name = "bullet_"+newDepth;
var clip = _root.attachMovie("bullet", name, newDepth);
clip._x = ship._x;
clip._y = ship._y;
//(plus rest of the code that enables the bullet to move)
}
its ok for now(this code works well).
But when I need to check if that bullet has collided with a target(wich is random generated each time you change level), i canīt get the bullets name to pass to the colision function(because I may have already fired hundreds of bullets).
I donīt want to use a "FOR LOOP", because it slows down the game when you have already fired a lot of bullets.
does any one knows a better way for correcting this code?
thanks for reading.:book:
Could you not just have an if statement checking a hittest between the target and the "bullet_"+newDepth movieclip? the newDepth should always be the current bullet fired.
irrationalistic
May 23rd, 2007, 12:00 AM
The problem with using the for loop might be avoidable in this situation. If your bullets are firing fast enough, you could apply an onEnterFrame to your target object and have it check either every other bullet, or some similar increment to "for(var i = 0; i < numBullets; i+=2)". That way, you check half the number of bullets fired.
Another way, which would probably be better, is to set up a variable that has the first bullet fired. The target will always check this one specific bullet and, if it finds a collision, do whatever you need to do to the bullet and the health of the target, then just re-assign the firstBullet variable. If you give each bullet an incremental name, such as "b1", "b2", etc, you could easily use AS to decide the next bullet fired in the stream.
Hopefully that helps, I suppose it is basically what Warheart said...
ajcates
May 23rd, 2007, 12:23 AM
make an array
when you make a bullet add it to the array
when a bullet leaves the stage subtract it form the array
in an onEnterFrame event loop thur the array and hit test it against stuff
I have made a space shooter game where i have implemented this if you would like to see it tell me
suvenier
May 23rd, 2007, 12:44 PM
thx. I prefer h080j03's answer. Warheart your technike doesn't apply because in my game you may fire hundreds of bullets in a lot of directions and there maybe a lot of "current bullets".
Iīll use an array of "current bullets" then.
thx.
ajcates
May 23rd, 2007, 01:01 PM
yes check the useful scripts thread in the action script forum for some good array functions that will probably help you
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.