0

我从 Flash 中的 AS2 慢慢变得对 AS3 更有经验。

有什么方法可以将函数添加到某种类型的已创建对象(可能是 Shape 或 MovieClip)?

过去,我使用过:

var testshape = _root.createEmptyMovieClip();
testshape.onEnterFrame = function():Void
{
    testshape._alpha -= 10;
    if(testshape._alpha < 20)
    {
        testshape.removeMovieClip();
    }
}

我可以在 AS3 中做类似的事情吗?

可能使用 eventListeners 或类似的东西。

目前我有它:

var testshape:MovieClip = new MovieClip();
testshape.graphics.beginFill(0xFF0000); 
testshape.graphics.drawCircle(300,300,50);
testshape.graphics.endFill();
testshape.addEventListener(Event.ENTER_FRAME, function runEvent():void
{
    testshape.alpha -= 0.1;
    if(testshape.alpha < 0.2)
    {           
        testshape.removeEventListener(Event.ENTER_FRAME,runEvent);
        removeChild(testshape);
    }
});
addChild(testshape);

谢谢

4

1 回答 1

2

请参阅以下代码。

var testShape:MovieClip = new MovieClip();
addChild(testShape);
testShape.addEventListener(Event.ENTER_FRAME, function():void{
    testShape.alpha -= 0.1;
    if(testShape.alpha < 0.2)
    {
        removeChild(testShape);
    }
});

已编辑

试试这个:

var testShape:MovieClip = new MovieClip();
testShape.graphics.beginFill(0xff0000);
testShape.graphics.drawCircle(100,100,100);
testShape.graphics.endFill();
addChild(testShape);
testShape.addEventListener(Event.ENTER_FRAME, onEnter);

function onEnter(e:Event):void
{
    testShape.alpha -= 0.1;
    if(testShape.alpha < 0.2)
    {
        testShape.removeEventListener(Event.ENTER_FRAME, onEnter);
        removeChild(testShape);
    }
};
于 2013-02-27T14:01:00.440 回答