Be a Supporter!

Bullet Collision Problem As2

  • 692 Views
  • 4 Replies
New Topic Respond to this Topic
YoinK
YoinK
  • Member since: Jan. 5, 2001
  • Offline.
Forum Stats
Member
Level 60
Game Developer
Bullet Collision Problem As2 Nov. 29th, 2011 @ 10:28 PM Reply

I got a problem where the bullets won't get detected during the collision code.... The code to shoot the bullets works fine.. BUT it won't check collision... my guess for the problem would be the variable that declares bulletID.... but if i switch it to an array the collision works BUT.. it screws up the shooting aspect......
(below is the shooting code)

////////////shooting gun TEST
	if (Key.isDown(65)&& _root.weapon =="gun" &&(_root.shooting == false) && _root.attack ==true) {
		if (_root.player.direction_right == false) {
		var bulletID:Number = Math.random();
		//create a variable that we'll use at the bullet's id
		//then attach a bullet to the stage
		_root.attachMovie('mcBullet', 'Bullet'+bulletID, _root.getNextHighestDepth());
		//setting the coordinates of the bullet to be the same as the main character
		_root['Bullet'+bulletID]._x = player._x-90*(_root.player._yscale/100);
		_root['Bullet'+bulletID]._y = player._y-172*(_root.player._yscale/100);
		_root['Bullet'+bulletID].onEnterFrame = function() {
			//giving the bullet some actions
			this._x -= bulletspeed;
			//moving the bullet
			if (this._x<-1) {
				//if the bullet goes off stage
				//then destroy it
				this.removeMovieClip();
			}}
		}
		
		
		if (_root.player.direction_right == true) {
		var bulletID:Number = Math.random();
		//create a variable that we'll use at the bullet's id
		//then attach a bullet to the stage
		_root.attachMovie('mcBullet', 'Bullet'+bulletID, _root.getNextHighestDepth());
		//setting the coordinates of the bullet to be the same as the main character
		_root['Bullet'+bulletID]._x = player._x+90*(_root.player._yscale/100);
		_root['Bullet'+bulletID]._y = player._y-172*(_root.player._yscale/100);
		_root['Bullet'+bulletID].onEnterFrame = function() {
			//giving the bullet some actions
			this._x += bulletspeed;
			//moving the bullet
			
			
			if (this._x>900) {
				//if the bullet goes off stage
				//then destroy it
				this.removeMovieClip();
			}}
		}
		
		
		
		
		
	}

(below is the collision code)

onClipEvent (enterFrame) {
	
		if (this.hitTest(_root["Bullet"+_root.bulletID])) {
			trace("check collision");
		}
	
	
}

Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!

Jin
Jin
  • Member since: Sep. 9, 2006
  • Online!
Forum Stats
Supporter
Level 49
Blank Slate
Response to Bullet Collision Problem As2 Nov. 30th, 2011 @ 12:55 AM Reply

You are only checking for collision with a single bullet. You'll either need to give the bullets the collision function, and check it with an array of targets, or give the targets the collision function to check them with an array of bullets. I recommend the latter.

Firstly, don't use random numbers to determine bullet ID. It's quite easy to do it with an array if you know some simple array methods/functions.

//Add a bullet reference into array, used when creating bullet.
_arrBullets.push(newBullet);

//Give bullet 'cleaning' method, destroys it when it goes out of stage area
newBullet.onEnterFrame = checkBorders(newBullet);

function checkBorders(target:MovieClip):void {
	if (out of stage area) {
		//Remove from array.
		var index:int = _arrBullets.indexOf(target);
		_arrBullets.splice(index,1);
		//Remaining remove code.
		//blabla
	}
}

//Give your character or bullet targets the collision script
this.onEnterFrame = checkCollision(this);

function checkCollision(target:MovieClip):void {
	for (var i:int =0; _arrBullets.length; i++) {
		if (collide) {
			//Remove from array.
			var index:int = _arrBullets.indexOf(i);
			_arrBullets.splice(index,1);
			//Remaining remove code.
			//blabla
		}
	}
}

//Obviously you can move the removal code into a function for reusing, but that'll require you to have classes. Also, _arrBullets should be global.

I haven't done AS2 in a long time but that's most what you should do logically. Take it as reference.


BBS Signature
Jin
Jin
  • Member since: Sep. 9, 2006
  • Online!
Forum Stats
Supporter
Level 49
Blank Slate
Response to Bullet Collision Problem As2 Nov. 30th, 2011 @ 01:01 AM Reply

Oops. I copy pasted my first block of code. It should be like this.

function checkCollision(target:MovieClip):void {
	for (var i:int =0; _arrBullets.length; i++) {
		if (collide) {
			//Remove from array.
			_arrBullets.splice(i,1);
			//Remaining remove code.
			//blabla
		}
	}
}

BBS Signature
YoinK
YoinK
  • Member since: Jan. 5, 2001
  • Offline.
Forum Stats
Member
Level 60
Game Developer
Response to Bullet Collision Problem As2 Dec. 1st, 2011 @ 01:13 AM Reply

the combination of arrays and functions seem to work... BUT.. at the moment when i shoot right and then i change the direction of my character to left... the bullet moves from moving right to moving left..... :(


Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!

YoinK
YoinK
  • Member since: Jan. 5, 2001
  • Offline.
Forum Stats
Member
Level 60
Game Developer
Response to Bullet Collision Problem As2 Dec. 1st, 2011 @ 03:02 AM Reply

nevermind! i figured it out. one array for each direction works perfectly. :D


Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!