A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Creating traffics lights

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5

    Creating traffics lights

    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.

    So far I have this:


    var myTimer:Timer = new Timer(2000,6);

    var counter:Number = 0;

    myTimer.start();

    myTimer.addEventListener(TimerEvent.TIMER, onRemind);

    function onRemind(evt:TimerEvent):void
    {
    counter += 1;
    trace("reminder "+ counter);
    }

  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Sorry for the double post, I don't see an edit button?

    if (counter==1)
    {
    trafficlight.gotoAndPlay('stop');
    }

    if (counter==2)
    {
    trafficlight.gotoAndPlay('ready');
    }

    if (counter==3)
    {
    trafficlight.gotoAndPlay('go');
    }

    I know this is wrong though....

  3. #3
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Just put that last code inside the function:

    Actionscript Code:
    var myTimer:Timer = new Timer(2000,6);

    var counter:Number = 0;

    myTimer.start();

    myTimer.addEventListener(TimerEvent.TIMER, onRemind);

    function onRemind(evt:TimerEvent):void
    {
        counter += 1;
        trace("reminder "+ counter);
        if (counter==1)
        {
            trafficlight.gotoAndPlay('stop');
        }
       
        if (counter==2)
        {
            trafficlight.gotoAndPlay('ready');
        }
       
        if (counter==3)
        {
            trafficlight.gotoAndPlay('go');
        }
    }
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    It still doesn't work.

    I changed the code, and I currently have:

    import flash.utils.Timer;

    //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.

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Mind postin your FLA file?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Here it is so far...
    Attached Files Attached Files

  7. #7
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Mind downsaving it to CS5 or CS4?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Cs4.
    Attached Files Attached Files

  9. #9
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    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();
    [SIGPIC][/SIGPIC]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center