Changing Your Movie Based on the Time of Day
April 26, 2002
by Will Turnage
Dear Multimedia Handyman,
I'm creating a CD that will, if the end user is viewing the project before 6PM, display a day scene for the main interface, and a night scene if not. Any ideas as to how to go about doing this?
Thanks,
Scott Sidman
Scott-
The only way Director allows you to check the time is by using one of the four Lingo properties for time: the time, the long time, the short time, or the abbrev time. In most instances, you will find that these values are nearly identical, with the long time actually displaying the seconds.
put the time
-- "4:35 PM"
put the long time
-- "4:35:14 PM"
put the short time
-- "4:35 PM"
put the abbrev time
-- "4:35 PM"
The biggest problem with this information is that the results are based purely on the user's system settings, so the results will vary based on time zones and different languages or operating systems. For instance, if your users were working on a German machine, this might be the results that your Director movie would return.
put the time
-- "16.35 Uhr"
put the long time
-- "16.35.14 Uhr"
put the short time
-- "16.35 Uhr"
put the abbrev time
-- "16.35 Uhr"
Keeping all of this in mind, your first step in writing code should be to create a handler that will convert the time from a string into a number. Once you have the time as a number, then you'll easily be able to compare it to other times using equal, greater than or less than (you could also combine this data with date information to create a UNIX-like time stamp).
In this handler, you will take the time, and convert it to a number representing the number of minutes that have elapsed sinced midnight of that day.
on convertTheTime me, whichTime
if offset (":", whichTime) then the itemDelimiter = ":"
else the itemDelimiter = "."
hours = value (whichTime.item[1])
minutes = value (whichTime.item[2])
if whichTime contains "PM" then
hours = hours + 12
end if
return (60 * hours) + minutes
end
This handler starts by determining a value for the itemDelimiter, which will be used to differentiate the different parts of the time. It tests to see if the colon is used to separate hours and minutes, then assumes that a period is used if the colon isn't found (you can use a more elaborate methodology to determine if a user has customized their system to use some other separator) . Next you get the value of the number of hours in the time. Then, you get the value of the number of minutes in the time. After that, you check to see if the time contains the string "PM". If it does contain "PM" then you add 12 hours to the current number of hours. If it doesn't contain "PM", then you don't do anything because that means the time is currently in the AM or the user has a 24 hour clock. Finally, you multiply the number of hours by 60 and add the minutes to it.
In the Message window, you could check these handlers like this:
put convertTheTime ("4:35:14 PM")
-- 995
put convertTheTime (the long time)
-- 1006
The next step is to create a behavior that will be attached to the sprite that contains your daytime or nighttime image. That behavior will look like this:
property pSprRef
on beginSprite me
pSprRef = sprite (me.spriteNum)
me.displayDayOrNightImage()
timeout ("displayDayOrNightImage" & me.spriteNum).new (60000, #displayDayOrNightImage, me)
end
on endSprite me
timeout ("displayDayOrNightImage" & me.spriteNum).forget ()
end
This behavior starts by storing a reference to its own sprite in a variable. Next, you call the displayDayOrNightImage handler which resets the sprite's member based on what time of day it is. Next, you create a timeout object that will execute once a minute. This means that your program will periodically check the time to see if it's updated. If so, it will change the graphics accordingly. Note that you need to include the spriteNum at the end of the name of the timeout object. This is done so that if you have multiple sprites on stage with this behavior, then their timeout objects will all have different names. Finally, when the sprite ends, you forget the timeout object.
The last step is to write the handler that will actually change your member on screen based on the time of day.
on displayDayOrNightImage me
theCurrentTime = me.convertTheTime (the long time)
if theCurrentTime <= me.convertTheTime ("6:00:00 PM") and theCurrentTime >= me.convertTheTime ("6:00:00 AM") then
pSprRef.member = member ("daytime background")
else
pSprRef.member = member ("nighttime background")
end if
end
This handler starts by taking the current time and converting it to a number using our original formula. Next you check to see if this time is between 6:00AM and 6:00PM. If it is, then you set your member to be the daytime member. Otherwise, you set it to be the nighttime member.
All colorized Lingo code samples have been processed by Dave Mennenoh's brilliant HTMLingo Xtra, available from his site at http://www.crackconspiracy.com/~davem/
Copyright 1997-2024, Director Online. Article content copyright by respective authors.