Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 839435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:27:47+00:00 2026-05-15T05:27:47+00:00

Ok, what im trying to do is make a day to night cycle behind

  • 0

Ok, what im trying to do is make a day to night cycle behind my landscape. There is a sun and a moon, they rotate in a circle on opposite sides. (i.e. the sun is up when the moon is down and vice versa) when the sun is coming up it should fade from the night movieclip to the dawn movieclip, then when the sun is up a little bit more, fade into the day moviclip, this works quite well, but, for some reason, when it gets to the sunset, it just wont work :/ and the same goes for when it goes from the sunset to night :/ any and all healp is greatly appreciated, ive spent 5 hours trying to figure this out and cant! please help!

stage.addEventListener(Event.ENTER_FRAME, daynightcycle)

//setChildIndex(night, getChildIndex(day));

setChildIndex(sunset, 0);
setChildIndex(day, 1);
setChildIndex(dawn, 2);
setChildIndex(night, 3);

function daynightcycle(e:Event):void {

 if(sun.currentLabel == "dawn") {
  setChildIndex(sunset, 0);
  setChildIndex(day, 1);
  setChildIndex(dawn, 2);
  setChildIndex(night, 3);
  stage.addEventListener(Event.ENTER_FRAME, nightTdawn);

 }else if(sun.currentLabel == "sunset") {
  setChildIndex(dawn, 0);
  setChildIndex(night, 1);
  setChildIndex(sunset, 2);
  setChildIndex(day, 3);
  stage.addEventListener(Event.ENTER_FRAME, dayTsunset);

 }else if(sun.currentLabel == "night") {
  setChildIndex(day, 0);
  setChildIndex(dawn, 1);
  setChildIndex(night, 2);
  setChildIndex(sunset, 3);
  stage.addEventListener(Event.ENTER_FRAME, sunsetTnight);

 }else if(sun.currentLabel == "day") {
  setChildIndex(night, 0);
  setChildIndex(sunset, 1);
  setChildIndex(day, 2);
  setChildIndex(dawn, 3);
  stage.addEventListener(Event.ENTER_FRAME, dawnTday);

 }else if(sun.currentLabel == "switch") {
  stage.addEventListener(Event.ENTER_FRAME, switchLayers);
 }



}

function nightTdawn(e:Event):void {


 if(night.alpha != 0) {

  night.alpha -= 0.01;
 }else {
  stage.removeEventListener(Event.ENTER_FRAME, nightTdawn);
  night.alpha = 100;
  //setChildIndex(night, getChildIndex(sunset));
 }


}


function dayTsunset(e:Event):void {


 if(day.alpha != 0) {
  day.alpha -= 0.01;
 }else {
  stage.removeEventListener(Event.ENTER_FRAME, dayTsunset);
  day.alpha = 100;
  //setChildIndex(day, getChildIndex(dawn));
 } 

 //day.visible = false;
 //sunset.visible = true;

}
function sunsetTnight(e:Event):void {


 if(sunset.alpha != 0) {
  sunset.alpha -= 0.01;
 }else{
  stage.removeEventListener(Event.ENTER_FRAME, sunsetTnight);
  sunset.alpha = 100;
  //setChildIndex(sunset, (getChildIndex(day)));
 } 

 //sunset.visible = false;
 //night.visible = true;

}
function dawnTday(e:Event):void {

 sunset.visible = true;
 day.visible = true;

 if(dawn.alpha != 0) {
  dawn.alpha -= 0.01;
 }else{
  stage.removeEventListener(Event.ENTER_FRAME, dawnTday);
  dawn.alpha = 100;
  //setChildIndex(dawn, (getChildIndex(night)));
 } 
}

function switchLayers(e:Event):void {
 setChildIndex(dawn, 0);
 setChildIndex(night, 1);
 setChildIndex(sunset, 2);
 setChildIndex(day, 3);

 night.alpha = 100;
 sunset.alpha = 100;
 day.alpha = 100;
 dawn.alpha = 100;
 stage.removeEventListener(Event.ENTER_FRAME, switchLayers);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:27:48+00:00Added an answer on May 15, 2026 at 5:27 am

    The problem likely stems from the fact that daynightcycle() is being called on every frame, and it in turn is adding a new EnterFrame listener every time.

    In addition, it is quite expensive to be calling daynightcycle() on every single frame, when you really only need to call it when you change the state of the sun.

    I’ve rewritten it for you in a cleaner style, so it should be pretty clear what is going on.

    If you are not familiar with the Switch/Case syntax, then watch this explanation: As3 Switch Statement

    var incomingLayer:DisplayObject
    
    //initialize the order of the layers
    setChildIndex(sunset, 0);
    setChildIndex(day, 1);
    setChildIndex(dawn, 2);
    setChildIndex(night, 3);
    
    
    //call this function anytime you change the label of the sun
    function onSunLabelChanged():void
    {
        switch (sun.currentLabel)
        {
            case "dawn":
                switchToLayer(dawn);
                break;
            case "sunset":
                switchToLayer(sunset);
                break;
            case "night":
                switchToLayer(night);
                break;
            case "day":
                switchToLayer(day);
                break;
        }
    }
    
    
    function switchToLayer(targ:DisplayObject):void
    {
        incomingLayer = targ; //save a reference to the incoming layer
        incomingLayer.alpha = 0; //make it transparent
        setChildIndex(incomingLayer, 3); //put it on top
        stage.addEventListener(Event.ENTER_FRAME, onEnterFrame); //set a listener to fade it in
    }
    
    
    function onEnterFrame(e:Event):void
    {
        incomingLayer.alpha += 0.01; //increment the alpha of the incoming layer
        if (incomingLayer.alpha >= 1) //if the layer is fully opaque
        {
            stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame); //remove the listener
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent the whole day trying to make a script which on submit
Good Day All we are trying to do is inside a trigger make sure
I spent a day trying to make Ent Lib Logging work and log anything
I am trying to make an 'opening hours' table that highlights the current day
I have spent the whole day trying to make my application use threads but
I've been trying all day and just can't make the the TTPickerTextField work. It
Have been struggling all day trying to make this simple example work using socket.io.
For my day job, I program in python or c++. I'm trying to make
Good day. I'm trying to make an online quiz application using php. The idea
Good day to all. I'm trying to make a task tracking module for my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.