我对子弹和敌人有一些问题。我不认为我需要解释这么多,只需看看代码。我不是很擅长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 ) );
}
}
}
我的想法是,当子弹击中时,我无法弄清楚我应该如何移除敌人。 请帮忙!
答案 0 :(得分:0)
只需使用removeChild
。
if (b.hitTestObject(Enemy))
{
//**I WANT TO REMOVE ENEMY!!!!**
removeChild(Enemy);
}
答案 1 :(得分: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);
}
您正在移除孩子,然后尝试与孩子一起访问活动。
<强>尝试强>
if (b.x < 0 || b.x > 1024 || b.y < 0 || b.y > 768)
{
b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
removeChild(b);
}