无法在Flash as3.0中从XML加载缩略图

时间:2010-11-15 13:52:53

标签: xml flash actionscript-3 thumbnails gallery

我制作了一个图库脚本,可以从xml加载大图像,文本和3个缩略图。 由于某种原因,我无法加载缩略图。 我有它,以便三个缩略图应该是前一个图像,当前图像和下一个图像。除非您单击当前图像,否则应加载随机图像。按钮适用于此但如果我尝试加载缩略图。它们没有出现。

要加载缩略图,我为缩略图数组创建了一个for循环,并且变量noOfImage记录了图像的编号。 然后,如果单击prev按钮(鼠标滚轮向下或键dwn或向左键),则通过减去1到noOfImage加载其他缩略图,如果单击下一个按钮(滚轮和键......),则向noOfImage添加1

当我追踪缩略图编号的变量时,它们似乎有时是正确的,有时我收到错误。

对于前。 如果我加载swf并单击下一步。上一个图像是“4294967295”,下一个是1,但它输出“错误#2044:未处理的IOErrorEvent:。text =错误#2035:找不到URL。”

如果nextImage的值= 1那么我不明白为什么它没有加载。为什么它会为0应该给出一个奇怪的数字呢?我无法判断问题是否与我用来处理noOfImage变量的if语句有关,或者它是否与加载器或缩略图容器有关...我会继续尝试我能想到的一切。我的原始版本用for循环加载所有拇指并且工作但我无法将它们变成可点击的按钮并决定使其更简单。

这是一个我用脚本获得了多远的例子。 http://soulseekrecords.org/psysci/portfolio/Portfolio.swf

如果它是凌乱或不清楚,下面的

是我的所有代码。我尽我所能。

//import tweening files
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import fl.controls.UIScrollBar;
import flash.display.Sprite;
import com.greensock.plugins.*;
TweenPlugin.activate([TintPlugin]);

//create sprite background
var backgrnd:Sprite = new Sprite();

//create array of image names
var imageNames:Array = new Array();

//create array of text descriptions
var descText:Array = new Array();

//create array for dice images
var dinames:Array = new Array();

//variable for total number of images
var totalImages:uint = imageNames.length;

//add sprite variables
var thumb:Sprite = new Sprite();
var thumb2:Sprite = new Sprite();
var thumb3:Sprite = new Sprite();

// create loaders
var smImageLoader:Loader = new Loader();
var smImageLoader2:Loader = new Loader();
var smImageLoader3:Loader = new Loader();

//create a variable to load the large image
var bigImageLoader:Loader = new Loader();

//create variables for the image #
var noOfImage:int;
var prevImage:uint;
var nextImage:uint;

//create random number var
var randomNumber:uint;

prevImage = 0;
nextImage = totalImages;

noOfImage = -1;

// add sprite to display list
addChild(thumb);
addChild(thumb2);
addChild(thumb3);
thumb.graphics.beginFill(0xFFFFFF);
thumb.graphics.drawRect(25, 325, 75, 75);
thumb.graphics.endFill();
thumb2.graphics.beginFill(0xFFFFFF);
thumb2.graphics.drawRect(125, 325, 75, 75);
thumb2.graphics.endFill();
thumb3.graphics.beginFill(0xFFFFFF);
thumb3.graphics.drawRect(225, 325, 75, 75);
thumb3.graphics.endFill();
thumb.alpha = 1;
thumb2.alpha = 1;
thumb3.alpha = 1;
thumb.addChild(smImageLoader2);
thumb2.addChild(smImageLoader3);
thumb3.addChild(smImageLoader);
smImageLoader.y = 325;
smImageLoader2.y = 325;
smImageLoader3.y = 325;
smImageLoader.x = 25;
smImageLoader2.x = 125;
smImageLoader3.x = 225;

