Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

elephant

macrumors newbie
Original poster
Feb 5, 2009
25
0
London, UK
I've used the tutorial and example from this link:

http://schoolofflash.com/2008/04/tutorial-creating-a-timer-in-actionscript-3/

...to build a simple countdown timer for my website. However, I don't really understand AS3, and I think it would be a massive waste of time learning the entire langauge just to be able to do the one thing I need to do:

The countdown is working as intended...BUT...it only counts down to a specific period in time.

I'd like for it to either:

1. Automatically start over every week, lets say tuesday (since this is Macrumors).

or

2. Count down to several specific anchor points. Lets say that it should count down to March 30th, and when it reaches zero, it starts counting down to April 14th.

The second behaviour would fit my needs perfectly, but the first behaviour is good enough.

Anyone care to teach me how to accomplish this?

Thanks. :)
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
Well, I don't know ActionScript, but here's the logic for at least one approach. You currently have a variable for the current end date for the count down. Instead of a single date variable, create an array of date variables. Then you'll loop through each date, and pick the first date that has yet to occur (that assumes dates are in chronological order). Then you'll feed that date into the existing countdown script.

Pseudo code:
PHP:
var dates = new Array(
  new Date(2008,03,30),
  new Date(2008,04,14)
);
var endDate = dates[0]; // give it something in case all dates have passed
var today = new Date();
for (var a=0, a < dates.length; a++) {
  if (today < dates[a]) {
    endDate = dates[a];
    break;
  }
}
Again, that's pseudo code, which means you can't just copy and paste it. When someone who knows ActionScript comes through perhaps they'll improve on this code.
 

lucidmedia

macrumors 6502a
Oct 13, 2008
702
37
Wellington, New Zealand
if you are not comfortable with AS3, am i right in thinking that the rest of your site is not in flash?

If so, you can find a lot of well-documented PHP countdown timers that are quite easy to configure.

If you are going the AS3 route, the tutorial has essentially already shown you how to add or subtract "time" using the date class.

Another option is to use the parse() method of the as3 date class. This will convert a date into unix time (milliseconds since Jan. 1 1970). You may find it easier to add or remove time from a date in this format. For example, you can add the number of milliseconds in a week to the current date (in unix format) to create a new end point for your timer.
 

elephant

macrumors newbie
Original poster
Feb 5, 2009
25
0
London, UK
if you are not comfortable with AS3, am i right in thinking that the rest of your site is not in flash?

If so, you can find a lot of well-documented PHP countdown timers that are quite easy to configure.

If you are going the AS3 route, the tutorial has essentially already shown you how to add or subtract "time" using the date class.

Another option is to use the parse() method of the as3 date class. This will convert a date into unix time (milliseconds since Jan. 1 1970). You may find it easier to add or remove time from a date in this format. For example, you can add the number of milliseconds in a week to the current date (in unix format) to create a new end point for your timer.
Correct, the only flash element on the website is that countdown timer. Because of basically one reason only: it has a rare custom font, and I'm using flash to embed it.

If the tutorial shows how to do what I want to achieve, I'll have to take another (closer) look...however, for someone with no knowledge at all regarding AS3, it's basically like reading chinese.

I understand how I change the date, and I have no problem setting any date I want. It's just the repitition part that I need to learn.
 

elephant

macrumors newbie
Original poster
Feb 5, 2009
25
0
London, UK
In another tutorial, I found this comment:

You would set up a function to check at some interval. Like this, but with proper indentation:

var timeCheck:Number = setInterval(checkTime, 25);
function checkTime():Void {
if(counter == 00:00:00:00){
gotoAndPlay(2);
}

So, to explain this code, you create the variable (it can be anything, I just used whatever came to my head first)and make it a number. You then use the set interval method to check the function that you put in the parentheses, at however many milliseconds you put in there, too. You then create the function that the setInterval method is going to run. Whatever variable you used to replace counter in your code from the original code up at the top of the page must be put in the if statement. So, when it reaches 0, it will goto and play (you can put in gotoAndStop if you want) frame 2 or whatever you have there.

Would my problem be easily solved by making a new counter (with a new end date) in frame 2, and then another in frame 3 (etc) perhaps?

The above code doesn't work though, but maybe someone could tell me an appropriate code to go to frame 2 when the timer reaches zero, and then I can solve the rest myself. :)
 

lucidmedia

macrumors 6502a
Oct 13, 2008
702
37
Wellington, New Zealand
You don't need frames for a dynamic animation like this... you want to use a timer, as the original tutorial did.

A timer is an As3 object that runs a certain chunk of code at regular intervals.

That way, your final movie will have only one frame, and the code triggered by the timer event will update the text field with you embedded font.
 

elephant

macrumors newbie
Original poster
Feb 5, 2009
25
0
London, UK
You don't need frames for a dynamic animation like this... you want to use a timer, as the original tutorial did.

A timer is an As3 object that runs a certain chunk of code at regular intervals.

That way, your final movie will have only one frame, and the code triggered by the timer event will update the text field with you embedded font.
Alright, and is there an easy way to explain how I do this? I feel like I'm almost there, as my countdown timer works, and I even managed to get it to stop and display a text when it reaches zero. :cool:
 

matius

macrumors newbie
Feb 16, 2009
1
0
Have you found a solution? I'm working on this very problem at the moment, except, it deals with random, rather than weekly events.

There's some work that goes into it. If I find something I'll post.

M
 

L3EOO

macrumors newbie
Feb 24, 2009
1
0
Alright, and is there an easy way to explain how I do this? I feel like I'm almost there, as my countdown timer works, and I even managed to get it to stop and display a text when it reaches zero. :cool:

I have recently created a countdown timer for an event we are having, how did you get text to display once the timer reached zero???
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.