【问题标题】:ActionScript: How to Re- setInterval Each Time Function Works?ActionScript:如何在每个 Time 函数起作用时重置Interval?
【发布时间】:2012-04-03 16:57:27
【问题描述】:

我有一个在屏幕上绘制矩形的函数(请参阅 createInfoPanel())
在绘制矩形时,我在其上添加了 2 个文本字段。
但正如您可能猜到的那样,它会立即添加这些内容。
我想延迟添加这些文本字段,然后我想在一段时间后删除这些面板。
问题是,当我设置间隔或计时器时,它们在我使用一次后将无法工作(我必须通过清除/删除来停止它们,它没有再次设置它们)。
由于每次图像更改时都会创建我的面板,因此每次图像更改时我都需要它们工作。

所以,我有两个问题:
1- 每次我的 createInfoPanel() 函数工作时如何重新设置间隔?设置和澄清一次后将不再起作用。

2- 你可以看到 infoPanel.addChild(titleField); addInfoPanel() 函数中的行。我怎样才能在这里制作流畅的动画?我的意思是,文字出现缓慢?

提前致谢。

public class ImageRotator extends Sprite
{
private var ... ; //Some variables

public function ImageRotator(xmlPath:String = "images.xml", interval:int = 8301):void
{
    timer = new Timer(interval);
    loadXML(xmlPath);
}

private function loadXML(file:String):void
{
    urlLoader = new URLLoader(new URLRequest(file));
    urlLoader.addEventListener(Event.COMPLETE, parseXML);
}

private function parseXML(e:Event):void
{
    xml = new XML(e.target.data);
    loadImages();
}

private function loadImages():void
{
    for (var i:int = 0; i < xml.children().length(); i++)
    {
        var loader:Loader = new Loader();
        loader.load(new URLRequest(xml.children()[i].@src));
        imagesVector.push(loader);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sortImages);
    }
}

private function sortImages(e:Event):void
{
    imagesCounter++;

    for (var i:int = 0; i < imagesVector.length; i++)
    {
        imagesVector.reverse();
        addChild(imagesVector[i]);
    }

    //I have only 3 images, I needed to set indexes because
    //they were covering each other
    this.setChildIndex(imagesVector[2], 0);
    this.setChildIndex(imagesVector[1], 0);
    this.setChildIndex(imagesVector[0], 0);

    if (imagesCounter == imagesVector.length)
    {
        createInfoPanel();
        timer.addEventListener(TimerEvent.TIMER, autoChange);
        timer.start();
    }

}

private function createInfoPanel():void
{
    infoPanel.graphics.beginFill(0x000000, 0.0);
    infoPanel.graphics.drawRect(0, 0, 967, 138);
    infoPanel.graphics.endFill();

////Here I want to run addInfoPanel() function with 2 seconds delay,
////After it starts, I want to run removeInfoPanel() function with 2 more seconds delay

    addChild(infoPanel);

}

private function addInfoPanel():void {
    titleField.text = xml.children()[infoCounter]. @ title;
    titleField.x = 425;
    titleField.y = 0;

    description.text = xml.children()[infoCounter]. @ description;
    description.x = 427;
    description.y = 26;

    infoPanel.y = 300;
    infoPanel.addChild(titleField);
    infoPanel.addChild(description);
}

private function removeInfoPanel():void {

    infoPanel.removeChild(titleField);
    infoPanel.removeChild(description);
}

private function addActions():void
{
    //Some function
}

private function changeImage(e:MouseEvent):void
{
    //Image changing function
}

private function changeDepth(e:TweenEvent):void
{
    //Some function
}

private function autoChange(e:TimerEvent):void
{
    //Some function
}
}

编辑:我过去是如何处理间隔的:

private function createInfoPanel():void
{
    //lines above code sample
    intervalInfoPanel = setInterval(addInfoPanel,2000);

    addChild(infoPanel);
}

private function addInfoPanel():void {
    //lines above code sample
    clearInterval(intervalInfoPanel);
    intervalInfoPanelRemove = setInterval(removeInfoPanel,3500);
}

private function removeInfoPanel():void {
    //lines above code sample
    clearInterval(intervalInfoPanelRemove);
}

【问题讨论】:

    标签: actionscript-3 flash animation actionscript


    【解决方案1】:

    1- 如何在每次我的 createInfoPanel() 函数时重新设置间隔 作品?设置和清理一次后就不行了。

    您究竟是如何重置间隔的?您没有在此处显示代码。
    但通常你可以像这样重置 + 重新使用计时器:

    timer.reset();
    

    这将停止并将计时器的 currentCount 重置为 0。
    然后你可以稍后说timer.start();,一切都应该像以前从未运行过一样工作。

    2- 你可以看到 infoPanel.addChild(titleField); addInfoPanel() 中的行 功能。我怎样才能在这里制作流畅的动画?我的意思是,出现文字 慢慢来?

    使用补间。添加带有txt.alpha = 0; 的TextFields,然后缓慢地将alpha 值补间到1.0。 TweenLite (http://www.greensock.com/tweenlite/) 是一个很棒的补间引擎。

    import com.greensock.*;
    
    txt.alpha = 0.0;
    TweenLite.to(txt, 1, {alpha:1.0, delay:0.4});
    

    【讨论】:

    • 我的错,我需要像这样重置:清除/删除并再次工作。因为如果我重置,无论我是否愿意,它都会再次工作。我想完全停止它,然后在 createInfoPanel() 函数中重新开始。当我清除时,它不会再次工作。
    • 亲爱的@pkyeck,我添加了代码示例,显示了我之前的工作间隔。如您所见,我清除它们是为了拒绝再次工作,但我每次都在 createInfoPanel() 中重新设置它们,问题是清除一次后它们将无法工作。
    • setInterval()clearInterval()Timer 类无关。为什么你使用两种不同的方法?如果你需要的话,你可以让Timer 只运行一次。 new Timer(1000, 1);
    • 好的,但我用计时器尝试了同样的方法。问题是我需要计时器来延迟我的文字出现。你的代码只工作一个计时器,它会如何延迟我的文字出现?
    • 为此我建议使用 TweenLite:greensock.com/get-started-tweening
    猜你喜欢
    • 2020-07-14
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多