View Full Version : Day and Night background
ThiZz^
March 10th, 2009, 06:05 AM
I have made my website:
http://cpower.co.il (http://cpower.co.il/)
and I have to SWF's one for the day and one for the night. I used a JS script to detect the user's
time and by that to activate the currect SWF but I find it hard when I want to update the FLA of the day.FLA I have also to update the night.FLA. Therefore, is there a way I can make that detection of user's time using ActionSctipt in the FLA of so I will have one FLA and in the preloading time it will detect the time and by that will put the correct background?
Thanks.
SandeR2
March 10th, 2009, 06:45 AM
first of all good looking website! 2nd I've made something simular to your idea. Check it out here:http://blog.libertystudio.nl/files/Header.swf You can actually change your PC time and it will refresh the scene every 5 seconds.
Classes used:
Spritefader.as
package classes {
import flash.display.Sprite;
public class SpriteFader extends Sprite {
private var lastDa:Number = 0;
private var sp:Number;
public function SpriteFader(speed:Number) {
sp = speed;
addEventListener("addedToStage",onStage);
}
private function onStage(evt) {
addEventListener("added",onAdded);
}
private function onAdded(evt) {
if(evt.target.parent != this) { return; }
if(numChildren >= 2) {
var lastAlpha:Number = 0;
while(numChildren>2) {
lastAlpha = getChildAt(0).alpha;
removeChildAt(0);
}
evt.target.alpha = lastAlpha;
startFade();
}
}
private function startFade() {
addEventListener("enterFrame", onFrame);
}
private function onFrame(evt) {
var da:Number = 1 - getChildAt(1).alpha;
getChildAt(1).alpha += da/sp;
if(da == lastDa) {
removeChildAt(0);
removeEventListener("enterFrame", onFrame);
getChildAt(0).alpha = 1;
trace("Whatup?! I'm Fading");
} else {
lastDa = da;
getChildAt(0).alpha = 1 - getChildAt(1).alpha;
}
}
}
}Refresh.as
package classes{
import flash.events.*;
import flash.utils.*;
import flash.display.*;
public class Refresh extends Sprite {
public var oTime:String;
public var oSeason:String;
public var recTime:String;
public var recSeason:String;
private var fadeSky:SpriteFader;
private var fadeSeasons:SpriteFader;
private var refreshTimer:Timer;
public function Refresh() {
oTime = TimeCheck.get().getDate();
oSeason = SeasonCheck.get().getSeason();
fadeSky = new SpriteFader(20);
addChild(fadeSky);
fadeSeasons = new SpriteFader(20);
addChild(fadeSeasons);
timer();
rebuild();
}
function timer() {
refreshTimer = new Timer(5000,0);
refreshTimer.addEventListener("timer",refresher);
refreshTimer.start();
}
function refresher(event:TimerEvent):void {
recTime = TimeCheck.get().getDate();
recSeason = SeasonCheck.get().getSeason();
// TIME
if (recTime == oTime) {
} else {
//**REFRESH**
oTime = TimeCheck.get().getDate();
rebuild();
}
// SEASON
if (recSeason == oSeason) {
} else {
//**REFRESH**
oSeason = SeasonCheck.get().getSeason();
rebuild();
}
}
private function rebuild() {
fadeSky.addChild( new Sky() );
fadeSeasons.addChild( new SeasonCheck() );
}
}
}Sky.as
package classes{
import flash.display.Sprite;
import flash.display.Shape;
public class Sky extends Sprite {
private var sky:Shape;
private var color:int;
private var maan:Moon;
private var zon:Sun;
private var ster:Star;
private var skyNacht:SkyNight;
private var skyOchtend:SkyMorning;
private var skyMiddag:SkyAfternoon;
private var skyAvond:SkyEvening;
public function Sky() {
sky = new Shape();
addChild(sky);
maan = new Moon();
maan.x = 75 + 650 * Math.random();
maan.y = 75;
zon = new Sun();
zon.x = 75 + 650 * Math.random();
zon.y = 75;
ster = new Star();
var curTime:String = TimeCheck.get().getDate();
switch (curTime) {
case "nacht" :
skyNacht = new SkyNight();
skyNacht.y = 172;
addChild(skyNacht);
color = 0x252f35;
addChild(ster);
addChild(maan);
break;
case "ochtend" :
skyOchtend = new SkyMorning();
skyOchtend.y = 172;
addChild(skyOchtend);
color = 0xb5dff5;
addChild(zon);
break;
case "middag" :
skyMiddag = new SkyAfternoon();
skyMiddag.y = 172;
addChild(skyMiddag);
color = 0x82cdf4;
addChild(zon);
break;
case "avond" :
skyAvond = new SkyEvening();
skyAvond.y = 172;
addChild(skyAvond);
color = 0x00466C;
addChild(ster);
addChild(maan);
break;
}
with (sky.graphics) {
beginFill(color,1);
drawRect(0, 0, 800, 300);
endFill();
}
}
}
}I think this is all you need to pick out the source to make your day and night effect. Good luck! :proud:
ThiZz^
March 10th, 2009, 06:57 AM
Thanks but I think you misunderstood my questions. I already have my 2 background.
How can I detect using AS the user's time and by that setting the correct background?
SandeR2
March 10th, 2009, 10:10 AM
I'm sorry I was too lazy and just gave you the whole script, basically everything you need is in there but here are the snippets you need:
package classes{
public class TimeCheck {
var time:Date;
var hours:int;
var day:String;
var night:String;
private var date:String;
private var bgDay:IMG_NAME_DAY;//change this to your image name (Export for Actionscript)
private var bgNight:IMG_NAME_NIGHT;//change this to your image name (Export for Actionscript)
public function TimeCheck() {
refreshTimer=new Timer(5000,0);
refreshTimer.addEventListener("timer",getDate);
refreshTimer.start();
}
public function getDate() {
time=new Date ;
hours=time.getHours();
//Day
if (hours >= 6 || hours < 23) {
date="day";
}
//Night
if (hours >= 23 && hours < 6) {
date="night";
}
generateBG();
}
public function generateBG() {
switch (curTime) {
case "day" :
addChild(bgDay);
case "night" :
addChild(bgNight);
}
}
}
}I haven't really tested it but you should get it to work with your knowledge of flash (looking at your website).
ThiZz^
March 10th, 2009, 01:00 PM
Thanks!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.