Copy link to clipboard
Copied
Hey guys,
Maybe simple question but I dont find any solution right now...
I have a number counting up from 0 to 86400.
When I'm pressing a key, I want to get the CURRENT number to be saved into a new variable. This new variable has to be static (not counting).
But the Basic counting have to go on.
Hope you know what I mean and can help me 😃
I think you could use a Global Array.
var laps:Array=new Array(); //out of all functions
laps.push(value); // inside your "HerdOn"
Each time you press a correct key is that mark would add to the array, you would have ordered marks and delete would use:
laps=new Array();
Copy link to clipboard
Copied
To get a key to trigger something you need to make use of a KeyboardEvent listener
stage.addEventListener(KeyboardEvent.KEY_UP, assignValue);
function checkText(event:KeyboardEvent):void {
yourVar = currentCount;
}
Copy link to clipboard
Copied
getting the key pressing work is not the problem, my problem is: How to get this Number.
The Number is counting up, and anytime I press the key i get the value in a new variable
Copy link to clipboard
Copied
Maybe you should show your code then, because if you do not know how to assign a new variable but can deal with the keyboard end of things, there is a disconnect with what your ability appears to be and what you say it is.
Copy link to clipboard
Copied
Ok here is my code:
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
import com.greensock.*;
import com.greensock.easing.*;
////////////////////////DAYTIME IN SECONDS
var today:Date;
var current:Date;
var time:Date;
var millisecond:int;
var second:int;
var minute:int;
var hour:int;
var day:int;
var month:int;
var secs:int;
var timer:Timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
function timerHandler(event:TimerEvent):void
{
time = new Date();
millisecond = time.getMilliseconds();
second = time.getSeconds();
minute = time.getMinutes();
hour = time.getHours();
day = time.getDate();
month = time.getMonth();
today = new Date(2012,5,28,0,0,0,0);
current = new Date(2012,month+1,day,hour,minute,second,millisecond);
secs = (current.getTime() - today.getTime())/1000;
trace(secs); //"secs" is the current daytime in seconds
stage.addEventListener(KeyboardEvent.KEY_DOWN, HerdOn);
////////////////////////EVENT
function HerdOn(event:KeyboardEvent):void
{
var xPos:Number = -411.10;
var yPos:Number = -8.05;
var pixWidth:int = 0;
if (event.charCode == 49) // key "1"
{
trace ("start");
//So, when I have pressed the key "1", here I want to have a new Variable which gives me a static number
//of "secs"
currentCount
trace ("One is pressed");
bob_mc.x = xPos;
bob_mc.y = yPos;
bob_mc.width = pixWidth;
TweenLite.to(bob_mc, 15.25, {x:xPos, y:yPos, scaleX:2, ease:Linear.easeNone});
}
if (event.charCode == 50) //key "2"
{
trace("stop");
TweenLite.to(bob_mc, 15.25, {paused:true, ease:Linear.easeNone});
}
}
}
Copy link to clipboard
Copied
I answered your question yeaterday regarding how to get the seconds (you should mark that posting as answered). You should clean up that code so that it is not doing things that you don't need done - which appears to be most of the date functions you run.
Also, you should never nest function within other functions. So you should separate the HerdOn function from being inside the timeHandler function.
If you want another variable, then outside any function declare a new variable, and then assign the secs value to it inside your keyboard handler function.
Copy link to clipboard
Copied
Ok I understand, maybe this is my big problem, so
How do I call a function or a variable in another function, even the variable is local. I thought it only works with global variables.
For me it's a bit cunfusing, why I put function into function.
Now, when I seperate the function, where I calculate the daytime in seconds, I have to call it again when I press my key, right?
How? (sorry for my inexperience xD)
Copy link to clipboard
Copied
I think you could use a Global Array.
var laps:Array=new Array(); //out of all functions
laps.push(value); // inside your "HerdOn"
Each time you press a correct key is that mark would add to the array, you would have ordered marks and delete would use:
laps=new Array();
Copy link to clipboard
Copied
Thanks for your help^^ Works fine 😃
Copy link to clipboard
Copied
you're welcome
Copy link to clipboard
Copied
Declare your variable outside any functions so that it is available outside of any function if you need it to be, juist like the rest of the vars you declared. If you need to store different values at different times and want to retain them all, use an array to store them in.