AS3 删除子项。子弹与敌人?

发布于 2024-12-25 10:43:19 字数 3133 浏览 1 评论 0原文

我在子弹和敌人方面遇到了一些问题。我觉得不需要解释那么多,直接看代码就可以了。我不太擅长 AS3,我是新手,正在学习,所以我需要帮助 :P

好的,这是在 flash/stage 时间轴上。在这里我说如果我按下鼠标就应该创建一个项目符号。

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!!!!**
    }
}

好的。在时间线上我也创造了敌人。像这样:

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

敌人类看起来像这样:

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 ) );

    }

}

}

我想的是,我不知道当子弹击中时我应该如何移除敌人。 请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2025-01-01 10:43:19

只需使用removeChild即可。

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

Just use removeChild.

if (b.hitTestObject(Enemy))
{
    //**I WANT TO REMOVE ENEMY!!!!**
    removeChild(Enemy);
}
溺ぐ爱和你が 2025-01-01 10:43:19
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.x < 0 || b.x > 1024 || b.y < 0 || b.y > 768)
{ 
    b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    removeChild(b);
}
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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文