Timer AI not working - actionscript-3

var moveTimer:Timer = new Timer(1);
moveTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void
{
//code
}
moveTimer.start();
moveTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone)
function timerDone(e:TimerEvent):void
{
upKey = false;
}
Hey Guys, so this is my code. I have some very simplistic AI in my game and I'm trying to utilize a timer in order for the enemy to move forward for about 2-3 seconds and then stop. To do this I'm using the variable upKey as a boolean which is set to true, but when the timer finishes it gets set to false and upon it being set to false, there is an if statement that will reduce the enemy's speed to 0.
This is my first time using a timer and the enemies dont really stop... they kind of just keep going until they wander off the screen. Am I doing this correctly or is it a problem elsewhere in my code? Also, is there a better more effiecient way to use a timer?
Thanks, James.

From the code you cite, the timer constructor does not specify repeatCount indicating it should repeat indefinitely. For the timerDone() handler to be called, you must specify a repeat count.
Also, note that a delay below 20-milliseconds is not recommended.
Timer constructor parameters: Timer(delay:Number, repeatCount:int = 0)
delay:Number — The delay between timer events, in milliseconds. A
delay lower than 20 milliseconds is not recommended. Timer frequency
is limited to 60 frames per second, meaning a delay lower than 16.6
milliseconds causes runtime problems.
repeatCount:int (default = 0) — Specifies the number of repetitions.
If zero, the timer repeats indefinitely, up to a maximum of 24.86 days
(int.MAX_VALUE + 1). If nonzero, the timer runs the specified number
of times and then stops.
Timers are not recommended for animated content. Instead, use Event.ENTER_FRAME to manipulate frame-based animation.
One approach would be to use timers to trigger state changes to your game model:
/** timer */
var timer:Timer;
/** whether enemies are advancing */
var advance:Boolean = false;
// start timer at 5-seconds intervals
timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
// animation controlled by Event.ENTER_FRAME
addEventListener(Event.ENTER_FRAME, frameHandler);
In your timer handler, you can adjust the timer delay depending on the state of your game.
/** timer handler */
function timerHandler(event:TimerEvent):void
{
// stop the current timer
timer.stop();
// depending on the current enemy state
switch (advance)
{
// if true, stop advancing and wait 5-seconds
case true:
trace("Stop advancing, wait 5-seconds");
timer.delay = 5000;
break;
// if false, advance for 2-seconds
case false:
trace("Advance for next 2-seconds");
timer.delay = 2000;
break;
}
// invert advance state.
advance = !advance;
// restart timer
timer.start();
}
Likewise on enter frame, control animation of your enemy based on game state:
/** frame handler, advancing enemy if 'advance' is true */
function frameHandler(event:Event):void
{
if (advance) { /** move enemy forward */ }
}
This alternates state of your enemies, outputting:
Advance for next 2-seconds
Stop advancing, wait 5-seconds
Advance for next 2-seconds
Stop advancing, wait 5-seconds
Advance for next 2-seconds

Related

Actionscript 3 Control Timeline after Duration of no input from user

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;
}
}

Remove a function from the stage

