动态地将图像加载到Flash项目中

时间:2011-07-07 14:32:56

标签: actionscript-3 actionscript

我正在尝试创建一个动态从数据库中提取图像的as3项目,并将它们粘贴到我用它制作的flash电影的框架中。目前我正在关注一个教程(在as2中,我正在将它转换为as3)并且我试图从与项目相同的目录中加载图像,但是我遇到了Loader的问题。我是as3的完全新手,因此几乎不知道我在做什么,所以我来找你们帮忙,为什么我的程序无法正常工作。这是我项目的全部代码。

import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.*;
import com.greensock.TweenLite;
import com.greensock.easing.*;

var xmlPath = "content.xml";
var photos_xml:XML = new XML();
var imageList:XMLList = new XMLList();
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, loadXML)
var imageLoader:Loader = new Loader();
imageLoader.addEventListener(Event.INIT, initHandler);
imageLoader.addEventListener(Event.COMPLETE, completeHandler);
var currentImage:Number = 0;
var timer:Number;

xmlLoader.load(new URLRequest(xmlPath));

//stop();

function loadXML(e:Event):void
{
    photos_xml = new XML(e.target.data);
    imageList = photos_xml.image.text();

    //for (var i:int = 0; i < imageList.length(); i++)
//  {
//      trace (imageList[i]);
//  }

    loadImage();
}

function loadImage()
{
    var loadURL = imageList[currentImage]
    var req:URLRequest = new URLRequest(loadURL);
    var targetClip:MovieClip = new MovieClip();
    targetClip.alpha = 0;
    clearInterval(timer);

    //trace(loadURL);
    //======== imageLoader refuses to throw events! ===========
    trace(req.url)
    imageLoader.load(req);
    targetClip.addChild(imageLoader);
    shell_mc.pics_mc.addChild(targetClip);

    trace("I am in loadImage");
}

function setTimer():void
{
    timer = setInterval(loadImage, 1000);
}

function removePrevious(prevImg:MovieClip):void
{
    if (prevImg != null)
    {
        removeChild(prevImg);
        //removeMovieClip(prevImg);
    }

    //trace(currentImage);
    currentImage = (currentImage + 1) % imageList.length();
}

function initHandler(event:Event):void
{
    trace("I am in initHandler");
    TweenLite.to(shell_mc.background_mc, 0.25, {width:event.target.width + 20, height:event.target.height + 20, ease:Quad.easeOut});
    TweenLite.to(shell_mc.border_mc, 0.25, {width:event.target.width, height:event.target.height, ease:Quad.easeOut});
    TweenLite.to(shell_mc.mask_mc, 0.25, {width:event.target.width, height:event.target.height, ease:Quad.easeOut});

    var clipXTarg = Math.round((stage.width / 2) - ((event.target.width + 20) / 2));
    var clipYTarg = Math.round((stage.height / 2) - ((event.target.height + 20) / 2));
    TweenLite.to(shell_mc, 0.25, {x:clipXTarg, y:clipYTarg, ease:Quad.easeOut});

    var prevImgNum:Number;
    if (currentImage == 0)
    {
        prevImgNum = imageList.length() - 1;
    }
    else
    {
        prevImgNum = currentImage - 1;
    }
    var prevImg = shell_mc.pics_mc["pic" + prevImgNum];
    TweenLite.to(prevImg, 0.25, {autoAlpha:0, onComplete:removePrevious(prevImg)});
}

function completeHandler(event:Event):void
{
    trace("I am in completeHandler");
    TweenLite.to(event.target, 0.25, {autoAlpha:100, delay:0.25});
    setTimer();
}

这里的问题是imageLoader在加载图像文件时不会抛出任何事件,并且绝对没有任何错误可以告诉我为什么它可能会失败。以下是我的xml文件的外观:

资产/ DaVinci1.jpg 资产/ DaVinci2.jpg 资产/ DaVinci3.jpg

项目和Assets文件夹都在同一目录中,因此文件路径不应该是一个问题,否则我不知道。这是我正在关注的教程(http://active.tutsplus.com/tutorials/xml/flash-slideshow-xml/)

2 个答案:

答案 0 :(得分:2)

当您加载swfimage类型时,您需要将eventListener添加到loader但不添加到contentLoaderInfo

contentLoader   = new Loader ();

contentLoader.contentLoaderInfo.addEventListener ( Event.COMPLETE, handleComplete );
contentLoader.contentLoaderInfo.addEventListener ( ProgressEvent.PROGRESS, handleProgress );
contentLoader.contentLoaderInfo.addEventListener ( ErrorEvent.ERROR, handleError );
contentLoader.contentLoaderInfo.addEventListener ( IOErrorEvent.IO_ERROR, handleError );
contentLoader.contentLoaderInfo.addEventListener ( SecurityErrorEvent.SECURITY_ERROR, handleError );

您需要加载与XMLLoader一样的数据(基于文本的文件)

dataLoader              = new URLLoader ();

dataLoader.addEventListener ( Event.COMPLETE, handleComplete );
dataLoader.addEventListener ( ProgressEvent.PROGRESS, handleProgress );         
dataLoader.addEventListener ( ErrorEvent.ERROR, handleError );
dataLoader.addEventListener ( IOErrorEvent.IO_ERROR, handleError );
dataLoader.addEventListener ( SecurityErrorEvent.SECURITY_ERROR, handleError );

答案 1 :(得分:2)

您需要将您的听众添加到loader.contentLoaderInfo,而不是loader

imageLoader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void
{
    trace("I am in completeHandler");
    TweenLite.to(event.target.content, 0.25, {autoAlpha:100, delay:0.25});
    setTimer();
}

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#contentLoaderInfo