PDA

Volledige versie bekijken : herhaling van AS2 code + XML


dragonlau
%Europe/Berlin %069 %2008, 01:39
Ik maak gebruik van XML om een gallery slideshow weer te geven.
Dit werkt allemaal perfect. Maar in plaats van 1 slideshow op de homepage, wil ik 4 slideshows tegelijkertijd weergeven.

Kan ik door middel van 1 XML 4 slideshows tonen (geheel onafhankelijk van elkaar laten roteren)?

En hoe kan ik onderstaande code compacter maken, nu geef ik voor alle term een nummer mee.. dus ik heb nu 4x het dubbele staan, waarvan maar 1 term verschilt en dat is het nummer erachter.

/////////////////// Gallery 1 ///////////////////
var myShowXML = new XML();
myShowXML.ignoreWhite = true;
myShowXML.load("gallery1.xml");

myShowXML.onLoad = function() {
_root.myWidth = myShowXML.firstChild.attributes.width;
_root.myHeight = myShowXML.firstChild.attributes.height;
_root.mySpeed = myShowXML.firstChild.attributes.speed;

_root.myImages = myShowXML.firstChild.childNodes;
_root.myImagesNo = myImages.length;

createContainer();
callImages();
};

function createContainer() {
_root.createEmptyMovieClip("myContainer_mc",_root.getNextHighestDepth());

myContainer_mc.lineStyle(3,0x000000,100);
myContainer_mc.lineTo(_root.myWidth,0);
myContainer_mc.lineTo(_root.myWidth,_root.myHeight );
myContainer_mc.lineTo(0,_root.myHeight);
myContainer_mc.lineTo(0,0);

myContainer_mc._x = 37;
myContainer_mc._y = 265;
}


/////////////////// Gallery 2 ///////////////////
var myShowXML2 = new XML();
myShowXML2.ignoreWhite = true;
myShowXML2.load("gallery2.xml");

myShowXML2.onLoad = function() {
_root.myWidth2 = myShowXML2.firstChild.attributes.width;
_root.myHeight2 = myShowXML2.firstChild.attributes.height;
_root.mySpeed2 = myShowXML2.firstChild.attributes.speed;

_root.myImages2 = myShowXML2.firstChild.childNodes;
_root.myImagesNo2 = myImages2.length;

createContainer2();
callImages2();
};

function createContainer2() {
_root.createEmptyMovieClip("myContainer_mc2",_root.getNextHighestDepth());

myContainer_mc2.lineStyle(3,0x000000,100);
myContainer_mc2.lineTo(_root.myWidth2,0);
myContainer_mc2.lineTo(_root.myWidth2,_root.myHeig ht2);
myContainer_mc2.lineTo(0,_root.myHeight2);
myContainer_mc2.lineTo(0,0);

myContainer_mc2._x = 865;
myContainer_mc2._y = 265;
}

// idem voor gallery3 en gallery4 met dezelfde code, maar dan met een andere nummering


kan iemand mij vertellen of dit compacter kan?

Jan
%Europe/Berlin %916 %2008, 21:59
Gebruik een for loop:

var aantal:Number=4;
for (var i:Number=1; i<=aantal; i++)
{
this["myShowXML"+i] = new XML();
this["myShowXML"+i].ignoreWhite = true;
this["myShowXML" + i].load("gallery"+i+".xml");

this["myShowXML"+i].onLoad = function() {
_root["myWidth"+i] = this.firstChild.attributes.width;
_root["myHeight"+i] = this.firstChild.attributes.height;
_root["mySpeed"+i] = this.firstChild.attributes.speed;

_root["myImages"+i] = this.firstChild.childNodes;
_root["myImagesNo"+i] = _root["myImages"+i].length;

this["createContainer"+i]();
this["callImages"+i]();
};

this["createContainer"+i]=function()
{
var myContainer_mc = _root.createEmptyMovieClip("myContainer_mc"+i,_root.getNextHighestDepth());

myContainer_mc.lineStyle(3,0x000000,100);
myContainer_mc.lineTo(_root["myWidth"+i],0);
myContainer_mc.lineTo(_root["myWidth"+i],_root["myHeight"+i] );
myContainer_mc.lineTo(0,_root["myHeight"+i]);
myContainer_mc.lineTo(0,0);

myContainer_mc._x = i * een waarde die misschien overeenkomt met: _root["myWidth"+.i]+10(=spatie)
myContainer_mc._y = 265;
};
}
Maar misschien moet je die i opslaan in het function object:

var aantal:Number=4;
for (var i:Number=1; i<=aantal; i++)
{
this["myShowXML"+i] = new XML();
this["myShowXML"+i].ignoreWhite = true;
this["myShowXML" + i].load("gallery"+i+".xml");

this["myShowXML"+i].onLoad = function() {
_root["myWidth"+i] = this.firstChild.attributes.width;
_root["myHeight"+i] = this.firstChild.attributes.height;
_root["mySpeed"+i] = this.firstChild.attributes.speed;

_root["myImages"+i] = this.firstChild.childNodes;
_root["myImagesNo"+i] = _root["myImages"+i].length;

this["createContainer"+i]();
this["callImages"+i]();
};
this["createContainer"+i].i=i
this["createContainer"+i]=function()
{
var myContainer_mc = _root.createEmptyMovieClip("myContainer_mc"+this.i,_root.getNextHighestDepth());

myContainer_mc.lineStyle(3,0x000000,100);
myContainer_mc.lineTo(_root["myWidth"+this.i],0);
myContainer_mc.lineTo(_root["myWidth"+this.i],_root["myHeight"+this.i] );
myContainer_mc.lineTo(0,_root["myHeight"+this.i]);
myContainer_mc.lineTo(0,0);

myContainer_mc._x = i * een waarde die misschien overeenkomt met: _root["myWidth"+this.i]+10(=spatie)
myContainer_mc._y = 265;
};
}
of meegeven als argument:
for (var i:Number=1; i<=4; i++)
{
this["myShowXML"+i] = new XML();
this["myShowXML"+i].ignoreWhite = true;
this["myShowXML" + i].load("gallery"+i+".xml");

this["myShowXML"+i].onLoad = function() {
_root["myWidth"+i] = this.firstChild.attributes.width;
_root["myHeight"+i] = this.firstChild.attributes.height;
_root["mySpeed"+i] = this.firstChild.attributes.speed;

_root["myImages"+i] = this.firstChild.childNodes;
_root["myImagesNo"+i] = _root["myImages"+i].length;

createContainer(i);
callImages(i);
};
}
//---------------------------
function createContainer(i:Number)
{
var myContainer_mc = _root.createEmptyMovieClip("myContainer_mc"+i,_root.getNextHighestDepth());

myContainer_mc.lineStyle(3,0x000000,100);
myContainer_mc.lineTo(_root["myWidth"+i],0);
myContainer_mc.lineTo(_root["myWidth"+i],_root["myHeight"+i] );
myContainer_mc.lineTo(0,_root["myHeight"+i]);
myContainer_mc.lineTo(0,0);

myContainer_mc._x = i * een waarde die misschien overeenkomt met: _root["myWidth"+.i]+10(=spatie)
myContainer_mc._y = 265;
};

Groeten,
Jan