I have a big issue with my code
I have a function called "delayCallFuntions":
function delayCallFuntions(delay: int, func: Function) {
var timer: Timer = new Timer(delay, 1);
timer.addEventListener(TimerEvent.TIMER, func);
timer.start();
}
And I used this function like below to make connection between 2 point in my screen:
delayCallFuntions(1, function (e: Event) {timer011(wireColor);});
And function "timer011" is making the connections:
function timer011(firstColor: int): void {
wireColor = firstColor;
//GRID is a class
//Path A to B
var PathAB: Grid;
PathAB = new Grid(4, 5, 20, 17, canvas, wireColor);
this.addChild(PathAB);
}
My problem is:
I have several of these functions like "timer012", "timer013", ... that they need to execute one after another.
When I go out of this scene and come back again, these is still some of these functions are working while I need them to start from the beginning and go one by one.
for example: when i come back, "timer011" is starting while "timer016" is also completing at the same time.
hope someone can help me as this problem made me frustrated.
Currently you are creating a whole new timer everytime you add a function. That timer will stay in memory because of the event listener, and since it's encapsulated in the function, you have no easy way to reference it again to stop them.
What would be a better approach, is to create just one timer globally referenced so you can stop it if needed.
Here is a way you could accomplish this:
//create an array that will hold all the functions you are planning on calling
var delayedFuncs:Array = [];
//this var will be used to store the current function that will be called next
var currentFuncObj:Object = null; //set it to null so it clears the value when you return to this frame
//create a single, global timer reference for everything
//don't initialize it here though
//if you revisit this frame, you don't want to create a whole new timer, but keep using the previous one
var funcTimer:Timer;
//if the timer has already been created (you've been to this frame before), stop it
if (funcTimer) {
funcTimer.stop();
}else {
//if you haven't been to this frame before, create the timer and add the listener
funcTimer = new Timer(1,1);
funcTimer.addEventListener(TimerEvent.TIMER, nextFunc, false, 0, true);
}
//this function adds items to your queue. I've added the ability to also include parameters
function delayCallFunctions(delay:int, func:Function, ... funcParams):void {
//add an object to the array that stores the function, delay, and any parameters to pass to that function
delayedFuncs.push({delay: delay, func: func, params: funcParams});
//if the timer hasn't started running yet, start it since we've added something
if(!funcTimer.running) nextFunc();
}
//this function runs when the timer completes
function nextFunc(e:Event = null):void {
//if we have an existing function to call, call it
if (currentFuncObj){
//invoke the function with the parameters
currentFuncObj.func.apply(null, currentFuncObj.params);
}
//if there are still items in the array, grab the next one
if(delayedFuncs.length > 0){
//array.shift grabs the first element in the array and removes it from the array
currentFuncObj = delayedFuncs.shift();
//reset the timer
funcTimer.reset();
//set the appropriate delay
funcTimer.delay = currentFuncObj.delay;
//start the timer again
funcTimer.start();
}
}
So now, you'd use by doing:
delayCallFunctions(3000, trace, "hello", "world", "I'll be traced 3 seconds from now");
delayCallFunctions(2000, trace, "I'll be called 2 seconds after the last one");
Or, with your specific code:
delayCallFuntions(1000, timer011, wireColor);
Now at any time (say you hit a button to go to change scenes), you can just stop the global timer.
funcTimer.stop();

AS3 addChild after set time

For a game we are creating we need to have a movieclip 'pop up' after a certain amount of time, usually somewhere between 10 and 20 seconds. If this movieclip appears, the timer needs to be paused while the movieclip is active and the timer needs to be restarted after the movieclip disappears. Anyone knows how to do this?
import flash.utils.setTimeout;
// Your Sprite / MovieClip
var clip:MovieClip;
// The time until you want to add the first one
var timeToAdd:uint = Math.random() * 20;
// This will be the timer
var _timer:uint;
// This will add us to the stage after the random time. Second variable is seconds, so we need to multiply by 1000.
_timer = setTimeout(addToStage, timeToAdd * 1000);
// Called when the timer expires
function addToStage():void{
clip = new MovieClip();
// You would need logic to decide when to remove it, but once it is removed this will fire
clip.addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
}
// Called once removed
function onRemove(e:Event):void{
// Remove the event listener
clip.removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
// Restart the timer
timeToAdd = Math.random() * 20;
_timer = setTimeout(addToStage, timeToAdd * 1000);
}
The above code will add yout sprite to the stage once within 0.001 - 20 seconds. You'd need to add a bit of code to remove your sprite (removeChild(clip)).

How to Increase a timer AS3

