Hello. I am creating traffic lights, but I'm new to flash. I have a timer that ticks 6 times. However, I want to make the timer refresh after 6, and on each tick, a different traffic light shows.
//set lights in start state
var flowIsEW:Boolean = true;
startState();
function startState()
{
//only use two of the lights for now!
this.lightN.gotoAndStop("stop");
this.lightW.gotoAndStop("go");
//park two of the lights for a while;
this.lightS.gotoAndStop("allOff");
this.lightE.gotoAndStop("allOff");
}
var flowTimer:Timer = new Timer(5000,1);
flowTimer.addEventListener(TimerEvent.TIMER,swapFl ow);
flowTimer.start();
var changeTimer:Timer = new Timer(2000);
changeTimer.addEventListener(TimerEvent.TIMER,lamp Change);
//swap the traffic flow on each ticker of the flow timer;
function swapFlow(evt:TimerEvent):void
{
flowIsEW = ! flowIsEW;
changeTimer.start();
trace("flow of traffic is swapped");
trace(flowIsEW);
if (flowIsEW)
{
this.lightN.gotoAndStop("stop");
this.lightW.gotoAndStop("go");
}
if (! flowIsEW)
{
this.lightN.gotoAndStop("go");
this.lightW.gotoAndStop("stop");
}
flowTimer.reset();
}
function lampChange(evt:TimerEvent):void
{
trace("lamps change");
}
However that just has 2 lights not moving. One on stop, one on go.
The logic has errs, use an else statement. Also, there are linear errs.
Not sure what your aiming for, but this works:
Code:
import flash.utils.Timer;
//set lights in start state
var flowIsEW:Boolean ;
var flowTimer:Timer = new Timer(2000,10);
flowTimer.addEventListener(TimerEvent.TIMER,swapFlow);
flowTimer.start();
var changeTimer:Timer = new Timer(2000, 10);
changeTimer.addEventListener(TimerEvent.TIMER,lampChange);
function startState()
{
//only use two of the lights for now!
this.lightN.gotoAndStop("stop");
this.lightW.gotoAndStop("go");
//park two of the lights for a while;
this.lightS.gotoAndStop("allOff");
this.lightE.gotoAndStop("allOff");
flowIsEW = false;
}
//swap the traffic flow on each ticker of the flow timer;
function swapFlow(evt:TimerEvent):void
{
//changeTimer.start();
trace("flow of traffic is swapped");
trace(flowIsEW);
if (flowIsEW==true)
{
this.lightN.gotoAndStop("stop");
this.lightW.gotoAndStop("go");
flowIsEW = false;
}
else if (flowIsEW==false)
{
this.lightN.gotoAndStop("go");
this.lightW.gotoAndStop("stop");
flowIsEW = true;
}
//flowTimer.reset();
}
function lampChange(evt:TimerEvent):void
{
trace("lamps change");
swapFlow(evt);
}
startState();