I'm trying to create an as3 project that dynamically pulls images from a database and sticks them into the framework of a flash movie that I making w开发者_如何学Goith this. Currently I am following a tutorial (in as2, and I am converting it to as3 as I go along) and am trying to get images to load from the same directory as the project, but I am having issues with the Loader. I am a complete newb at as3 and as such have almost no idea what I am doing, so I come to you guys for help as to why my program won't work properly. Here is the entire code from my project.
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();
}
The issue here is that the imageLoader won't throw any events as it is loading the image file, and there are absolutely no errors to give me any clue why it might be failing. Here is how my xml file looks:
Assets/DaVinci1.jpg Assets/DaVinci2.jpg Assets/DaVinci3.jpg
Both the project and the Assets folder are in the same directory, so the filepath should not be an issue, but otherwise I don't know. Here is the tutorial I am following (http://active.tutsplus.com/tutorials/xml/flash-slideshow-xml/)
When you are loading the swf
or image
types you need to add eventListener not to the loader
but to the 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 );
and whe you need to load data ( text based files ) you doing like with your 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 );
You need to add your listeners onto loader.contentLoaderInfo
, not 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
精彩评论