Hi. I am having trouble just getting started on a simple as3 image slider. I cannot figure out oop in flash.
In my fla file I have:
var datastr:String = '["img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg", "img6.jpg", "img7.jpg", "img8.jpg"] ';
var slideshow:Slideshow = new Slideshow(datastr);
addChild(slideshow);
then Slideshow is:
package {
import com.adobe.serialization.json.JSON
import flash.display.*
import flash.events.*
import flash.utils.*
public class Slideshow extends Sprite
{
// Constants:
// Public Properties:
// Private Properties:
private var container:Sprite;
private var displayTimer:Timer;
private var displayTimerDelay:Number = 4000;
private var queue:Array;
private var currentIndex:int = -1;
// Initialization:
public function Slideshow(jsonString:String)
{
var container = new Sprite();
addChild(container);
//parse the data
parseJsonString(jsonString);
//initialize
init();
}
// Public Methods:
// Protected Methods:
private function parseJsonString(str):void
{
queue = new Array();
var o:Object = JSON.decode(str);
var item:Object = new Object();
for (var i:int = 0; i < o.length; i++)
{
item.url = o*;
queue.push(item);
}
}
private function init():void
{
//start the timer
displayTimer = new Timer(displayTimerDelay);
displayTimer.addEventListener(TimerEvent.TIMER, display);
displayTimer.start();
}
private function display(e:TimerEvent):void
{
//stop the timer while we animate
displayTimer.reset();
var i:int = getNextIndex();
var temp = currentIndex;
//update current index
currentIndex = i;
var currentTime:Date = new Date();
trace("Time: " + currentTime.time);
trace("queue.length is: "+queue.length+" and currentIndex was: " + temp + " and now is: " + currentIndex + " and i is: " +i);
//restart the timer
//displayTimer.start();
}
private function getNextIndex():int
{
var i:int = currentIndex + 1;
//trace("i is:" + i + " and queue.length is: " +queue.length + " and currentIndex is: " + currentIndex);
if(i == queue.length)
{
i = 0;
}
return i;
}
}
}
I feel like I should only get the trace to output one time because I stop the timer and never start it again. I don’t really understand why, but I am guessing that I don’t just get one instance of the Slideshow. New instances keep getting assigned or something because this is the trace output:
Time: 1272436012629
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436012797
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436012965
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436013133
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436013301
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436013461
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436013629
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436013797
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436013965
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Time: 1272436014133
queue.length is: 8 and currentIndex was: -1 and now is: 0 and i is: 0
Debug session terminated.
Also, why is my currentIndex not getting updated? I tried every combination of incrementing I could think of, but the output is the same. I guess all of my questions have the same answer, but another one is why isn’t my timer taking four seconds to go off?