Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing variables 1

Status
Not open for further replies.

peasepud

Programmer
Jul 20, 2007
35
Hi all,

I have a simple countdown script and wish to pass a date and time to it.

I have tried a few things all to no avail, I think Im close but still dont get any result other than everything staying as 00:00:00:00 (the default text)

Im using the following:

var targetDate:Date = new Date(yr,mn,dy,hr,mn);

and then a URL of param name="movie" value="flashcount2.swf?yr=2007&mn=6&dy=28&hr=15&mn=00"

I know the script works normally because inserting a date into targetDate works fine so its something to do with passing the data. Do I have to set up the variables beforehand or do something to them before assigning?
 
oops realised I used mn twice as a variable so Ive changed the last one to mi, still doesnt solve the problem though.
 
It's working fine for me, created Date object "Sat Jul 28 15:00:00 GMT+0100 2007".

First you have to verify you are actually getting the variables into your movie. Note if you're using AS3 things have changed - if you need a code example let me know.

Kenneth Kawamoto
 
kenneth,

Thanks, a code example would be great, I know its going to be something silly but I cant quite get it. Heres what I have:

In my Actionscript (which is AS2):

var today:Date = new Date();
var currentTime = today.getTime();
var yr = _root.yr;
var mn = _root.mt;
var dy = _root.dy;
var hr = _root.hr;
var mi = _root.mi;
var targetDate:Date = new Date(yr,mn,dy,hr,mi);

All of that var = _root. stuff I added in later to see if that was the problem, Ive also tried just using the variables directly ie no var = statements just the var targetDate one and also var targetDate:Date = new Date(_root.yr,_root.mn,_root.dy etc etc)

The fla file can be found here >
and the resulting swf file is in
 
I haven't looked at your file, but in AS2, Date object accepts only Number data type as constructor parameters (in AS3 it is somewhat different).

When you pass variables from HTML, your variables can only be in String type, therefore you have to convert them to Numbers in Flash.

Here's an example - you don't need anything on Stage.
Code:
//AS2 main timeline
var yr:String, mn:String, dy:String, hr:String, mi:String;

var nYr:Number = Number(yr);
var nMn:Number = Number(mn);
var nDy:Number = Number(dy);
var nHr:Number = Number(hr);
var nMi:Number = Number(mi);

var targetDate:Date = new Date(nYr, nMn, nDy, nHr, nMi);

var tf:TextField = this.createTextField("tf", 1, 10, 10, 1, 1);
with (tf) {
	autoSize = true;
	text = targetDate;
}

stop();

Kenneth Kawamoto
 
Well I was hoping not to have to do this but I still cant get it to work, I now get NaN in the place of the the zeros.

Heres my actionscript:

this.onEnterFrame = function() {
var yr:String, mn:String, dy:String, hr:String, mi:String;
var today:Date = new Date();
var currentTime = today.getTime();
var nyr:Number = Number(yr);
var nmn:Number = Number(mn);
var ndy:Number = Number(dy);
var nhr:Number = Number(hr);
var nmi:Number = Number(mi);
var targetDate:Date = new Date(nyr,nmn,ndy,nhr,nmi);
var targetTime = targetDate.getTime();
var timeLeft = targetTime - currentTime;
var sec = Math.floor(timeLeft/1000);
var min = Math.floor(sec/60);
var hrs = Math.floor(min/60);
var days = Math.floor(hrs/24);
sec = string(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = string(min % 60);
if (min.length < 2) {
min = "0" + min;
}
hrs = string(hrs % 24);
if (hrs.length < 2) {
hrs = "0" + hrs;
}
days = string(days);
if (days.length < 2) {
days = "0" + days;
}
var counter:String = days + " : " + hrs + " : " + min + " : " + sec;
time_txt.text = counter;
}

Any help would be greatly appreciated.

Peter
 
Code:
var yr:String, mn:String, dy:String, hr:String, mi:String;
var targetTime:Number = new Date(Number(yr), Number(mn), Number(dy), Number(hr), Number(mi)).getTime();
var sDays:String, sHrs:String, sMin:String, sSec:String;
this.onEnterFrame = function() {
	var timeLeft:Number = targetTime-new Date().getTime();
	var sec:Number = Math.floor(timeLeft/1000);
	var min:Number = Math.floor(sec/60);
	var hrs:Number = Math.floor(min/60);
	var days:Number = Math.floor(hrs/24);
	sec %= 60;
	sec<10 ? sSec="0"+sec : sSec=""+sec;
	min %= 60;
	min<10 ? sMin="0"+min : sMin=""+min;
	hrs %= 24;
	hrs<10 ? sHrs="0"+hrs : sHrs=""+hrs;
	days<10 ? sDays="0"+days : sDays=""+days;
	time_txt.text = sDays+" : "+sHrs+" : "+sMin+" : "+sSec;
};

stop();

Kenneth Kawamoto
 
Thank you, thats a much tidier script and works great but can I be a pain and ask where mine was going wrong? No matter how much I search I cant seem to find the problem and I'll never learn otherwise :D

Thanks again, you've been a great help!
 
Your code was almost there but just a couple of points:

You're creating the target Date object again and again on onEnterFrame, which is not good. You only need to create it once at the beginning.

[tt]sec = string(sec % 60);[/tt]
This is illegal. "sec" is Number and you cannot assign a String to a Number. I think this was possible in AS1 but not in AS2/3.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top