Groups | Blog | Home
all groups > flash actionscript > april 2004 >

flash actionscript : load different jpeg depending on time of day


Bluefish21
4/17/2004 8:47:12 PM
Ok, I'm trying to load a different jpg depending upon the time of day, but
someting isn't going right. Here is what I have coded in frame 1 of my as layer:
var myDate:Date=new Date();
var currentHour:Number = myDate.getHours();

function pictureRotate(){
if ((currentHour>21)&&(currentHour<6)){
loadMovie("night.jpg", "_root.main");
}else if ((currentHour>6)&&(currentHour<9)){
loadmovie("sunrise.jpg", "_root.main");
}else if ((currentHour>9)&&(currentHour<12)){
loadmovie("morning.jpg", "_root.main");
}else if ((currentHour>12)&&(currentHour<15)){
loadmovie("midday.jpg", "_root.main");
}else if ((currentHour>15)&&(currentHour<18)){
loadmovie("afternoon.jpg", "_root.main");
}else{
loadmovie("twilight.jpg", "_root.main");
}
}
then I attached this to my mc named main and instanced main:
onClipEvent(load){
pictureRotate();
}
I've been through it several times and I'm lost. I thought maybe the time
wasn't showing up, but when I used the trace(currentHour) function it returns a
number, so I don't know what I'm doing wrong. Any help is much appreciated.
Thanks!


var myDate:Date=new Date();
var currentHour:Number = myDate.getHours();

function pictureRotate(){
if ((currentHour>21)&&(currentHour<6)){
loadMovie("night.jpg", "_root.main");
}else if ((currentHour>6)&&(currentHour<9)){
loadmovie("sunrise.jpg", "_root.main");
}else if ((currentHour>9)&&(currentHour<12)){
loadmovie("morning.jpg", "_root.main");
}else if ((currentHour>12)&&(currentHour<15)){
loadmovie("midday.jpg", "_root.main");
}else if ((currentHour>15)&&(currentHour<18)){
loadmovie("afternoon.jpg", "_root.main");
}else{
loadmovie("twilight.jpg", "_root.main");
}
}
Jack.
4/17/2004 9:05:52 PM
you need to add a path from clip to the main timeline function -

onClipEvent(load){
_parent.pictureRotate();
}
kglad
4/17/2004 9:07:01 PM
unless pictureRotate() is defined on your mc main's timeline you should use, for example:

onClipEvent(load){
_root.pictureRotate(); // if pictureRotate() is defined on the _root timelin
Bluefish21
4/17/2004 9:14:29 PM
ok so I put that in and said "Oh! so that was what wasn't right!", but then I
got this error message:
Error opening URL
"file:///D|/John%27s%20Art/04%2DNew%20work/flash/afternoon.jpg"
which doesn't make sense to me because I imported the jpg's into the flash
file. So then I said, "Just put the friggin' files where they want 'em!", So I
did and all I got was a blank screen. I am so lost. Thank you for your help
though.
Jack.
4/17/2004 9:28:55 PM
[quoted text, click to view]
then there is no need to use loadMovie,

using loadMovie, this produces an identical error -
main.loadMovie("progressive.jpg");
the error is caused because your jpeg is in progressive format,
re-save your jpegs in non-progressive format,
Bluefish21
4/17/2004 9:40:04 PM
I resaved them (although they weren't in progressive), but it still doesn't
work. I even changed it so that the path pointed to a url instead. The source
fla is
[L=here]http://www.21bluefish.com/media_in_progress/random_pictures.fla[/L].
Thanks for all your help. I just wish I knew what was going wrong.
Jack.
4/17/2004 9:54:14 PM
i have stayed with Flash6, so viewing your fla is a no-go.

as you have stated -> I imported the jpg's into the flash file,
try using the method in this file -
http://www.jackleaman.co.uk/temp/daylight.zip
kglad
4/17/2004 11:43:08 PM
the problem is your load event is attached to your target movieclip. that's
not going to work: when main first instantiates it calls pictureRotate() which
immediated begins loading a pic which initiates another load event again
calling pictureRotate() etc, ad infinitum. so, your pic will never complete
loading. it will simply start loading repeatedly.

to remedy attach that load code to another movieclip or use a different
movieclip as the target for your pic load.
Bluefish21
4/18/2004 5:12:02 AM
Thank you so much! That works great! Makes sense too. There's only one thing I
have a problem with now and that's that at 6, 9, 12, 15, 18 the picture goes to
night for that hour, but then goes back to the right one at the following hour.
I figured if I just added a minute code:
var currentMin:Number = myDate.getMinutes();
and then put this in there:
if ((currentHour>5)&&(currentHour<7)&&(currentMin<59)){
loadmovie(path+"sunrise.jpg", "_root.main");
it would work, but it doesn't. How can I get around this? I've got a thousand
thoughts on how to do this, but I can't piece one together for the life of me
right now. Thanks for the help!
kglad
4/18/2004 2:24:33 PM
it should display twilight at those times. in any case, it would be simplier
to use conditionals other than strict inequalities to accomplish your goal.
i'm not sure the following is how you want to do it, but it should give you the
an idea of how to do this:

if ((currentHour>=21)&&(currentHour<6)){
loadMovie(path+"night.jpg", "_root.main");
}else if ((currentHour>=6)&&(currentHour<9)){
loadmovie(path+"sunrise.jpg", "_root.main");
}else if ((currentHour>=9)&&(currentHour<12)){
loadmovie(path+"morning.jpg", "_root.main");
}else if ((currentHour>=12)&&(currentHour<15)){
loadmovie(path+"midday.jpg", "_root.main");
}else if ((currentHour>=15)&&(currentHour<18)){
loadmovie(path+"afternoon.jpg", "_root.main");
}else{
loadmovie(path+"twilight.jpg", "_root.main");
}
Bluefish21
4/18/2004 5:50:08 PM
That was exactly what I thought of when I woke up this morning! So now
everything works and is kosher so thank you kglad and Jack, I appreciate your
help so very much. I decided I wanted the pictures to rotate directly when the
said time hit, so I added in a minute variable. Here's my final code for
everyone to see in case they have need of some thing like this.
var myDate:Date=new Date();
var currentHour:Number = myDate.getHours();
var currentMin:Number = myDate.getMinutes();
var myTime = currentHour+ "." +currentMin;
var path = "http://www.21bluefish.com/media_in_progress/"
function pictureRotate(){
if ((myTime>=6.00)&&(myTime<=8.59)){
loadmovie(path+"sunrise.jpg", "_root.main");
}else if ((myTime>=9.00)&&(myTime<=11.59)){
loadmovie(path+"morning.jpg", "_root.main");
}else if ((myTime>=12.00)&&(myTime<=14.59)){
loadmovie(path+"midday.jpg", "_root.main");
}else if ((myTime>=15.00)&&(myTime<=17.59)){
loadmovie(path+"afternoon.jpg", "_root.main");
}else if ((myTime>=18.00)&&(myTime<=20.59)){
loadmovie(path+"twilight.jpg", "_root.main");
}else {
loadMovie(path+"night.jpg", "_root.main");
}
}
Then on your empty_mc put this code(big thanks to kglad for seeing that):
onClipEvent(load){
_parent.pictureRotate();
}
I was using the ":" sign for the time, but kept receiving an error so I just
changed it to a period, because it didn't matter how the time looked, as long
as it works, since I'm not printing the actual time. Thanks again to the two of
you! This was my first script that I've written from scratch so I'm very
excited about it. Thanks again!
kglad
4/18/2004 7:54:31 PM
AddThis Social Bookmark Button