Incremental Color Change Over Long Periods

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 6:03 am

Yeah, I already got the fullscreen working well. I'm exploring the rest now. Thanks a whole lot.

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 6:07 am

See that tKey in there?
Put a breakpoint in and see what tKey holds. You can build an if/then from there.
That's the only bit I'm foggy about. I don't know what you mean by seeing what it holds or how that translates into an ability to move implement an if/then. Thanks!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 6:20 am

Put
put numToChar(charToNum(tkey))
into optionKeyDown
You see the key you are pressing in the Message Box

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Fri Mar 07, 2014 6:39 am

Have you considered reading the background color directly? Then you don't need to keep track of the color variable.

Code: Select all

command do_nexsetting
   if tContinue = true then
      put the backgroundcolor of this stack into tValue
      if tValue < 255 then
         set the backgroundcolor of this stack to tValue+1,tValue+1,tValue+1
         send "do_nexsetting tValue" to me in 1 second -- adjust timing here
      end if
   end if
end do_nexsetting
Then when you pause, you don't need to store any color variables, because the backgroundcolor is your storage.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 8:58 am

Jacque, that is a great angle of approach. I can't seem to figure out exactly how to integrate it into what I have... or how to initiate the "pause." Maybe you can help me place the code?

Here's my stack script:

Code: Select all

global tContinue
on mouseup
   set the fullscreen of this stack to true
   If tContinue <> true then
      put true into tContinue
      send "do_nexsetting 1" to me in .1 secs
   end if
end mouseup

command do_nexsetting tValue
   if tContinue = true then
      set the backgroundcolor of this stack to tValue,tValue,tValue
      ## We did not reach the last possible value yet, so:
      if tValue < 255 then
         if tValue > 254 then breakpoint
         add 1 to tValue
         put .1 into tNextDelivery
         send "do_nexsetting tValue" to me in tNextDelivery secs
      end if
   end if
end do_nexsetting

on functionKey F2
   if tValue < 255 then
         if tValue > 254 then breakpoint
         add 1 to tValue
         put .1 into tNextDelivery
         send "do_nexsetting tValue" to me in tNextDelivery secs
      end if
end functionKey
And here's my card script:

Code: Select all

on optionKeyDown tkey
   global tContinue
   put false into tContinue
end optionKeyDown

on escapeKey
      set the fullscreen of this stack to false
end escapeKey
And, Simon, I'm still not clear as to the tkey thing... all I want to do is make hotkeys for different functions. Like alt+p for one thing and alt+k for another. Sorry about the ignorance level here--I looked for a while to no avail.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 9:24 am

Code: Select all

on optionKeyDown tkey
global tContinue
put numToChar(charToNum(tkey)) into tKey
if tKey = "p" then
   put false into tContinue
end if
if tKey = "k" then
  --up to you :)
end if
end optionKeyDown
How is that?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 9:39 am

At some point I think my gratitude loses its value--but, seriously, thanks a lot, Simon. Is there a smooth way of integrating a "resume" that does the same function (adds incremental value to the colors) but starts at the current backgroundcolor? I can't figure out if there is a way to reintroduce the tContinue true value and simply keep everything going without restarting the whole process. Or maybe a better way?...I think Jacque may be onto something with the direct reference to the backroundcolor.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 10:04 am

HI,
OK sorry I keep doing this but, I really want you to figure this out yourself, it will make you very happy.
You are asking about "k" right?

When you hit " option + p" what does tValue hold?
What does the 1 do in "do_nexsetting 1"?

Can you figure it out from there?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 10:34 am

Well, I just installed Livecode 24 hours ago... so I don't really have the repertoire for this. Believe me though, I really want me to figure it out myself too :wink:

But if I had to take a stab at it, I'd say that perhaps the "1" in "do_nexsetting 1" sets the initial parameter when the command runs. That was actually going to be my next question though. When "option + p" is pressed, tValue holds whatever the R,G,B values for the backgroundcolor are... aka 1 + the number of repetitions the command has been looped. I suppose one method of deducing the tValue would be some assessment of the time since the initial mouse click, though that hardly seems a useful endeavor. I'm thinking maybe somehow calling a "do_nexsetting [tValue]" is the way to go, but I admit I'm lost. I thought I tried that. Maybe I'm missing the point here, but I do appreciate the prompting.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Fri Mar 07, 2014 10:46 am

Well, I just installed Livecode 24 hours ago
Heck, not going to help you walk in tomorrow and say "Solved it!"

Just, you are on the right track.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Fri Mar 07, 2014 11:40 am

Alright, well let me be more specific then. I can't figure out how to get the loop to continue running after a change to false for tContinue. I was messing around with "put the value of tvalue" but I don't know where to put it. It doesn't seem to make a difference when I call the command do_nexsetting tValue from the card using that method. I also couldn't make the "wait" function pause the script and then resume it. Anything in particular I should be reading up on? I don't mind doing a little work... but I'm not sure what to reference.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Incremental Color Change Over Long Periods

