我在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);
由于
答案 0 :(得分: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);
}
};