我想要实现的是,当点击牛仔时他会消失。我也尝试过跟踪评论,点击牛仔时没有任何显示。
当我运行程序时,我没有错误。我无法弄清楚为什么。 这是我的代码
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Bitmap;
import flash.ui.Mouse;
import flash.events.MouseEvent;
/**
* ...
* @author Callum Singh
*/
public class Main extends Sprite
{
public var gun:crosshair;
public var cowboy:enemy;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
gun = new crosshair();
stage.addChild (gun);
addEventListener(Event.ENTER_FRAME, moveCrosshair);
cowboy = new enemy();
cowboy.x = Math.random() * 600;
cowboy.y = Math.random() * 400;
stage.addChild (cowboy);
addEventListener(MouseEvent.CLICK, handleShoot);
}
private function moveCrosshair(e:Event):void
{
gun.x = mouseX -120;
gun.y = mouseY -115;
}
private function handleShoot(e:MouseEvent):void
{
if (e.target == cowboy)
{
cowboy.visible = false;
}
}
}
}
答案 0 :(得分:0)
确保将侦听器添加到 cowboy 对象,有时将MouseEvent.CLICK更改为MouseEvent。 MOUSE_DOWN 有帮助。
cowboy.addEventListener(MouseEvent.MOUSE_DOWN, handleShoot);