Problem Spawning Bullets

Subscribe to Problem Spawning Bullets 8 posts, 4 voices

Sign in to reply


 
avatar for L3viath3nt L3viath3nt 116 posts
Flag Post

Get ready for a hard one here. I’m making a Contra-esque shooter, and like Contra, you can shoot in eight directions, up, down, up-right, etc. Anyway, I can get the bullet to spawn in 7 of the 8 directions, except up and right, and I’m completely lost as to the reason. Here’s my code and see if you can fix it.

//function that positions the bullet
function bullSpawn(bullet) {
	bullet._x += _x;
	bullet._x += _root.Buster._x;
	bullet._x += spawnZones[_root.Buster.zone][0] * _root.Buster.dirRay[_root.Buster.dir][1];
	bullet._y += _y;
	bullet._y += _root.Buster._y;
	bullet._y += _root.Buster.upperBody._y;
	bullet._y += spawnZones[_root.Buster.zone][1];
	bullet._rotation = _root.Buster.zone * 45 * _root.Buster.dirRay[_root.Buster.dir][1];
}
//positions of the bullet at various zones
spawnZones = [[ -7.5, -18.5],
			  [ 17.8, -18.4],
			  [ 18.5,  -7.5],
			  [ 28.4,   7.8],
			  [  7.5,  18.5]];
//function call
function onEnterFrame() {
	if (Key.isDown(75) && shot == false) {
		var bullet = _root.attachMovie(bulletType+"Bullet", bulletType+"Bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
		bullSpawn(bullet);
		shot = true;
	} else if (!Key.isDown(75)) {
		shot = false;
	}
}

Also, the zones go 0 at top, 1 at up right, 2 at right, 3 at down right, and 4 at down. dir is -1 when facing left and 1 when facing right.

 
avatar for UnknownGuardian UnknownGuardian 1701 posts
Flag Post

How come you only have 5 spawn zones?

 
avatar for L3viath3nt L3viath3nt 116 posts
Flag Post
_root.Buster.dirRay[_root.Buster.dir][1]

This refers to -1 when the character is facing left and 1 when facing right. The _y position is the same regardless of which way he is facing and the _x is the opposite, so I removed some extra weight by just multiplying whatever part of SpawnZones is called by multiplying it by dir.
(I really have 10 zones as he can be looking left or right when shooting upwards.)

 
avatar for Cervello Cervello 76 posts
Flag Post

The only advice I can give right now is to use an integer instead of a multidimensional array for the player direction (I have no idea why you need to access a multidimensional array with _root.Buster.dirRay[_root.Buster.dir][1]; for this).

Example:


function bullSpawn(bullet) { //assuming _root.Buster.dir is an int either 1 or -1.
	bullet._x = (_x + _root.Buster._x + spawnZones[_root.Buster.zone][0] * _root.Buster.dir); 
	bullet._y = (_y + _root.Buster._y + _root.Buster.upperBody._y + spawnZone[_root.Buster.zone][1]);
	bullet._rotation = _root.Buster.zone * 45 * _root.Buster.dir;
	//trace statements may help
	trace("spawnZone coordinates: (" + (spawnZones[_root.Buster.zone][0] * _root.Buster.dir) + ", " + (spawnZone[_root.Buster.zone][1]));
	trace("rotation: " + bullet._rotation);
}

I really can’t help you with your problem with just a snippet of code and no description as to exactly what kind of problem it’s causing. (Does it fire in the wrong direction? Does it not fire at all? Does it fire and not move? Have you tried debugging, or at least trace statements to try to figure out what went wrong where?)

 
avatar for UnknownGuardian UnknownGuardian 1701 posts
Flag Post
Originally posted by Cervello:

The only advice I can give right now is to use an integer instead of a multidimensional array for the player direction (I have no idea why you need to access a multidimensional array with _root.Buster.dirRay[_root.Buster.dir][1]; for this).

Example:


function bullSpawn(bullet) { //assuming _root.Buster.dir is an int either 1 or -1.
	bullet._x = (_x + _root.Buster._x + spawnZones[_root.Buster.zone][0] * _root.Buster.dir); 
	bullet._y = (_y + _root.Buster._y + _root.Buster.upperBody._y + spawnZone[_root.Buster.zone][1]);
	bullet._rotation = _root.Buster.zone * 45 * _root.Buster.dir;
	//trace statements may help
	trace("spawnZone coordinates: (" + (spawnZones[_root.Buster.zone][0] * _root.Buster.dir) + ", " + (spawnZone[_root.Buster.zone][1]));
	trace("rotation: " + bullet._rotation);
}

I really can’t help you with your problem with just a snippet of code and no description as to exactly what kind of problem it’s causing. (Does it fire in the wrong direction? Does it not fire at all? Does it fire and not move? Have you tried debugging, or at least trace statements to try to figure out what went wrong where?)

It clearly says that it only fires in 7 of the 8 wanted directions.

 
avatar for L3viath3nt L3viath3nt 116 posts
Flag Post

Well, Unknown is right in that that is the problem. After using some traces I’ve determined that it doesn’t even attach a movieclip when going up and right, though all other directions work perfectly fine.
Anyway, I have found that strings are needed when it comes to animating the character, but integers are needed for the actually movement and such, so that is why I need a multidimensional array. (root.Buster.dir is actually 0 when facing left and 1 when facing right so by using it like this root.Buster.dirRay[_root.Buster.dir] I can add a 0 or 1 afterward, depending on whether I need a string or integer. This is root.Buster.dirRay’s definition:

dirRay = [["left", -1],
             ["right", 1]];
 
avatar for Cervello Cervello 76 posts
Flag Post
Originally posted by L3viath3nt:

Well, Unknown is right in that that is the problem. After using some traces I’ve determined that it doesn’t even attach a movieclip when going up and right, though all other directions work perfectly fine.

Anyway, I have found that strings are needed when it comes to animating the character, but integers are needed for the actually movement and such, so that is why I need a multidimensional array. (root.Buster.dir is actually 0 when facing left and 1 when facing right so by using it like this root.Buster.dirRay[_root.Buster.dir] I can add a 0 or 1 afterward, depending on whether I need a string or integer. This is root.Buster.dirRay’s definition:

dirRay = [["left", -1],
             ["right", 1]];

Gotcha.

That’s strange, though. I don’t see any reason in the actual create/attach code that would prevent specific directions from working. Maybe you shouldn’t call _root.getNextHighestDepth() twice when you use attachMovie, since it may be sticking objects named “1Bullet23” at depth 24, though it’s hard to imagine how that could be a problem right now.

var depth:Number = _root.getNextHighestDepth();
var bullet = _root.attachMovie(bulletType+"Bullet", bulletType+"Bullet"+depth, depth);

It may not help with your current problem, but it’s better for performance and may avoid future trouble, so you might as well try it.

I’m sorry, but so far I can’t find anything else that could possibly be wrong with the code you posted. Maybe you’re not setting _root.Buster.zone properly? If you already have code for the bullet that kills it or hides it when it leaves the screen and other stuff, are you sure it’s not being triggered somehow?

 
avatar for Draco18s Draco18s 2335 posts
Flag Post

Maybe you shouldn’t call _root.getNextHighestDepth() twice when you use attachMovie, since it may be sticking objects named “1Bullet23” at depth 24, though it’s hard to imagine how that could be a problem right now.

That’s completely irrelevant. If you call _root.getNextHighestDepth() four times before attaching a clip, all four times it will return the same value.

Sign in to reply


Click Here