Okay, so I am a very amateur AS3 programmer. I have a timer setup, and after 45 seconds it should move to scene 6, however if it calls hitTestObject, it should stop and reload from 0 when the scene reloads. EDIT: I know this code is probably really bad coding, I'm also taking tips on how to fix this code up. Here's my code:
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, onEnterFrame);
myTimer.start();
{MAIN FUNCTION}
function onEnterFrame(e:Event):void {
var startTime:int=getTimer();
var currentTime:int=getTimer();
trace(currentTime);
if (currentTime>45000){
gotoAndStop(1, "Scene 6");
}
}
My issue, is that the timer keeps running when it hits test object and so when scene 3 is reloaded, the timer just keeps going. Therefore you only have to play for a total of 45 seconds, no matter how many times you die. It should be that once you die the timer reloads when you reload scene 3. Any ideas on what I can do?
Hope this helps:
var myTimer:Timer;
function resetTimer():void
{
// check if timer already initialize
if(myTimer != null)
{
// just reset
myTimer.stop();
myTimer.reset();
}
else
{
// initialize timer and set a 45 sec delay;
myTimer = new Timer(45*1000,1);
myTimer.addEventListener(TimerEvent.TIMER, handleTimerTick);
}
myTimer.start();
}
function handleTimerTick(event:TimerEvent):void
{
// stop and null timer;
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
myTimer = null;
// goto my 6th scene
gotoAndStop(1, "Scene 6");
}
// whenever a hit test is performed and a timer reset is needed just call
resetTimer();
Related
Turn off when the programa is not touched for 5-10 minutes.
I am using timer
Even when the program is touched, it closes when the time is up
How can i solve it?
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
function timerListener (e:TimerEvent):void{
fscommand("quit");
}
myTimer.start();
myTimer.reset(); reset it and then start it again myTimer.start(); you just have to put that in some event handler that indicates "activity" - perhaps every n time to keep it from firing a lot
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
function timerListener (e:TimerEvent):void{
fscommand("quit");
}
myTimer.start();
I won't dive into the custom event class but there are a good number of sources for that but basically use the .reset() and .start() in those.
For example
https://gamedev.stackexchange.com/a/12230
https://stackoverflow.com/a/23559690/125981
Here is a simple example to study...
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, reset_Timer); //check for any mouse movement
myTimer.start();
function timerListener (e:TimerEvent) :void
{
//# function happens when Timer amount is reached (eg: mouse did not move to reset it)
//choose one below..
//fscommand("quit"); //# close app
//myTimer.stop(); myTimer.start(); //# stop and then restart Timer
//stage.removeEventListener(MouseEvent.MOUSE_MOVE, reset_Timer); //# cancel any further usage of this function
}
function reset_Timer (e:MouseEvent) :void
{
//# function happens after mouse not moved for total millisecond count of Timer amount
myTimer.reset(); //reset countdown because mouse was moved
}
can anyone help me with this. I know its something very basic, but I just cant work it out.
What I need is for the timeline gotoandstop at frame 1 after 15 seconds of inactivity.
Basically this is for a directory board so if no one is using it, it will return back to the home screen after a period of inactivity.
Any help would be greatly appreciated.
Thankyou
What you can do, is use a Timer object. Then, whenever the user moves the mouse or clicks or presses a key, reset that timer back to 15 seconds.
On your frame 1, make a timer object:
//create the timer object var
var resetTimer:Timer;
//if it doesn't exist yet, create a new timer object and assign it to that var
if(!resetTimer){
resetTimer = new Timer(15000,1); //tick 1 time with a delay of 15
//listen for the TIMER event (fires when the delay is up)
resetTimer.addEventListener(TimerEvent.TIMER, reset);seconds
}else{
resetTimer.reset(); //if it did previously exist, stop/reset it (for when you revisit frame 1)
}
//go back to the first frame if the timer fires
function reset(e:Event = null):void {
resetTimer.reset(); //reset the timer
gotoAndStop(1); //go to frame 1
}
//LISTEN for various user input type events on stage (globally)
stage.addEventListener(MouseEvent.MOUSE_DOWN, userInput);
stage.addEventListener(MouseEvent.MOUSE_MOVE, userInput);
stage.addEventListener(KeyboardEvent.KEY_DOWN, userInput);
stage.addEventListener(KeyboardEvent.KEY_UP, userInput);
//if there was user input, reset the timer and start it again
function userInput(e:Event = null):void {
resetTimer.reset();
resetTimer.start();
}
The only thing left to do is, when you leave frame 1 and want the timeout to be applicable call resetTimer.start(). Presumably that would be on frame 2.
its possible to simulate it so:
class test extends MovieClip{
public var myTimer:Number;
public var input:TextField;
function test(){
myTimer=0;
input=new TextField();
this.addChild(input);
this.addEventListener(Event.ENTER_FRAME,timer);
input.addEventListener(Event.CHANGE, input_from_user);
}
function timer(ev){
myTimer +=(1/25);//if the frame rate is 25 frame per sconde
if(myTimer ==15){
this.gotoAndStop(1);
this.removeEventListener(Event.ENTER_FRAME,timer);
}
}
function input_from_user(ev){
myTimer =0;
}
}
I'm making a game in actionscript 3.0. I currently have a countdown timer (counts down from 30 seconds). Once the 30 seconds is up I want the frame to move from frame 2 to frame 3. I have put gotoAndStop(3) in the code under the timer but when the timer starts it goes to frame 3 straight away. It doesnt go to frame 3 when the 30 seconds is up. I would appreciate any help at all!
var nCount:Number = 30;
var myTimer:Timer = new Timer(1000, nCount);
timer_text.text = nCount.toString(nCount);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void
{
nCount--;
timer_text.text = nCount.toString();
gotoAndStop(3);
}
You are calling gotoAndStop(3); on the very first timer event, i.e. after one second since you are not checking the value of nCount. You need to call gotoAndStop(3); only when nCount is zero.
function countdown(e:TimerEvent):void {
nCount--;
timer_text.text = nCount.toString();
if (nCount == 0) {
gotoAndStop(3);
}
}
Now I am also using a timer in frame 8, which is my Gamescreen frame to try and create an energy bar, so decreasing by 1 every second, and everytime the character collides with an object then increment the value of count by 1 (which in my min is 1sec, right?), however the timer runs out prematurely, when the label is showing 3secs left after collecting 3 items the timer automatically ends, HELP ME! :)
var count:Number = 5; (temporary value for testing)
var theTimer:Timer = new Timer(1000, count);
theTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
theTimer.start();
function whenTimerComplete(e:TimerEvent):void
{
theTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, whenTimerComplete); //Remove listener
gotoAndStop("frameFive"); // Advance to score screen.
}
theTimer.addEventListener(TimerEvent.TIMER, theCountdown);
function theCountdown(e:TimerEvent):void
{
count--;
timerLabel.text = count.toString()
}
//Start the timer and show in the label.
timerLabel.text=count.toString();
theTimer.start();
All help and a solution is VERY much appreciated.
Here's an example countdown timer:
Launch Flash example
FLA source code
Countdown Timer AS3 source code
CS6 ZIP of source code
CS5 ZIP of source code
Create a countdown timer class at the root of your FLA:
CountdownTimer.as
package {
import flash.events.TimerEvent;
import flash.utils.Timer;
public class CountdownTimer extends Timer {
public var time:Number = 0;
public function CountdownTimer(time:Number = Number.NEGATIVE_INFINITY, delay:Number = 1000) {
super(delay, repeatCount);
if (!isNaN(time))
this.time = time;
repeatCount = Math.ceil(time / delay);
addEventListener(TimerEvent.TIMER, timerHandler);
}
protected function timerHandler(event:TimerEvent):void {
time -= delay;
if (time == 0)
dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
}
public function dispose():void {
removeEventListener(TimerEvent.TIMER, timerHandler);
}
}
}
On the timeline of your FLA, create a timer with the total number of milliseconds to countdown:
var timer:CountdownTimer = new CountdownTimer(60000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();
In the example above, the timer will countdown for 1-minute (60-seconds). Each second the timerHandler will be called. When it reaches 0, the timerCompleteHandler will be called.
function timerHandler(event:TimerEvent):void {
timerText.text = (timer.time / 1000).toString();
}
function timerCompleteHandler(event:TimerEvent):void {
timerText.text = "COMPLETE";
}
To add time to the timer, add milliseconds to time. If you want the timer to dispatch timer complete event when it reaches 0, update the repeatCount:
timer.time += 1000;
timer.repeatCount += 1;
Likewise to remove time from the timer, subtract milliseconds from time; and again, if you want the timer to dispatch timer complete event when it reaches 0, update the repeatCount:
timer.time -= 1000;
timer.repeatCount -= 1;
I am currently in a Flash game programming class with action script 3 and can't seem to find a clock anywhere that counts down and then does an action. I've been using Lynda tutorials and that hasn't been helpful and I have also Google searched quite a bit, but no luck. I have a Countdown clock and have been try "if" statements, but no luck.
Can someone guide me in the right direction on what I am doing wrong?
//game timer
var count:Number = 60; // amount of time
var myTimer:Timer = new Timer(1000,count); // time in ms, count is calling from line above
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
}
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
This should help ;) Without any magic numbers.
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(2);
}
Add your if statement inside countdown like so:
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
}