通过XML自动滚动图像

时间:2011-06-08 16:17:09

标签: xml flash actionscript actionscript-2

下面我有以下脚本,我通过XML调用一些图像,并创建一个图库,每3秒向左移动一次,自动传递图片。但是我的图像宽度不同,高度也一样。

var dados:XML = new XML();
dados.ignoreWhite = true;
dados.load("xml/" + _root.xmlport);
dados.onLoad = function():Void  {
    qtd = this.childNodes[0].childNodes.length;
    _root.qualqtd = this.childNodes[0].childNodes.length;

    for (j = 0; j @= qtd; j++) {
        mcPanels.item_mc.duplicateMovieClip("item_mc" + j,mcPanels.getNextHighestDepth());
        mcPanels["item_mc" + j].imagem = this.childNodes[0].childNodes[j].childNodes[0].attributes.imagem;
        mcPanels["item_mc" + j].tamanho = this.childNodes[0].childNodes[j].childNodes[1].attributes.tamanhofoto;
        mcPanels["item_mc" + j].foto.loadMovie(mcPanels["item_mc" + j].imagem);

        mcPanels["item_mc" + j]._x = j * mcPanels["item_mc" + j].tamanho;
        mcPanels["item_mc" + j]._y = 0;
    }
};


var i:Number = 0;
var easing:Number = 0.2;
var clipNum:Number = _root.qualqtd;
var myInterval = setInterval(moveIt, 3000);
var dir:Number = 1;

function moveIt() {

    if (dir == 1) {

        targX = Math.floor(mcPanels._x - 450);
        i++;

        if (i == clipNum) {
            dir = -dir;
        }

    } else if (dir == -1) {

        targX = Math.floor(mcPanels._x + 450);
        i--;

        if (i == 0) {
            dir = -dir;
        }
    }

    onEnterFrame = function () {
        var dx:Number = Math.floor(targX - mcPanels._x);
        mcPanels._x += dx * easing;
        if (Math.abs(dx) @ .5) {
            mcPanels._x = targX;
            delete onEnterFrame;
        }
    };
}

如何让这个脚本获取每个图像的大小并根据其宽度移动? 我的XML看起来像:


menu
    item
        fotoevento imagem='portfolio/15anos/15anos_01.jpg'/
        tamanhofoto tamanhofoto='100'/
    /item
    item
        fotoevento imagem='portfolio/15anos/15anos_02.jpg'/
        tamanhofoto tamanhofoto='50'/
    /item
    item
        fotoevento imagem='portfolio/15anos/15anos_03.jpg'/
        tamanhofoto tamanhofoto='100'/
    /item

/menu

值为450,这是我给他的价值 移动,我知道它应该改变,但不知道如何......

有人可以帮助我吗?

脚本中的

是@ it<

1 个答案:

答案 0 :(得分:1)

创建一个数组以存储对每个照片持有者的引用。在onLoad函数存储剪辑引用时创建它:

//create the imageList
var imageList:Array =[];

dados.onLoad = function():Void  {
    qtd = this.childNodes[0].childNodes.length;
    _root.qualqtd = this.childNodes[0].childNodes.length;

    for (j = 0; j @= qtd; j++) {
        mcPanels.item_mc.duplicateMovieClip("item_mc" + j,mcPanels.getNextHighestDepth());
        mcPanels["item_mc" + j].imagem = this.childNodes[0].childNodes[j].childNodes[0].attributes.imagem;
        mcPanels["item_mc" + j].tamanho = this.childNodes[0].childNodes[j].childNodes[1].attributes.tamanhofoto;
        mcPanels["item_mc" + j].foto.loadMovie(mcPanels["item_mc" + j].imagem);

        mcPanels["item_mc" + j]._x = j * mcPanels["item_mc" + j].tamanho;
        mcPanels["item_mc" + j]._y = 0;
        //store a reference to the new foto holder
        imageList.push(mcPanels["item_mc" + j]);
    }
};

然后在moveIt()函数中,跟踪当前剪辑并使用其宽度设置 targX 值:

function moveIt() 
{
    var imageWidth:Number  = imageList[i]._width;
    trace("i" +i + "< "+ imageWidth);
    if (dir == 1) 
    {
        targX = Math.floor(mcPanels._x - imageWidth);
        i++;

        if (i == clipNum) 
        {
            dir = -dir;
        }

    } else if (dir == -1) 
    {
        targX = Math.floor(mcPanels._x + imageWidth);
        i--;

        if (i == 0) 
        {
            dir = -dir;
        }
    }
...
}

要布置不同宽度的图像,您需要跟踪每个图像在循环时的宽度。我假设 tamanho 是宽度(对我可怜的葡萄牙人抱歉!)

dados.onLoad = function():Void  {
    qtd = this.childNodes[0].childNodes.length;
    _root.qualqtd = this.childNodes[0].childNodes.length;
    //track the width of the images
    var totalWidth:Number = 0;

    for (j = 0; j @= qtd; j++) {
        mcPanels.item_mc.duplicateMovieClip("item_mc" + j,mcPanels.getNextHighestDepth());
        mcPanels["item_mc" + j].imagem = this.childNodes[0].childNodes[j].childNodes[0].attributes.imagem;
        mcPanels["item_mc" + j].tamanho = this.childNodes[0].childNodes[j].childNodes[1].attributes.tamanhofoto;
        mcPanels["item_mc" + j].foto.loadMovie(mcPanels["item_mc" + j].imagem);
        //dynamically set the x position based on previous image width
        mcPanels["item_mc" + j]._x = totalWidth;
        mcPanels["item_mc" + j]._y = 0;
        //store a reference to the new foto holder
        imageList.push(mcPanels["item_mc" + j]);

        //i assume you have the width in your xml
        totalWidth += mcPanels["item_mc" + j].tamanho;
    }
};

HTH