繁体   English   中英

在Flash AS3中单击时如何从舞台上删除特定符号?

[英]How to remove a specific symbol from stage when clicking on it in Flash AS3?

我在舞台上以随机的x和y添加了动画片段(苹果的简单图像)的一些实例。 现在,我尝试单击每个按钮将其删除。

这是我所拥有的:

public function Apples() {

        for(var count:int=1; count<=10; count++){
            var apple = new Apple();
            apple.x = Math.random() * stage.stageWidth;
            apple.y = Math.random() * stage.stageHeight;    
            apple.name = count;
            stage.addChild(apple);
            }

stage.addEventListener(MouseEvent.CLICK, onClick);



        function onClick(e:MouseEvent):void{
            stage.removeChild(e.target);
            }
}

如果尝试编译,会出现以下错误:

1118: Implicit coercion of a value with static type Object to a possibly unrelated  type flash.display:DisplayObject.

我也尝试用e.target.name代替e.target。 在这种情况下,程序将运行,但是,一旦我单击了苹果,我就会在输出日志中得到以下错误:

TypeError: Error #1034: Type Coercion failed: cannot convert "9" to flash.display.DisplayObject.
at MethodInfo-4()

那么,有什么方法可以删除我单击的特定对象?

在您的onClick事件处理程序中,尝试执行以下操作:

var displayObject:DisplayObject = (DisplayObject) (e.target);
displayObject.parent.removeChild(displayObject);

您可能还想确保使用弱引用将事件处理程序添加到显示对象中,前提是单击时该事件处理程序将完全消失/删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM