Timer (AS3)

Subscribe to Timer (AS3) 7 posts

avatar for LennonLenford LennonLenford 30 posts
Flag Post

I’m trying to make a timer that sets dialogue_txt.text to blank after a five second period. There aren’t any errors, but something isn’t working correctly because it’s not happening.

I’ve highlighted the areas where the problem most likely lies but it may be somewhere else.

Char.as:
http://pastebin.com/hSZXnaD5

Game.as:
http://pastebin.com/ZNCMM8bG

It would be great if I could also get a summary of what I did wrong. Thanks :)

 
avatar for NineFiveThree NineFiveThree 1378 posts
Flag Post

see Char.as line 55 and 56.

The adding of the Event listener happens within the function that you want to add.
The listener is added when the function is executed, which only happens when the listener is added beforehand.
You should add the Event listener somewhere else. Where do you want to start it.

This code is quite messy. You should not handle things with the static keyword this way. dispatch events instead.

Game.as line 24 to 45:
use an array, please

 
avatar for DrYoshiyahu DrYoshiyahu 678 posts
Flag Post

I have never used Timers. For that purpose I use Numbers.

var timer:Number = 0;
function enterFrame() {
    timer++;
    if (timer >= 600) {
        taDaaaa();
    }
}

PS. Line 31 says “if (selectDialogueTrue = true)” It should have == but you could also just use “if (selectDialogueTrue)” (and “if (!selectDialogueTrue)” if it were false)

 
avatar for Ace_Blue Ace_Blue 1130 posts
Flag Post

Measuring time and counting frames is not quite the same thing, though.

 
avatar for LennonLenford LennonLenford 30 posts
Flag Post

If I change my even listeners to before hand I get this error:
/Users/Lennon/Desktop/To Step In One’s Shoes/Char.as, Line 32 1120: Access of undefined property stopDialogue.

Strange because runDialogue didn’t give that problem. I know my code isn’t the best organized but I’m trying to fix this problem currently not organize my code.

public function spawnChar(e:Event):void

{
		stage.addEventListener(KeyboardEvent.KEY_DOWN, Key_down);
		stage.addEventListener(KeyboardEvent.KEY_UP, Key_up)
		stage.addEventListener(Event.ENTER_FRAME, enter_frame);
		Game.dialogueTimer.addEventListener(TimerEvent.TIMER, runDialogue)
		Game.dialogueTimer.addEventListener(TimerEvent.TIMER_COMPLETE, stopDialogue);
		
}
 
avatar for LennonLenford LennonLenford 30 posts
Flag Post

Okay. A friend told me that the timer is still not a good idea to use because it has all these issues. I’ve decided to use the way described by DrYoshiyahu. There is still an issue because the number only counts when the two objects are touching (I want it to keep going until it’s reached over the designated number).

 function enter_frame(e:Event):void{
			if (this.hitTestObject(People.People)){
			Game.selectDialogueTrue = true;
			
			if(!Game.selectDialogueTrue)
			Game.selectDialogue = Math.round(Math.random()* 4);
			Game.selectDialogueTrue = true;
			Game.dialogueTimerTrue ++;
			trace(Game.dialogueTimer)
			}else{
				if (Game.dialogueTimer >= 30){
				Game.selectDialogueTrue = false;
				Game.dialogueTimer = 0;
			}
		} 
 
avatar for dragon_of_celts dragon_of_celts 295 posts
Flag Post

First, why do you set Game.selectDialogueTrue = true both before and after if(!Game.selectDialogueTrue) when there is no intermediate possible changes? The number only counts when the two objects are touching because you have the counting code inside the hit test block. No?

Edit: Wait, why do you even have the if(!Game.selectDialogueTrue) statement when you just told it to be true? If there are lines of code not shown, you should probably have an indicator for such.