I am trying to program a Phidgets device to repeatedly turn an external light on for .5 sec and off for .5 sec.
(these times correspond to Frames 1 and 11 of a 20 frame movie clip.
I've tried to insert the on- and off- commands into the movie clip, but they don't transfer to the main stage, so now I'm just trying to program the light to go on and off from the main program
The code below turns the light on and off at the SAME TIME- which means NOTHING HAPPENS.
Does anyone know how to delay the timer so that the off command comes 500 msec AFTER the on command ?
THanks
phid.addEventListener(PhidgetEvent.ATTACH,onAttach);
//connects Phidget to software
function onAttach(evt:PhidgetEvent):void{
trace(evt);
}
var phidControl:Timer = new Timer (500);
phidControl.addEventListener(TimerEvent.TIMER, lightOn);
function lightOn (evt:TimerEvent):void {
phid.setOutputState(0, true);} //this turns the light on
enter code here
var phidOff:Timer = new Timer(500);
phidControl.addEventListener(TimerEvent.TIMER, lightOff);
function lightOff(evt:TimerEvent):void {
phid.setOutputState(0, false);//this turns light off
}
it looks like you're creating two Timers which both run at a 500ms interval simultaneously.
It might be easier to put one function on a 500ms timer, which will call either function intermittently, something like
var lightIsOn = false;
function lightToggle(evt:TimerEvent):void{
if(lightIsOn){
lightOff();
}else{
lightOn();
}
lightIsOn = !lightIsOn;
}
---- UPDATE ----
I'm not very familiar with Phidgets, however that shouldn't make a difference in this case. Try this:
val = true;
var phidControl:Timer = new Timer(500); //If you don't set the repeat count it goes on forever
phidControl.addEventListener(TimerEvent.TIMER, lightToggle);
function lightToggle(evt:TimerEvent):void {
val = !val;
phid.setOutputState(0,val);
}
phidControl.start(); //Make sure you start the timer.
If you only want to switch it once every .5 seconds then you don't have to worry about reading the state of the light, just tell it when to change. So from what I can tell, you don't need the inputChange function.
Related
I am new to actionscript.
I want to know how to run timer class and keep running even when i go to other frame.
For example. I run timer in frame 2 and when i go to frame 1 timer keeps running, until i go back to frame 2 to stop the timer.
I appreciate the help.
UPDATE :
I try to make a stopwatch with these code :
var sec,min,hr:uint = 0;
sec_txt.text = min_txt.text = hr_txt.text = "00";
var timerStopWatch:Timer = new Timer(1000);
timerStopWatch.addEventListener(TimerEvent.TIMER, timerHandler);
function timerHandler(e:TimerEvent){
var sec:String = String(uint(timerStopWatch.currentCount%60));
var min:String = String(uint((timerStopWatch.currentCount/60)%60));
var hr:String = String(uint((timerStopWatch.currentCount/60)/60));
sec_txt.text = (uint(sec)< 10) ? "0" + sec:sec;
min_txt.text = (uint(min)< 10) ? "0" + min:min;
hr_txt.text = (uint(hr) < 10) ? "0" + hr:hr;
}
When i start the timer it work, but when i go to the second frame or another it stop and show a output error :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Stopwatch_fla::MainTimeline/timerHandler()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
What i want is, when i start the Timer and go to the other frame it will still running(ticking) and when i go back to the Timer frame i could stop it.
First, if you're going to be re-visiting the frame where your posted code lives, you want to make sure that timer doesn't get re-created every time and cause a memory leak.
Change the following line
var timerStopWatch:Timer = new Timer(1000);
To these two lines:
var timerStopWatch:Timer;
if(!timerStopWatch) timerStopWatch = new Timer(1000);
This will ensure your timer is only created once. That event listener will keep the timer in memory, so if you're constantly creating new timers and adding a listener to them, they will all stay in memory and your application will get slower and slower.
Second, make sure you clean up after yourself.
When you want your timer to be gone, stop it, remove the event listener and null the reference:
timerStopWatch.stop();
timerStopWatch.removeEventListener(TimerEvent.TIMER, timerHandler);
timerStopWatch = null;
Third, if your going to have this object span multiple frames, anything that is referenced in the event handler needs to persist in those frames as well.
That means:
sec_txt
min_txt
hr_txt
All have to exist on any frame where this timer is running. Make sure on those frames that they have their instance names set properly.
OR, check if they exist prior to using them:
if(sec_txt) sec_txt.text = (uint(sec)< 10) ? "0" + sec:sec;
If you have this problem you are probably typing code in the frame itsel. Write a document class and create your timer in there, so no matter which frame, all properties are still there and keep running.
At the moment I am putting together a timer that is supposed to add a movieclip to the stage at a random moment within 20 seconds, but it has to count a full 20 seconds and then reset.
Now like this I can make the timer countdown for a random time up to a maximum of 20 seconds, but it repeats right after that random time. So I was thinking of a Timer delay of 20000 - whatever time the randommath + 20000 has produced. However I can't think of a way to reference the produced time.
Anybody out there who knows of a way to accomplish this? Or maybe a workaround with another method?
Thanks in advance!
var treasureclip:MovieClip= new xClip;
var treasureTimer = new Timer(Math.random() * 20000);
treasureTimer.addEventListener("timer", placeTreasure);
treasureTimer.start();
function placeTreasure(evt:TimerEvent):void {
addChild(treasureclip);
Timer(evt.currentTarget).delay = 20000 - //something
}
import flash.utils.setTimeout;
var treasureclip:MovieClip= new xClip;
/*to check it adds movieClip uncomment this and comment previous
var treasureclip:MovieClip= new MovieClip;
with(treasureclip.graphics)
{
beginFill(0);
drawRect(0,0,50,50);
}*/
var timeout:uint;
startTimeout();
setInterval(startTimeout, 20000);
function startTimeout():void
{
timeout = setTimeout(placeTreasure, int(20000 * Math.random()));
}
function placeTreasure():void {
addChild(treasureclip);
}
//Don't forget to kill timeout with clearTimeout(timeout) to avoid memory leaks
I've run across a piece of code that I'm really struggling to get working... It's meant to wait three seconds before fading an object (swslogo), however when I test it, it doesn't seem to work.. anyone know why this might be
var GameMode:Number = 0;
swslogo.alpha = .0;
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
intro();
function intro(e:Event=null):void
{
IntroTimer.reset()
IntroTimer.start();
}
function swsfadein(e:Event=null):void
{
IntroTimer.stop();
swslogo.addEventListener(Event.ENTER_FRAME, fadein)
}
function fadein(e:Event=null){
if(swslogo.alpha <=0){
this.alpha +=0.1;
if(swslogo.alpha >=1){
this.removeEventListener(Event.ENTER_FRAME, fadein);
}
}
}
Edit: Removed accidental line that wasn't meant to be there
From your last few questions I can tell you totally need to learn what a program is, in general, unrelated to AS3 of any other given language, instead of struggling with random pieces of code while treating them like magic spells.
// This part is fine.
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
// A method is called. It resets and restarts timer.
intro();
// Another method is called. It stops the times and starts the fading thing.
swsfadein ();
So basically that code in a single go:
Creates timer.
Starts timer.
Stops timer.
Starts fading.
Which obviously suppresses the intended use of the timer.
What I want to do is:
After colliding with an [object], I want the screen to flash for about half of a second. I have tried for loops and while loops but they seem to not work. I have no idea how I should program this.
I've been trying to figure out how to do this since I'v been making the game so it would be helpful if someone could help me.
Thank you for reading.
You need to use something that involves time. loops all run in a thread which doesn't pause for time - which is why they don't work.
Here is how you could do this with an AS3 Timer (let's say this code runs right after you've determined there's been a collision)
function flashScreen():void {
var timer:Timer = new Timer(50, 10); //run the timer every 50 milliseconds, 10 times (eg the whole timer will run for half a second giving you a tick 10 times)
var flash:Shape = new Shape(); //a white rectangle to cover the whole screen.
flash.graphics.beginFill(0xFFFFFF);
flash.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
flash.visible = false;
stage.addChild(flash);
timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
//we've told AS3 to run this every 50 milliseconds
flash.visible = !flash.visible; //toggle visibility
//if(Timer(e.currentTarget).currentCount % 2 == 0){ } //or you could use this as a fancy way to do something every other tick
});
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
//the timer has run 10 times, let's stop this flashing madness.
stage.removeChild(flash);
});
timer.start();
}
Other ways you can do this are with setInterval, setTimeout, a Tweening library,and an ENTER_FRAME event handler.
I'm trying to make a dice game in Flash/ActioScript 3. I did all the essentials and it works smoothly. Now I want to improve the user experience. For instance, when it's computer's turn (to roll and do things according to die value) I want to animate the die. The die has 6 keyframes. So, for, say, 2 seconds the die will loop those 6 frames then it will stop on a value (depending on random generator). Somehow I can't do it as I want. How can I write a function(s) so that when I say,
animateDice()
it will do nothing but just animate the dice for a specified interval?
Update:
var timer:Timer = new Timer(10, 50);
myButton.addEventListener(MouseEvent.CLICK, onClick);
timer.addEventListener(TimerEvent.TIMER, animateDice);
function onClick(event: Event):void {
timer.start();
}
function animateDice(event: Event):void {
dice.play();
}
For instance, I don't understand why the above code doesn't work properly. It does work properly on first click, but not there after.
Update 2: I guess I'm still having problems. How do I suspend the running code until the timer stops? (Yes there is a work around---putting timer handlers inside other timers, etc. Is there an easy way?
Maybe, this will help:
First we see the die rolling (and a message box informs the user that the game will decide whom starts). Then it's either Human's or Computer's turn. When it's computer's turn, first we see the rolling die again for, say, 1 second. Then it stops and and we see the outcome. I'm a beginner and I nay be missing something, but from what I see it seems that all these simple steps (just showing the die rolling for some time) means lots and lots of lines.
If I use a simple timer for die animation, the script continues and the whole show goes away.
The timer object has three properties:
delay, or how often the event should fire
repeatCount, or how many times the event should fire
currentCount, or how many times the timer's event has fired thus far
You are creating the timer with new Timer(10, 50), which sets delay to 10 and repeatCount to 50. This means that, once you call timer.start(), timer will fire TimerEvent.TIMER every 10 milliseconds. Each time it is fired, it adds 1 to currentCount. When currentCount is greater than or equal to repeatCount (50), it stops looping the timer.
Once your timer has stopped, if you call timer.start() again, it will only fire the event once, because currentCount has not been reset to zero, and is still >= repeatCount.
If you call timer.reset() before calling timer.start(), it will set this value to zero and things should behave as expected.
var timer:Timer = new Timer(2000, 1);
myButton.addEventListener(MouseEvent.CLICK, onClick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
function onClick(event: Event):void {
timer.reset();
timer.start();
dice.play();
}
function onTimerComplete(event:TimerEvent):void {
var roll:int = int(Math.floor(Math.rand()*6))+1;
dice.gotoAndStop(roll);
}
The timer is set to run only once, for 2000 milliseconds (which are 2 seconds). When Click occurs, the timer is reset (so that if it's not the first time it was clicked, it will run as if it was the first time) and started, and the animation starts a well. After 2 seconds, TIMER_COMPLETE will be fired by the timer, and we catch it and determine a final number for the die, then gotoAndStop to that frame.
I didn't try to compile the code, but the gist of it should work for you.
P.S, dice is the plural of 'die' :) you're skipping a great opportunity for the type of variable names we all want to use but can't!
You could try something a little more like this:
var t:Timer = new Timer(10, 50);
t.addEventListener(TimerEvent.TIMER, timerHandler);
t.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
t.start();
function timerHandler(e:TimerEvent):void {
gotoRandomFrame();
}
private function timerCompleteHandler(e:TimerEvent):void {
var finalNum:int = gotoRandomFrame();
// Using finalNum
}
private function gotoRandomFrame():int {
var num:int = Math.floor(Math.random() * 6) +1;
dice.gotoAndStop(num);
return num;
}
So use gotoAndStop to set your frame rather than using play