Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

making a customized clock

New Here ,
Aug 11, 2011 Aug 11, 2011

Hello  I'm building a customized clock at the moment. For seconds i'm using 12 movieclips with bars inside (fig1)

For every second a bar lights up. I can get the number of "group" to pick and the number of "bar" to light up but i can't actually make them light up because I'm not sure how to actually select the group of bars according to the time. My movieclips are called "group0", "group1" etc. and inside I made different states depending on how many bars light up (fig2)

fig1secs.JPG fig2bars.JPG

Here's my code:

var now:Date;
var hour:Number;
var min:Number;
var sec:Number;
var group_access:Number;
var specificbar:Number;

addEventListener(Event.ENTER_FRAME, settime);

function settime (evt:Event) {
      //get time
      now = new Date();
   

      //check hours and set
      if (now.getHours() > 12) {
     hour = now.getHours() - 12;
      } else if (now.getHours()==0) {
       hour=12;
      } else {
       hour=now.getHours();
      }

   

      //set min and sec
      min=now.getMinutes();
      sec=now.getSeconds();
}

addEventListener(Event.ENTER_FRAME, setSecs);

function setSecs (evt:Event) {
//Task: Light up a bar every second

group_access = (Math.floor ( (sec*2 )/10) ); // to selects the right group of bars eg. 33 seconds will be group 6
specificbar = sec-(group_access * 5); // to light up the right bar inside the group

// Need help with this part to work out how to access the moviclip according to the seconds on the clock

}

Really worried right now, please help and thank you

TOPICS
ActionScript
771
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 12, 2011 Aug 12, 2011

if that's your trace output and you're trying to go to keyframes labeled bar4, bar0 etc, you should be using:

this ["group"+group_access].gotoAndStop("bar"+specificbar);

Translate
Community Expert ,
Aug 11, 2011 Aug 11, 2011

you want to "light up":

this["group"+group_access]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 11, 2011 Aug 11, 2011

Thank you so much i'm not getting any errors now but the bars are still not lighting up =(
i know it's selecting each group properly but it doesn't seem to be going to the different states in the each movieclip. I thought it might be because i labelled them with "bar" in front of the number but when i changed that it still doesn't work... maybe it's because i've written the code wrong? :

this ["group"+group_access].gotoAndStop(specificbar);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 11, 2011 Aug 11, 2011

specificbar doesn't look right.  use the trace() function to confirm:

trace(specificbar);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 11, 2011 Aug 11, 2011

ok i've traced :

trace(group_access , specificbar);

result:

3  4
3  4
3  4
3  4
4  0

4  0

4  0

seems right =S

group no.3 and bar no. 4

then group 4

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 12, 2011 Aug 12, 2011

if that's your trace output and you're trying to go to keyframes labeled bar4, bar0 etc, you should be using:

this ["group"+group_access].gotoAndStop("bar"+specificbar);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2011 Aug 13, 2011

You can make you code less verbose.

One should be careful with using event listeners, especially enter frame event. In your case, actually, it make more sense to use Timer.

In addition, int is faster and take less space than  Number. Also, using integers elevates the need to use Math functions like floor().

Here is an example of how your code can be optimized:

import flash.events.TimerEvent;

import flash.utils.Timer;

var now:Date;
var hour:int;
var min:int;
var sec:int;
var group_access:int;
var specificbar:int;
var timer:Timer;

init();

function init():void
{
     timer = new Timer(500);
     timer.addEventListener(TimerEvent.TIMER, settime);
     timer.start();
}

function setSecs():void
{
     group_access = sec / 5; // to selects the right group of bars eg. 33 seconds will be group 6
     specificbar = sec % 5; // to light up the right bar inside the group
     this["group" + group_access].gotoAndStop("bar" + specificbar);
}

function settime(e:TimerEvent):void
{
     now = new Date();
     hour = now.getHours() - 12;
     if (hour > 13)
     {
          hour = hour - 12;
     }
     else if (hour == 0)
     {
          hour = 12;
     }
     min = now.getMinutes();
     sec = now.getSeconds();
     setSecs();
     e.updateAfterEvent();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 15, 2011 Aug 15, 2011

wow thanks everyone much appreciated

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 15, 2011 Aug 15, 2011
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines