简体   繁体   English

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

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

I have added a few instances of a movieclip (a simple image of an apple) on to the stage at random x and y. 我在舞台上以随机的x和y添加了动画片段(苹果的简单图像)的一些实例。 Now I am trying to click on each one to remove them. 现在,我尝试单击每个按钮将其删除。

Here is what I have: 这是我所拥有的:

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

I get the following error if I try to compile: 如果尝试编译,会出现以下错误:

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

I have also tried substituting e.target.name for e.target. 我也尝试用e.target.name代替e.target。 In this case the program runs, but as soon as I click on the apple, I get the following error in the output log: 在这种情况下,程序将运行,但是,一旦我单击了苹果,我就会在输出日志中得到以下错误:

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

So is there any way to remove the specific object that I am clicking on? 那么,有什么方法可以删除我单击的特定对象?

In your onClick event handler try something like: 在您的onClick事件处理程序中,尝试执行以下操作:

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

You may also want to make sure the event handler is added to the display object using a weak reference, if it is meant to be completely gone/removed on click. 您可能还想确保使用弱引用将事件处理程序添加到显示对象中,前提是单击时该事件处理程序将完全消失/删除。

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

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