【问题标题】:AS3 RemoveChild. Bullet and Enemy?AS3 RemoveChild。子弹和敌人?
【发布时间】:2012-01-09 11:08:13
【问题描述】:

我对子弹和敌人有些问题。我觉得不需要解释这么多,看代码就行了。我不太擅长 AS3,我是新手,正在学习,所以我需要帮助:P

好的,这是在快闪/舞台时间线上。在这里我说如果我按下鼠标应该创建一个子弹。

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

    }

}

}

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

【问题讨论】:

  • 只需让parent.removeChild(this) 删除他。
  • 然后我收到此错误:TypeError: Error #1034: Type Coercion failed: cannot convert global@2ac9f29 to flash.display.DisplayObject。在 Function/SpaceDefend_fla:MainTimeline/Spawn_Enemy/SpaceDefend_fla:bulletEnterFrame()[SpaceDefend_fla.MainTimeline::frame1:119]

标签: actionscript-3 hittest removechild


【解决方案1】:

只需使用removeChild

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

【讨论】:

  • 然后我收到此错误:TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 AI/loop()[C:\Users\MYNAME\Desktop\Project SpaceDefend\AI.as:30]
  • 您可能需要检查if(getChildByName("Enemy")!=null){removeChild(Enemy);} 或者您可以将 removeChild 代码包装在 try catch 块中。
  • 您编写的第一个代码不起作用...仍然出现类型错误:错误 #1009:无法访问空对象引用的属性或方法。将 removeChild 包装在 try catch 块中是什么意思?
  • 该错误来自您的 AI 类的 loop 方法。很可能是因为您在实例已从阶段中删除后尝试访问阶段(“空对象引用”)。在将循环方法从舞台上移除之前,您需要告诉敌人停止运行循环方法。
  • 哦,好吧,听起来确实是这个问题。有什么想法可以做到吗?
【解决方案2】:
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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多