//Scroll Variables
var scrollBarAdd:Boolean = false;
var scrollBar:UIScrollBar = new UIScrollBar();

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("portfolio.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event)
{
    myXML = new XML (e.target.data);
    for(var i:uint = 0; i < myXML.img.length() ; i++)
    {
        imageNames.push(myXML.img[i].text());
        descText.push(myXML.txt[i].text()); 
        dinames.push(myXML.imgd[i].text());
    }
    totalImages = imageNames.length;
}

//add background color sprite
addChild(backgrnd);
setChildIndex(backgrnd,0);

//add background properties
backgrnd.graphics.beginFill(0xFFFFFF);
backgrnd.graphics.drawRect(0, 0, 900, 500);

//add background image
var backrobe:Loader = new Loader();
addChild(backrobe);
backrobe.x = 0;
backrobe.y = 0;
backrobe.load(new URLRequest("images/background.png"));

//add bigImageLoader to the display list
addChild(bigImageLoader);
bigImageLoader.x = 225;
bigImageLoader.y = 25;

//add buttons fwd and rev
var forward:fwd = new fwd();
var reverse:rev = new rev();
addChild(forward);
addChild(reverse);
forward.x = 155;
forward.y = 365;
reverse.x = 33;
reverse.y = 365;

forward.addEventListener(MouseEvent.MOUSE_OVER, mseovr);
reverse.addEventListener(MouseEvent.MOUSE_OVER, mseovr2);
forward.addEventListener(MouseEvent.MOUSE_OUT, mseout);
reverse.addEventListener(MouseEvent.MOUSE_OUT, mseout2);
function mseovr (event:MouseEvent){
    TweenLite.to(forward, .1, {alpha:.5});
}
function mseovr2 (event:MouseEvent) {
    TweenLite.to(reverse, .1, {alpha:.5});
}
function mseout (event:MouseEvent){
    TweenLite.to(forward, .1, {alpha:1});
}
function mseout2 (event:MouseEvent) {
    TweenLite.to(reverse, .1, {alpha:1});
}

//Variable Key Press Function
var rkey:uint = 39;
var lkey:uint = 37;
var ukey:uint = 38;
var dkey:uint = 40;

//create a textField
var myText:TextField = new TextField();

//add text box to display list
addChild(myText);
myText.width = 200;
myText.height = 340;
myText.x = 5;
myText.y = 5;
myText.wordWrap = true;
myText.multiline = true;
myText.selectable = true;

var format:TextFormat = new TextFormat();
            format.font = "Arial";
            format.color = 0xFFFFFF;
            format.size = 20;
            format.underline = false;
            myText.defaultTextFormat = format;

//add button
var hotSpot:hotspot = new hotspot();

//add button to display list
addChild(hotSpot);

//Set button x and y position
hotSpot.x = 92;
hotSpot.y = 367;
hotSpot.alpha = 0;
hotSpot.width = 50;
hotSpot.height = 50;

//set buttonmode
hotSpot.buttonMode = true;

//make sure all clicks register with the thumb itself (not inner contents)
hotSpot.mouseChildren = false;

//add click listener to the button
hotSpot.addEventListener(MouseEvent.MOUSE_DOWN, buttonClickHandler);
hotSpot.addEventListener(MouseEvent.MOUSE_OVER, mouseoverhotspot);
hotSpot.addEventListener(MouseEvent.MOUSE_OUT, mouseouthotspot);

function mouseoverhotspot(event:MouseEvent){
TweenLite.to(hotSpot, .1, {alpha:.5});
}
function mouseouthotspot(event:MouseEvent){
TweenLite.to(hotSpot, 1, {alpha:0});
}

function buttonClickHandler (event:MouseEvent) 
    {
        randomNumber = Math.random()*3;
        noOfImage = randomNumber;
        loadersnlist();

    }

stage.addEventListener(MouseEvent.MOUSE_WHEEL, upmouse);

function upmouse(event:MouseEvent) 
    {
        if (event.delta == 3)
        {
            noOfImage = noOfImage + 1;
                if (noOfImage == totalImages)
                    {
                        noOfImage = noOfImage - totalImages;
                    }

        }
            if (event.delta == -3)
                {

                    if (noOfImage == -1 || noOfImage == -2)
                        {
                        noOfImage = totalImages;
                        }
                            if (noOfImage == 0)
                                {
                                    noOfImage = noOfImage + totalImages;
                                }
            noOfImage = noOfImage - 1;
                }
        loadersnlist();

    }

forward.addEventListener(MouseEvent.CLICK, fwdclck);
    function fwdclck (event:MouseEvent)
    {
        noOfImage = noOfImage + 1;
        if (noOfImage == totalImages)
            {
            noOfImage = noOfImage - totalImages;
            }
        loadersnlist();
    }

reverse.addEventListener(MouseEvent.CLICK, revclck);

function revclck(event:MouseEvent) 
    {
        if (noOfImage == -1 || noOfImage == -2)
            {
            noOfImage = totalImages;
            }
        if (noOfImage == 0)
            {
            noOfImage = noOfImage + totalImages;
            }
        noOfImage = noOfImage - 1;
        loadersnlist();
    }

//Key Right Arrow Event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyright);

function keyright(event:KeyboardEvent)
{

    if (event.keyCode==rkey || event.keyCode==ukey) 
        {
            noOfImage = noOfImage + 1;
            if (noOfImage == totalImages)
                {
                noOfImage = noOfImage - totalImages;
                }
            loadersnlist();
        }
}

//Key Left Arrow Event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyleft);

function keyleft(event:KeyboardEvent){

    if (event.keyCode==lkey || event.keyCode==dkey) 
        {
            if (noOfImage == -1 || noOfImage == -2)
                {
                noOfImage = totalImages;
                }
    if (noOfImage == 0)
        {
        noOfImage = noOfImage + totalImages;
        }
        noOfImage = noOfImage - 1;
        loadersnlist();
        }
}

function scrollerthing ()
    {
        if (myText.textHeight > myText.height)
        {
            scrollBar.scrollTarget = myText;  //assign the target of the scrollBar to your textfield
            scrollBar.height = myText.height;  //make the height the same as the textfield
            scrollBar.move(myText.x + myText.width, myText.y);  //Move the scrollbar to the righthand side
            addChild(scrollBar);
            scrollBar.update();
            scrollBarAdd = true;
        } else 
        { 

            if((myText.textHeight < myText.height) && scrollBarAdd == true)
                {
                    scrollBarAdd = false;
                    removeChild(scrollBar);
                }
        }
    }

function loadersnlist () 
    {
        bigImageLoader.load(new URLRequest("images/" + imageNames[noOfImage] ) );
        bigImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, tweenage);
        smImageLoader.load(new URLRequest("thumbs/" + dinames[noOfImage] ) );
        smImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, tweenage2);
        prevImage = noOfImage - 1;
        nextImage = noOfImage + 1;
        if (prevImage <= -1){
            prevImage = totalImages;
        }
        if (nextImage >= totalImages){
            nextImage = 0;
        }
        trace (prevImage);
        trace (nextImage);
        smImageLoader2.load(new URLRequest("thumbs/" + dinames[prevImage] ) );
        smImageLoader2.contentLoaderInfo.addEventListener(Event.COMPLETE, tweenage2);
        smImageLoader3.load(new URLRequest("thumbs/" + dinames[nextImage] ) );
        smImageLoader3.contentLoaderInfo.addEventListener(Event.COMPLETE, tweenage2);
    }

