Skip to main content
Dave8585
Participating Frequently
October 20, 2021
Answered

Beginner needs help coding his live day night animation

  • October 20, 2021
  • 1 reply
  • 1055 views

hi
i made a cool floating cloud animation wich i want to add some code to change the background to day or night depending on the intern windonws clock... so for example

if it is 06-21 o clock i want the background image to be day image 1...
if the time is 21:01 -05:59 i want the background image to be night image 2....

i already googled for hours but all i could find was css or javascript and im a total beginner with coding so im not sure how to use it with action script...

i found alot of ways to get time and hours ( also on adobe documentation) 
but im not sure how to use all these codes...
this  here should basically do what i want... 
https://www.webdeveloper.com/d/194549-loading-different-images-based-on-time-of-day
i applied this code to the  global script section but when i test it inside the broswer,  it just shows me a blank screen with a broken image logo with text DJimage ..
i changed all file names and path accoringly to my stuff but still doesnt work...

i also tested to put the code on the background layer or the background object and then nothing happens at all .. .i get the normal animation inside my browser...
can anybody point me in the right direction ?

any help would be appriciated.
thanks alot 
dave

    This topic has been closed for replies.
    Correct answer kglad

    but it doesnt work... thats why im asking here...
    how should it be setup? if u try to help some information instead of just confirming what i already know would be awesome LOL

    should the code be inside of global script or where? 


    what you're trying to use is too complicated for a novice.  use my previous suggestion and add both images to the stage.  convert each to a movieclip (right click each>click convert to symbol>movieclip). assign each an instance name (in the properties panel). eg, day_mc and night_mc.

     

    you can then use:

     

    this.day_mc.visible=false;

    this.night_mc.visible=false;

    TODimage.bind(this)();

     

    function TODimage() {
    var hr = new Date().getHours(); 
    if ((hr >= 6) && (hr < 21)) {

    this.day_mc.visible=true;

    } else {

    this.night_mc.visible=true;

    }
    }

    1 reply

    kglad
    Community Expert
    Community Expert
    October 20, 2021

    are you sure you want actionscript (for an as3 project), and  not javascript for an html5/canvas project?

     

    are both backgrounds movieclips and on-stage with instance names?  (they should be to make it easier to code).

    Dave8585
    Dave8585Author
    Participating Frequently
    October 20, 2021

    it doesnt matter to me in what language 😄 i dont really know about any language...

    no the background is a normal image, the code i wanted to use is for normal images i thought... 

    var DJimages = [  // add path to directory if necessary
       "DJimage1.png",  
       "DJimage2.png"
       // NOTE: no comma after last entry
    ];
    function TODimage() {
      var now = new Date();
      hr = now.getHours();                               // alert(hr);  // for testing
      var DJ = 2;                                               // 21 bis 06
      if ((hr >= 6) && (hr < 21)) { DJ = 1; }   // 06 bis 21
      document.getElementById('DJimage').src=DJimages[DJ];
      document.getElementById('DJimage').alt = DJimages[DJ];
    }