AS3 XML 产品组合:图像彼此堆叠而不是终止

发布于 2024-10-17 03:38:28 字数 4341 浏览 3 评论 0原文

我最近开始学习 AS3,并决定用它制作一个作品集网站。我正在加载有关我从外部 XML 文件创建的网站的信息。

一切都运行良好,但是当我将新图片加载到舞台时,前一张图片仍保留在那里,根据我的理解,这种情况不应该发生。正如我从 Adob​​e 的 AS3 指南中了解到的那样,一旦创建了加载程序的新实例,就应该删除前一个实例。

我一直在到处寻找解决方案,但找不到可行的解决方案。我尝试使用 .unload 类,也尝试了removeChild,但它一直给我错误。

这是我一直在使用的代码:

import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.filters.*;
import flash.display.MovieClip;
import flash.events.MouseEvent; 

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("portfolio.xml"));

function LoadXML(e:Event):void {
    xmlData = new XML(e.target.data);
    ParsePortfolio(xmlData);
}

function ParsePortfolio(siteInput:XML):void {

    var titleList:XMLList = siteInput.Site.title;
    var servicesList:XMLList = siteInput.Site.services;
    var urlList:XMLList = siteInput.Site.url;
    var picList:XMLList = siteInput.Site.picture;

    var XX:Number = 0;

    var titleElement:XML = titleList[XX];
    var servicesElement:XML = servicesList[XX];
    var urlElement:XML = urlList[XX];   
    var picElement:XML = picList[XX];

    container_mc.title_txt.text = (titleElement);
    container_mc.services_txt.text = (servicesElement);

    var pictureNumber:String = picElement;
    var my_loader:Loader = new Loader();
    my_loader.load(new URLRequest(pictureNumber));
    container_mc.pictureFrame.addChild(my_loader);  

    next.addEventListener (MouseEvent.CLICK, NbuttonClicked);
    prev.addEventListener (MouseEvent.CLICK, PbuttonClicked);

    function NbuttonClicked(e:Event):void  {

        var t1:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 1, 0, .2, true);

        t1.addEventListener(TweenEvent.MOTION_FINISH, dropbomb);        

        function dropbomb() {       

            XX++;

            if (XX == titleList.length()) {
            XX = 0;
            }

            var titleElement:XML = titleList[XX];
            var servicesElement:XML = servicesList[XX];
            var urlElement:XML = urlList[XX];
            var picElement:XML = picList[XX];

            container_mc.title_txt.text = titleElement;
            container_mc.services_txt.text = servicesElement;
            var pictureNumber:String = picElement;

            trace(pictureNumber);

            var my_loader:Loader = new Loader();
            my_loader.load(new URLRequest(pictureNumber));
            container_mc.pictureFrame.addChild(my_loader);              

            visit.addEventListener (MouseEvent.CLICK, VbuttonClicked);

            function VbuttonClicked(e:Event):void  {

            navigateToURL(new URLRequest(urlElement), "_blank");

            }

            var t2:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 0, 1, .2, true);
        }
    }

    function PbuttonClicked(e:Event):void  {

        var t3:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 1, 0, .2, true);

        t3.addEventListener(TweenEvent.MOTION_FINISH, dropbomb);

        function dropbomb() {

            if ( XX == 0 )
            {
            XX = (titleList.length()) - 1;
            }

            else {XX--;}

            var titleElement:XML = titleList[XX];
            var servicesElement:XML = servicesList[XX];
            var urlElement:XML = urlList[XX];
            var picElement:XML = picList[XX];           

            container_mc.title_txt.text = titleElement;
            container_mc.services_txt.text = servicesElement;
            var pictureNumber:String = picElement;

            var my_loader:Loader = new Loader();
            my_loader.load(new URLRequest(pictureNumber));

            visit.addEventListener (MouseEvent.CLICK, VbuttonClicked);

            function VbuttonClicked(e:Event):void  {

            navigateToURL(new URLRequest(urlElement), "_blank");

            }           

            var t4:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 0, 1, .2, true);

        }   
    }
}

影片剪辑被手动添加到舞台上。我不知道这是否导致了问题,或者是否完全无关。

更具体地说,当 container_mc 淡出时,您可以看到上一张图片,其中显示它们正在堆叠而不是被替换。

我真的很感激一些帮助,至少我想知道我做错了什么并指出正确的方向。

我对这些菜鸟代码和愚蠢的问题感到抱歉,但我确实仔细寻找了答案,我也寻找过有关创建 XML 幻灯片的教程,但到目前为止没有任何帮助。

i recently started to learn AS3 and i decided to make a portfolio website with it. I am loading the information about the websites i created from an external XML file.

Everything its working well but when i load a new picture to the stage the previous picture remains there which in my understanding it shouldn't happen. As i understood from Adobe's AS3 Guide once you create a new instance of a loader, the previous one should be erased.

I've been looking all over the place for a solution but i can't find one that will work. I tried using the .unload class and i tried removeChild as well but it keeps giving me errors.

Here is the code i've been using:

import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.filters.*;
import flash.display.MovieClip;
import flash.events.MouseEvent; 

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("portfolio.xml"));