Post by Klaus » Fri Mar 07, 2014 3:55 pm

Hi guys,

sorry for the confusion I may have created by leaving out the most important line in my first example script! :oops:

Code: Select all

command do_nexsetting tValue
  ## We did not reach the last possible value yet, so:
  if tValue < 255 then
    add 1 to tValue

   ### !!!!
   set the backgroundcolor of this stack to tValue,tValue,tValue
   ### !!!!

    put 15*60 into tNextDelivery
    send "do_nexsetting tValue" to me in tNextDelivery secs
  end if
end do_nexsetting
Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Fri Mar 07, 2014 9:49 pm

I'd say if you've only been at this for a day, you've made remarkable progress. It's been my experience teaching new users that they need a certain amount of basic info before any hints or suggestions will work. At your stage in the game, I think it would help to give more specific guidance. A good first step is to write out the goal in short steps called pseudocode. When you've been doing this for a while, the pseudocode will be in your head but for now it's good to see it clearly.

1. To initiate the sequence you need to:

Set the stack to full screen
Set the background to black
Start a timer running that updates the color every so often
On every update, change the color slightly
When the color is white, stop

2. To implement a pause/restart function:

Set a variable that stores the current state (on or off)
If "on" then keep the timer going
If "off" then stop the timer, which will stop the updates

3. To resume from a stopped state:

Restart the timer by setting the flag to "on" again
Send the "update" command to kick off the sequence again. The update will read the current color and continue from there. This is almost identical to what happens when the sequence is first initiated. The only difference is that at the beginning we set the background to black, and after a pause, we don't.

4. To use a key press to toggle the state: (since there are only two states, you don't really need two different keys. Just use a toggle.)

Check for the correct keypress (I'm using function key 6 because it's one of the few that Apple doesn't control)
Toggle the flag variable; if "on" then turn it off, if "off" then turn it on

5. To abort everything:

Catch the "escape" keypress
Set the state flag to "off", which stops the timer
Restore the stack size

Once this is working and you understand it, I don't think you will find it too difficult to revise it to assign different timings and color variations for other tests.

The next step is to translate the pseudocode into real LiveCode syntax. Here's a start, put this all in the card script.

Code: Select all

local sOnState -- our on/off flag; this variable is available to all handlers in this script

on startSequence
  set the fullscreen of this stack to true
  set the backgroundcolor of this stack to "0,0,0" -- black
  put true into sOnState -- set flag to "on"
  send "do_nextsetting" to me in 1 second -- start the timer
end startSequence

-- this handler does the work. If the state flag is "on" it updates the color.
-- if the flag is "off" it does nothing. Since the next timer message
-- is only sent when "on" is true, the timer automatically stops if the on-state is false
command do_nextsetting -- we'll read the color directly here
  if sOnState = true then -- state is "on"
    -- since all 3 items are the same, we only need to look at one of them:
    put item 1 of the backgroundcolor of this stack into tValue
    if tValue < 255 then  -- it isn't white yet
      add 1 to tValue -- increment
      set the backgroundcolor of this stack to tValue,tValue,tValue
      send "do_nextsetting" to me in .1 second -- keep the timer going
    end if
  end if
end do_nextsetting

on functionKey pKey -- "pKey" will be the number of the function key that was pressed, this is the state toggle
  if pKey = 6 then -- check for the correct key
    put not sOnState into sOnState -- toggles the state flag true/false
    if sOnState = true then -- state is "on", resume:
      send "do_nextsetting" to me in 1 second -- restart the timer
    end if
    -- we don't need to do anything if it is off; 
    -- the do_nextsetting handler won't run because the flag is false
  end if
end functionKey

on escapeKey -- abort everything
  put false into sOnState
  set the fullscreen of this stack to false
  -- optional:
  set the backgroundcolor of this stack to "255,255,255" -- white
end escapeKey

Now you just need a button with a mouseUp handler to start things going. Call "startSequence" from the button and you should be off and running.

You'll be cheating if you don't go through each line and understand what it does. ;) Normally we don't give away all the answers like this, but you've been making an honest attempt for long enough that I think you've earned a leg up.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Sat Mar 08, 2014 8:34 am

Jacque, thanks a million. I was giving it my best go. Now my best go can be something even better.

Pseudocode is a great organizational problem solving strategy. I'm throwing in the code and checking it all out now. Thanks for appreciating that not all of life can be accomplished through only direct contact with successes & failures sans valuable verbal guidance. :D

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Sat Mar 08, 2014 10:46 am

Seems to do exactly what I need it to do. I created a card with menu options that each open a separate card which launches in fullscreen--all the cards have (or will have) basically the same code but different change rates and maximums for the tValue.

Now to incorporate sound files that play along with each card event... I'm assuming that will be more straightforward. The idea is to fade in volume of white noise the same way I fade in light--but I think I'll just make mp3s that already contain the fade. Or did I read somewhere that livecode only handles WAVs?

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”