Discuss Scratch

shrubmaster
Scratcher
5 posts

How to Make a Day/Night Cycle

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
axisjack
Scratcher
100+ posts

How to Make a Day/Night Cycle

Id need to check tbe project
ResExsention
New Scratcher
1000+ posts

How to Make a Day/Night Cycle

shrubmaster wrote:

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.

Again, you could use a timer, and go like:

. . .foreveriftimer=900thenbroadcastnight or dayswitchbackdroptonight or day or however timers measureiftimer=1800thenbroadcastday or nightswitchbackdroptoday or nightresettimer or however timers measure

Last edited by ResExsention (May 22, 2019 04:27:29)

deck26
Scratcher
1000+ posts

How to Make a Day/Night Cycle

ResExsention wrote:

shrubmaster wrote:

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.

Again, you could use a timer, and go like:

. . .foreveriftimer=900thenbroadcastnight or dayswitchbackdroptonight or day or however timers measureiftimer=1800thenbroadcastday or nightswitchbackdroptoday or nightresettimer or however timers measure
NEVER use the equals wirth the built-in timer as you're very unlikely to hit the exact value - use inequality like greater than or less than. For example, the timer may go from 6.99 seconds to 7.02 seconds between one check and the next so (apparently) never be equal to 7.

Last edited by deck26 (May 22, 2019 08:28:34)

ResExsention
New Scratcher
1000+ posts

How to Make a Day/Night Cycle

deck26 wrote:

ResExsention wrote:

shrubmaster wrote:

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.

Again, you could use a timer, and go like:

. . .foreveriftimer=900thenbroadcastnight or dayswitchbackdroptonight or day or however timers measureiftimer=1800thenbroadcastday or nightswitchbackdroptoday or nightresettimer or however timers measure
NEVER use the equals wirth the built-in timer as you're very unlikely to hit the exact value - use inequality like greater than or less than. For example, the timer may go from 6.99 seconds to 7.02 seconds between one check and the next so (apparently) never be equal to 7.

Lol. My logic when I wrote that code was that a timer would always hit every value. Didn't know it was that inaccurate.
deck26
Scratcher
1000+ posts

How to Make a Day/Night Cycle

ResExsention wrote:

deck26 wrote:

ResExsention wrote:

shrubmaster wrote:

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.

Again, you could use a timer, and go like:

. . .foreveriftimer=900thenbroadcastnight or dayswitchbackdroptonight or day or however timers measureiftimer=1800thenbroadcastday or nightswitchbackdroptoday or nightresettimer or however timers measure
NEVER use the equals wirth the built-in timer as you're very unlikely to hit the exact value - use inequality like greater than or less than. For example, the timer may go from 6.99 seconds to 7.02 seconds between one check and the next so (apparently) never be equal to 7.

Lol. My logic when I wrote that code was that a timer would always hit every value. Didn't know it was that inaccurate.
It's accurate, not inaccurate.

Scratch will generally loop approx 30 times a second so roughly 0.033 seconds pass between one check of your if statement and the next. So the chances of you checking the time exactly on a whole second number are very slim.
shrubmaster
Scratcher
5 posts

How to Make a Day/Night Cycle

I think I figured it out.

whenclickedsetTime Dilationto1ifcurrenthour<7orcurrenthour>18thenswitchcostumetoMoonifcurrenthour>12thensetxto-920+currenthour*4+currentminute/15*10elsesetxto-240+currenthour*4+currentminute/15*10elseswitchcostumetoSunsetxto-240+currenthour*4+currentminute/15*10foreverglide900*TimeDilationsecstox:xposition+10y:yposition+pickrandom-0.99999to0.99999ifxposition>240thensetxto-240nextcostume

Last edited by shrubmaster (May 24, 2019 01:26:42)

legendofv
Scratcher
1 post

How to Make a Day/Night Cycle

ResExsention wrote:

shrubmaster wrote:

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.

Again, you could use a timer, and go like:

. . .foreveriftimer=900thenbroadcastnight or dayswitchbackdroptonight or day or however timers measureiftimer=1800thenbroadcastday or nightswitchbackdroptoday or nightresettimer or however timers measure
it didnt work for me
Feigh10
Scratcher
19 posts

How to Make a Day/Night Cycle

Eisthebestletter wrote:

legendofv wrote:

ResExsention wrote:

shrubmaster wrote:

I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.

Again, you could use a timer, and go like:

. . .foreveriftimer=900thenbroadcastnight or dayswitchbackdroptonight or day or however timers measureiftimer=1800thenbroadcastday or nightswitchbackdroptoday or nightresettimer or however timers measure
it didnt work for me
Don't Necropost.
It's not necroposting.
Itchy-Scratchy-show
Scratcher
1 post

How to Make a Day/Night Cycle

I actually have a Day/Night Cycle project to look at or use for reference or just download and re-time in your projects. https://scratch.mit.edu/projects/1118817116/

Last edited by Itchy-Scratchy-show (Today 03:47:59)

Powered by DjangoBB