function LoadXML(e:Event):void {
    xmlData = new XML(e.target.data);
    ParsePortfolio(xmlData);
}

function ParsePortfolio(siteInput:XML):void {

    var titleList:XMLList = siteInput.Site.title;
    var servicesList:XMLList = siteInput.Site.services;
    var urlList:XMLList = siteInput.Site.url;
    var picList:XMLList = siteInput.Site.picture;

    var XX:Number = 0;

    var titleElement:XML = titleList[XX];
    var servicesElement:XML = servicesList[XX];
    var urlElement:XML = urlList[XX];   
    var picElement:XML = picList[XX];

    container_mc.title_txt.text = (titleElement);
    container_mc.services_txt.text = (servicesElement);

    var pictureNumber:String = picElement;
    var my_loader:Loader = new Loader();
    my_loader.load(new URLRequest(pictureNumber));
    container_mc.pictureFrame.addChild(my_loader);  

    next.addEventListener (MouseEvent.CLICK, NbuttonClicked);
    prev.addEventListener (MouseEvent.CLICK, PbuttonClicked);

    function NbuttonClicked(e:Event):void  {

        var t1:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 1, 0, .2, true);

        t1.addEventListener(TweenEvent.MOTION_FINISH, dropbomb);        

        function dropbomb() {       

            XX++;

            if (XX == titleList.length()) {
            XX = 0;
            }

            var titleElement:XML = titleList[XX];
            var servicesElement:XML = servicesList[XX];
            var urlElement:XML = urlList[XX];
            var picElement:XML = picList[XX];

            container_mc.title_txt.text = titleElement;
            container_mc.services_txt.text = servicesElement;
            var pictureNumber:String = picElement;

            trace(pictureNumber);

            var my_loader:Loader = new Loader();
            my_loader.load(new URLRequest(pictureNumber));
            container_mc.pictureFrame.addChild(my_loader);              

            visit.addEventListener (MouseEvent.CLICK, VbuttonClicked);

            function VbuttonClicked(e:Event):void  {

            navigateToURL(new URLRequest(urlElement), "_blank");

            }

            var t2:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 0, 1, .2, true);
        }
    }

    function PbuttonClicked(e:Event):void  {

        var t3:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 1, 0, .2, true);

        t3.addEventListener(TweenEvent.MOTION_FINISH, dropbomb);

        function dropbomb() {

            if ( XX == 0 )
            {
            XX = (titleList.length()) - 1;
            }

            else {XX--;}

            var titleElement:XML = titleList[XX];
            var servicesElement:XML = servicesList[XX];
            var urlElement:XML = urlList[XX];
            var picElement:XML = picList[XX];           

            container_mc.title_txt.text = titleElement;
            container_mc.services_txt.text = servicesElement;
            var pictureNumber:String = picElement;

            var my_loader:Loader = new Loader();
            my_loader.load(new URLRequest(pictureNumber));

            visit.addEventListener (MouseEvent.CLICK, VbuttonClicked);

            function VbuttonClicked(e:Event):void  {

            navigateToURL(new URLRequest(urlElement), "_blank");

            }           

            var t4:Tween = new Tween (container_mc, "alpha", Strong.easeOut, 0, 1, .2, true);

        }   
    }
}

The movie clips are being added to the stage manually. I don't know if this is causing the problem or if it's completely unrelated.

To be more specific while the container_mc is fading out, you can see the previous picture which shows that they are stacking rather then being replaced.

I would really appreciate some help, at least i'd like to know what i am doing wrong and to be pointed to the right direction.

I am sorry for the noobish code and for the stupid question, but i've really looked all over for the answer, i've looked for tutorials on creating XML slideshows and so far nothing helped me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔小萌 2024-10-24 03:38:28

当您实例化(并对其调用 addChild)加载器时,它会添加到显示列表中。然后必须将其删除才能不再出现在显示列表中。您只需使用动态 URLRequest 调用 Loader.load() 即可,而不必一遍又一遍地实例化加载程序。这样,您只需坚持使用一个每次调用时都会发生变化的加载器。

例如:

import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;

var _loader:Loader = new Loader();
var _toLoad:URLRequest = new URLRequest("path.jpg");
_loader.load(_toLoad);
addChild(_loader);

loadImage("1.jpg");

function loadImage(path:String):void
{
    _toLoad = new URLRequest(path);
    _loader.load(_toLoad);
    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleted, false, 0, true);
}

function loadCompleted(evt:Event):void
{
    trace("load completed");
}

When you instantiate (and call addChild on it) a loader, it's added to the display list. It then has to be removed to cease to be in the display list. Instead of instantiating a loader over and over, you can just call Loader.load() on it with a dynamic URLRequest. That way, you're just sticking with one loader that changes every time you call it.

For example:

import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;

var _loader:Loader = new Loader();
var _toLoad:URLRequest = new URLRequest("path.jpg");
_loader.load(_toLoad);
addChild(_loader);

loadImage("1.jpg");

function loadImage(path:String):void
{
    _toLoad = new URLRequest(path);
    _loader.load(_toLoad);
    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleted, false, 0, true);
}

function loadCompleted(evt:Event):void
{
    trace("load completed");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文