function tweenage(e:Event)
    {
        bigImageLoader.alpha = 0;
        TweenLite.to( bigImageLoader, 5, {alpha:1, ease:Expo.easeOut} );
        myText.alpha = 0;
        TweenLite.to( myText, 5, {alpha:1} );
        myText.text = String (descText[noOfImage]);
        scrollerthing();
    }

function tweenage2 (e:Event)
    {        
        //create a variable to load small thumbnail url image        
        TweenLite.to( smImageLoader, 5, {scaleX:.6, scaleY:.6, ease:Expo.easeOut} );
        TweenLite.to( smImageLoader2, 5, {scaleX:.6, scaleY:.6, ease:Expo.easeOut} );
        TweenLite.to( smImageLoader3, 5, {scaleX:.6, scaleY:.6, ease:Expo.easeOut} );
    }

1 个答案:

答案 0 :(得分:0)

你几乎在追踪缩略图值。我的下一步是查找您尝试加载的实际URL。

执行此操作将显示您实际引用的内容。看起来您正在跟踪prevImage,并将其设置为小于1时的图像数量。但是缩略图数组是从零开始的,因此使用3个缩略图时,拇指位于0,1和2并且你正试图加载dinames [3]。

将loadersnlist功能更改为   prevImage = totalImages-1; 应该修复它。

顺便说一下,我建议将所有资产创建代码放在布局函数中,然后将所有内容添加到舞台后再调用加载器函数来提取数据。