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);
}
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