Hey everyone cant really figure out the easiest approach to this problem.
Basically I have a timer that starts in the beginning of the game like so:
//Create new timer object
tEggTimer = new Timer (nTimerSpeed);
//Listen for timer intervals
tEggTimer.addEventListener(TimerEvent.TIMER, addEggs, false, 0, true);
//start timer
tEggTimer.start();
The nTimerSpeed is equal to (800);
Then I add the eggs like so:
private function addEggs(e:TimerEvent):void
{
//var eggGlobalPosition:Point = _Egg.localToGlobal(new Point(_Bunny.x, _Bunny.y));
_Egg = new mcEgg();
stage.addChild(_Egg);
_Egg.x = _Bunny.x;
_Egg.y = _Bunny.y + 30;
aEggArray.push(_Egg);
trace(aEggArray.length);
}
So in another enter frame function I want to change the value of the timer to (500), but whenever I try like so:
tEggTimer = new Timer (500);
tEggTimer.start();
like So:
private function updateDifficulty():void
{
if (difficultyUpdate) return;
if (nScore >= 2)
{
tEggTimer.removeEventListener(TimerEvent.TIMER, addEggs);
tEggTimer.stop();
tEggTimer = new Timer(200);
tEggTimer.addEventListener(TimerEvent.TIMER, addEggs);
tEggTimer.start();
But this doesnt do anything but stop the timer entirely.
What can I do in order to decrease the timer correctly?
Thanks guys.
If you just want to change the timer speed, while keeping everything else the same, you could just change the delay property in the timer object.
Sample here:
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
var speeds:Vector.<int> = new <int>[1000, 2000, 5000];
var currentSpeed:int = 0;
var timer:Timer = new Timer(speeds[currentSpeed]);
function timerTick(inputEvent:TimerEvent):void {
trace("Timer ticking: "+ getTimer());
}
timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
timer.start();
function clickedStage(inputEvent:MouseEvent):void {
currentSpeed = ++currentSpeed % speeds.length;
timer.delay = speeds[currentSpeed];
trace("Timer delay set to "+ timer.delay);
}
this.stage.addEventListener(MouseEvent.CLICK, clickedStage, false, 0, true);
Clicking on the stage will change the timer delay from 1 second, 2 seconds, 5 seconds and cycle. I'm just using getTimer() to show the rate of which the timer is ticking.
Note that it seems from the output, every time the value is changed, the timer will automatically restart.
timer.reset();
timer.delay = 2000;
timer.start();
May no be the best way but, instead using nTimerSpeed, make it run through every millisecond:
tEggTimer = new Timer (1);
Then in your AddEggs function use nTimerSpeed and a counter variable. counter is initialize to 0. Incase all your logic in an if statement, increment counter every time through function. if counter equals nTimerSpeed, allow for them inside the if statement and reset counter.
function AddEggs()
{
if(counter == nTimerSpeed)
{
//adding eggs logic
counter = 0;
}
counter++;
}
Did you try saying: tEggTimer.stop() just before re-instantiating with the 500 ms version? I'm guessing that your first Timer instance will just keep firing as your new one starts, unless you deliberately stop it.

How can we reset the repeatCount property while the timer is running

How can we reset the repeatCount property while the timer is running.
In a game a countdown timer starts running at 120. If the user clicks on "hint" button i need to reduce the time by 5 seconds and start displaying the countdown
Now the problem is the countdown timer is reduced by five but the Timer runs till "-n*5".
"n" being the number of times hint button clicked.
How to solve this issue?
Here's one approach to your problem. Maintain a timer count separate from the Timer's repeatCount, and stop when that separate counter hits 0, instead of when the TimerComplete event occurs.
public var t:Timer;
public var count:int = 120;
protected function init(event:FlexEvent):void
{
t = new Timer(1000,count);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();
}
protected function onTimer(evt:TimerEvent):void
{
count--;
//display count as time remaining
if (count <= 0)
{
//out of time!
t.stop();
}
}
protected function onHint(evt:MouseEvent):void
{
count-=5;
//update time or wait for next tick
}