2011-06-08 70 views
0

我在下面通过XML调用一些图像,并创建一个图库,每隔3秒左右移动一次,自动传递图片。但是,我的图像宽度不同,高度相同。通过XML自动滚动图像

 
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的值是我向他提出 移动的价值,我知道它的存在应该改变,但不知道如何....

有人可以帮我吗?

上脚本,是@其<

回答

1

创建一个数组来存储到每张照片持有者的参考。在moveIt()函数

//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]); 
    } 
}; 

然后,跟踪当前剪辑,并使用其宽度来设置targX值::在onload函数存储剪辑参考在创建它

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; 
    } 
}; 

心连心

+0

很不错...我有一个问题!!看到代码(对不起,我可怜的葡萄牙!): mcPanels [“item_mc “+ j] ._ x = j * mcPanels [”item_mc“+ j] .tamanho; 这把一张照片放在另一张照片的前面,但它们宽度不同,并且不工作..我怎样才能使它正确? – Preston 2011-06-09 11:08:57

+0

由于你有一个对剪辑的引用,你可以得到每个元素的宽度 - 所以var imageWidth:Number = imageList [i] .foto._width;或var imageWidth:Number = imageList [i] .tamanho._width; -hth – 2011-06-09 12:55:04

+0

让我们去:我把这个var imageWidth:Number = mcPanels [“item_mc”+ j] .tamanho;和我得到的所有图像的宽度...但我不明白的是如何计算对另一个位置... – Preston 2011-06-09 13:00:18