Hey guys,
I'm trying to do a Flash banner in which I cycle through a collection of images which are stored inside an Iterator class. First, everything goes fine until the index hits the maximum and resets back to 0. When that happen, the images do not display anymore...
Here's the code:
private function imagesReady(e:Event):void
{
images = loader.data;
pIterator = new Iterator(images);
addChild(pIterator.current);
TweenLite.from(pIterator.current, 2, { alpha:0, onComplete: playSlideshow } );
trace(pIterator.index); // outputs 0
}
private function playSlideshow()
{
var current:DisplayObject = pIterator.current;
var next:DisplayObject = pIterator.next();
trace(pIterator.index); // starts at 1 goes up to 4 then back to 0 and so on
addChild(next);
addChild(current);
TweenLite.to(current, 2, { alpha:0, delay:3, onComplete: cleanUp, onCompleteParams:[current, next] } );
TweenLite.from(next, 2, { alpha:0, delay:3 } );
}
private function cleanUp(old:DisplayObject, next:DisplayObject)
{
pIterator.current = next;
if (stage.contains(old))
{
removeChild(old);
}
playSlideshow();
}
And here's the Iterator class:
package
{
/**
* A class which iterates through a collection
* @author Kassem Darwish
*/
import flash.display.DisplayObject;
public class Iterator
{
private var _collection:Array;
private var _index:uint;
private var _current:DisplayObject;
public function Iterator(collection:Array)
{
_collection = collection;
_index = 0;
_current = _collection[0];
}
public function hasNext():Boolean
{
return _index < _collection.length - 1;
}
public function next():DisplayObject
{
if (this.hasNext())
{
return this._collection[++_index];
} else {
reset();
return this._collection[_index];
}
}
public function reset():void
{
this._index = 0;
}
public function get current():DisplayObject
{
_current = _collection[_index];
return this._current;
}
public function set current(value:DisplayObject):void
{
_current = value;
}
public function get index():uint
{
return _index;
}
}
}
The weird part is that I'm getting no errors at all, yet nothing is being displayed after the first cycle... I just get a blank white screen and the trace keeps going on fine.
Suggestions please? If there's a better way of doing this please tell me about it.