1

I having some problem with bullet and enemy. I don't think i need to explain so much, just take a look at the code. Im not very good at AS3, im new and learning so I need help :P

Ok, this is on the flash/stage timeline. Here I say if I press mouse a bullet should be created.

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

function mouseDown(pEvent)
{
    // Create a new bullet
    var b = new Bullet();
    // Set his position to the tank position
    b.x = Player.x;
    b.y = Player.y;
    // Save the randian angle between the mouse and the tank
    // This angle will set the direction of the bullet
    b.angleRadian = Math.atan2(AIM.y - Player.y,AIM.x - Player.x);
    // Add an enter frame event on each bullet
    b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    // Add this display object on the display list
    addChild(b);
}

// Velocity of each bullet
var speed = 8;

function bulletEnterFrame(pEvent)
{
    // Get the current object (Bullet)
    var b = pEvent.currentTarget;
    // Move this bullet on each frames
    // On X axis use the cosinus angle
    b.x +=  Math.cos(b.angleRadian) * speed;
    // On Y axis use the sinus angle
    b.y +=  Math.sin(b.angleRadian) * speed;
    // Orient the bullet to the direction
    b.rotation = b.angleRadian * 180 / Math.PI;
    // You have to remove each created bullet 
    // So after every moves you must check bullet position
    // If the bullet is out of the screen
    if (b.x < 0 || b.x > 1024 || b.y < 0 || b.y > 768)
    {
        // Remove it from the display list
        removeChild(b);
        // /!\ AND REOMOVE HIS EVENT LISTER
        b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    }

    if (b.hitTestObject(Enemy))
    {
        **I WANT TO REMOVE ENEMY!!!!**
    }
}

OK. And on timeline i also create enemys. Like this:

var Enemy:MovieClip = new AI(stage);
addChild(Enemy);

And the enemyclass looks like this:

package 
{

import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;

public class AI extends MovieClip
{

    var speed:Number = 1;
    var distance:Number;

    public function AI(stage):void
    {


        addEventListener(Event.ENTER_FRAME, onadd);

    }

    public function onadd(e:Event):void
    {
        addEventListener(Event.ENTER_FRAME, loop);
    }

    private function loop(e:Event):void
    {

        var Player = MovieClip(root).Player;

        var yDistance:Number = Player.y - y;
        var xDistance:Number = Player.x - x;

        if (Math.sqrt(yDistance*yDistance +  xDistance*xDistance) < speed)
        {
            x = Player.x;
            y = Player.y;
        }
        else
        {
            var radian:Number = Math.atan2(yDistance,xDistance);
            x +=  Math.cos(radian) * speed;
            y +=  Math.sin(radian) * speed;
            rotation = radian * 180 / Math.PI;
        }

        if (this.hitTestObject(Player))
        {
            trace("DEAD");
        }

        //distance = Math.sqrt( ( MovieClip(root).Player.x - this.x ) * ( MovieClip(root).Player.x - this.x ) + ( MovieClip(root).Player.y - this.y ) * ( MovieClip(root).Player.y - this.y ) );

    }

}

}

The think is that I can't figure out how I should remove enemy when bullet hits hit. Please help!

shanethehat
  • 15,460
  • 11
  • 57
  • 87
user1133188
  • 71
  • 4
  • 12
  • Just make `parent.removeChild(this)` to remove him. – Fabricio Jan 09 '12 at 12:23
  • I get this error then: TypeError: Error #1034: Type Coercion failed: cannot convert global@2ac9f29 to flash.display.DisplayObject. at Function/SpaceDefend_fla:MainTimeline/Spawn_Enemy/SpaceDefend_fla:bulletEnterFrame()[SpaceDefend_fla.MainTimeline::frame1:119] – user1133188 Jan 09 '12 at 20:20

2 Answers2

0
if (b.x < 0 || b.x > 1024 || b.y < 0 || b.y > 768)
{
    // Remove it from the display list
    removeChild(b);
    // /!\ AND REOMOVE HIS EVENT LISTER
    b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
}

You are removing the child, then trying to access an event with the child.

Try

if (b.x < 0 || b.x > 1024 || b.y < 0 || b.y > 768)
{ 
    b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    removeChild(b);
}
Marko
  • 20,385
  • 13
  • 48
  • 64
Ryan
  • 1
0

Just use removeChild.

if (b.hitTestObject(Enemy))
{
    //**I WANT TO REMOVE ENEMY!!!!**
    removeChild(Enemy);
}
Cadin
  • 4,275
  • 1
  • 14
  • 22
  • I get this error then: TypeError: Error #1009: Cannot access a property or method of a null object reference. at AI/loop()[C:\Users\MYNAME\Desktop\Project SpaceDefend\AI.as:30] – user1133188 Jan 09 '12 at 20:18
  • you might want to check `if(getChildByName("Enemy")!=null){removeChild(Enemy);}` Alternatively you can wrap the removeChild code in a try catch block. – ToddBFisher Jan 10 '12 at 03:35
  • The first code you wrote didn't work...Still get's TypeError: Error #1009: Cannot access a property or method of a null object reference. What do you mean with wrap the removeChild in a try catch block? – user1133188 Jan 10 '12 at 16:49
  • That error is coming from the `loop` method your AI class. Most likely because you are trying to access the stage after the instance has already been removed from the stage (a 'null object reference'). You need to tell the Enemy to stop running the loop method before you remove it from the stage. – Cadin Jan 10 '12 at 17:46
  • Oh, ok, sounds like that's the problem actually. Any ideas how I can do that? – user1133188 Jan 10 '12 at 19:30
  • Remove the ENTER_FRAME event. – Cadin Jan 10 '12 at 21:52
  • Yeah, i understood that. But how i meant to ask? But i figure it out, maybe a "noob" solution but it works... I did this: if (b.hitTestObject(Enemy)) { and then said in the AI.as that if this.x < -200, then remove this.parent.removechild(this) and then I removed the loop to! Thanks for the help! :D Enemy.x = -200; } – user1133188 Jan 11 '12 at 15:07