Help with bullet detection with enemy
Hi guys,
For some unknown reason whilst coding my shoot em up game, the bullet detection with the enemy will suddenly becomes unfunctional (not detecting bullets) when write additional code that has nothing to do with function CollisionWithEnemies(). This has happened a few times during the development of my game and I've always worked around it, but now its got to a point where I can't that anymore.
Note: This isn't all my code as I have approx 800+ lines of code so far, that would take up too much of your time to read through so I just included the sections that would be related to this issue
The bullet detection/collision is bubbled between 3 class/documents which may be apart of the problem -
Main Class - Main logic and enemy properies of the game
Bullet Class - The bullet properies
Player Class - Player controls but in this case related cause I create the bullet here as I press a button to fire bullet - shootbullet function()
Main Class
Actionscript Code:
public function makeEnemies():void
{
for(var i:int = 0; i < 2 + level; i++) //// This line of code changes the bullet detention from being functional to non-functional
{
var tempEnemy:MovieClip
tempEnemy = new Enemy;
addChild(enemy);
enemies.push(tempEnemy);
}
function checkCollisionWithEnemies(bullet:MovieClip)
{
if (enemy.hitTestPoint(bullet.x, bullet.y,true))
{
if (player.char_type == 1)/// Square Bullets
{
enemy.meter.width -= 10;
enemy.gotoAndStop(2);
removeChild(bullet);
bullet = null;
}
else
{
enemy.gotoAndStop(1);
}
if (player.char_type == 3) /// Circle Bullets
{
enemy.scaleX += 0.1;
enemy.scaleY += 0.1;
enemy.meter.width += 20;
removeChild(bullet);
bullet = null;
}
If i remove that "for(var i:int = 0; i < 2 + level; i++)" bullet detention works fine but hasn't nothing to do with "function checkCollisionWithEnemies(bullet:MovieClip)" which is very odd.
Player Class
Actionscript Code:
private function onMouseDown(event:MouseEvent):void
{
if (! _shotFired)
{
shootBullet();
_shotFired = true;
}
}
private function onMouseUp(event:MouseEvent):void
{
_shotFired = false;
}
private function shootBullet():void
{
{
var bullet_type:String = "square";
if (char_type == 3)
{
bullet_type = "circle";
}
if (char_type == 1)
{
bullet_type = "square";
}
//Bullet's velocity
var bullet_Vx:Number=Math.cos(_angle) * -10;
var bullet_Vy:Number=Math.sin(_angle) * -10;
//Bullet's start position
var radius:int =- 50;
var bullet_StartX:Number = x + radius * Math.cos(_angle);
var bullet_StartY:Number = y + radius * Math.sin(_angle);
//Create bullet instance and add it to the stage
var bullet:Bullet = new Bullet(bullet_Vx, bullet_Vy, bullet_StartX, bullet_StartY, bullet_type);
parent.addChild(bullet);
Bullet Class
Actionscript Code:
private function onEnterFrame(event:Event):void
{
//Rotate bullet
rotation += 20;
//Move bullet
x += _vx;
y += _vy;
//Stage Boundaries:
//Top[/i]
if (x + playerHalfWidth > stage.stageWidth)
{
_vx *= _friction;
_vx *= _bounce;
x = stage.stageWidth - playerHalfWidth;
}
else if (x - playerHalfWidth < 10)
{
_vx *= _friction;
_vx *= _bounce;
x = 10 + playerHalfWidth;
}
if (y - playerHalfHeight < 0)
{
_vy *= _friction;
_vy *= _bounce;
y = 0 + playerHalfHeight;
}
else if (y + playerHalfHeight > stage.stageHeight - 5)
{
_vy *= _friction;
_vy *= _bounce;
y = stage.stageHeight - playerHalfHeight - 5;
}
{
MovieClip(parent).checkCollisionWithEnemies(this);
}
The line "MovieClip(parent).checkCollisionWithEnemies(t his) ;" calls the "function checkCollisionWithEnemies(bullet:MovieClip)" within the Main class I don't understand why this isn't be called, any ideas?
Thank you for your time and reading all that and any help/suggestions would be greatly appreciated.
Thanks
Jonesy