Hi,
I'm trying to learn how to use a Timer and AS3 to set up 2 objects (for example, 2 squares, each with a separate instance name) that would show/hide alternately for 500 ms each for 30 seconds (30000 ms) total. Easy enough to do using a timeline, but instead, I need to set this up to include using AS3 to add to a companion .as script file (Main.as), which loads and all runs on Frame 1. I first looked at using setTimeout or setInterval, but these don't seem to have enough properties to do what is needed. So, Timer is probably the best choice. I've read the online documents and examples for a Timer, but still can't figure out how for example to alternate one object's 500 ms show with the other's 500 ms hide, then alternate/switch them, and stop the alternating sequence after 30 seconds.
Any help, example AS3 script would be great help.
Regards,
Hi Ned,
Unfortunately that's what I have been trying, without success. Actually, have gotten fairly stuck in experimenting and am hoping that someone can provide a simple example. The following is some of the "gibberish" that I tried without success.
private var c1On: uint = 500
private var c1Off: uint = 500
private var c2On:uint = 500
private var c2Off:uint = 500
private var c1OnTimer:Timer
private var c2OnTimer:Timer
c1OnTimer = new Timer(c1On, 30);
c1OnTimer.addEventListener(TimerEvent.TIMER, c1OnF);
public function c1OnF(event:TimerEvent) {
mcsquare1.visible = true
mcsquare2.visible = false
}
c2OnTimer = new Timer(c2On, 30);
c2OnTimer.addEventListener(TimerEvent.TIMER, c2OnF);
public function c2OnF(event:TimerEvent) {
mcsquare2.visible = true
mcsquare1.visible = false
}
c1onTimer = new Timer(c1On, 30);
c1onTimer.addEventListener(TimerEvent.TIMER, c1onF);
//onTimer.start();
}//end
public function c1onF(event:TimerEvent) {
mcsquare1.visible = true
mcsquare2.visible = false
}//end c1onF
c1offTimer = new Timer(c1Off, 30);
c1offTimer.addEventListener(TimerEvent.TIMER, c1offF);
//onTimer.start();
}//end
public function c1offF(event:TimerEvent) {
mcsquare1.visible = false
mcsquare2.visible = true
}//end c1onF
Yeah, it looks like you are over-thinking it. One timer should be all you need.
The problem with giving you a simple example is that the simplest example is the solution to what you say you are trying to learn how to do. Doing it for you is not you learning how.
As I said, you just need to make the one object's visible property the oppostite of what the other is each time you change the other...
mcsquare1.visible = !mcsquare1.visible; // switches the visible state to the opposite
mcsquare2.visible = !mcsquare1.visible; // ditto
Yes, I've been coming up with far too complex script (that doesn't work), when it should be simple.
If I set up a simple Timer with your suggestion,
mcsquare1.visible = !mcsquare1.visible; // switches the visible state to the opposite
mcsquare2.visible = !mcsquare1.visible; // ditto
would I need one or two timers? One for the visible square1 and not visible square2, alternating with one for the visible square2, not visible square1? How do I get the second timer (assuming two timers needed) to fire 500ms after the first, then repeat this pattern for 30 seconds? I've been lost trying to figure out both how to create a simple script and then implement it in AS3.
Regards,
OK, I think it means that one timer can do the job.
So, something like?:
private var c1On: uint = 500
private var c1OnTimer:Timer
c1onTimer = new Timer(c1On, 30);
c1onTimer.addEventListener(TimerEvent.TIMER, c1onF);
}
public function c1onF(event:TimerEvent) {
mcsquare1.visible = !mcsquare1.visible;
mcsquare2.visible = !mcsquare1.visible;
}
Not sure this will work, how to trigger in a button.
Regards,
North America
Europe, Middle East and Africa
Asia Pacific