0
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;


var timer:Timer = new Timer(1000);


start_btn.buttonMode = true;
stop_btn.buttonMode = true;
start_btn.addEventListener(MouseEvent.CLICK, onStart, false, 0, true);
timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
//stage.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
function onStart(evt:MouseEvent):void
{
    var minutes:Number = Number(min_txt.text);
    var seconds:Number = Number(sec_txt.text);
    timer.start();
}

function onTimer(evt:TimerEvent):void
{
    minutes--;
    trace("Timer Triggered!!");
}

So how do i make it so that the "minutes--" works ..as the variables are in a seperate function..

(or give me another way)..

Thanks..

1
  • 1
    change the scope of minutes so that it is accessible to both functions. Commented Feb 24, 2013 at 15:08

1 Answer 1

1

if you declare variable in function, is a local variable. you not access variable other function, other scope. but if you declare in global variable. available anywhere.

Easy way, if you variable declared globally. It's available.

import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

var timer:Timer = new Timer(1000);

start_btn.buttonMode = true;
stop_btn.buttonMode = true;
start_btn.addEventListener(MouseEvent.CLICK, onStart, false, 0, true);
timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
//stage.addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
var minutes:Number;
function onStart(evt:MouseEvent):void
{
  minutes = Number(min_txt.text);
  var seconds:Number = Number(sec_txt.text);
  timer.start();
}

function onTimer(evt:TimerEvent):void
{
  minutes--;
  trace("Timer Triggered!!");
}
Sign up to request clarification or add additional context in comments.

7 Comments

are you updated your textfield at onTimer handler? below minutes--; line as follows: min_txt.text = String(minutes);
yes i did that too...and right now... i created a test variable var test:Number; and then gave it a value inside the "onStart" function.. test = Number("5");..and then i traced "test--" in the onTimer function...and its working like a charm...why is it not working with a text field??
I really don't understand. I wonder what exactly problem. umm.. would you give me a source code link?
Look at your original code, the variables was increased, good but problem that the variable is initialized as a textfield string value. your textfiled value is 0. so minutes 1->0 this statement infinite loop.
Here is solution: datafilehost.com/download-f5246402.html I perfectly corrected your code. you should closely look at the code :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.