factorize the code, scroll the opacity

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

Post Reply
problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

factorize the code, scroll the opacity

Post by problème » Wed Nov 25, 2015 3:10 am

Hello,

I have a code that put a graphic effect (drop shadow) to a field and scroll the opacity (of drop shadow), to 30 at 255 and 255 at 30.
I have 2 cards, card1 and card2. So i factorize the code in my main stack

In my main stack

Code: Select all

global appField # the field i want to put the graphic effect 
global fin # When i close a card, stop the animationMenu 

Command animationMenu 
   if fin is false then
      opacitéCroissante 
   end if 
end animationMenu

command opacitéCroissante  # grow the opacity
   if fin is false then
      if (the dropShadow["opacity"] field appField < 255) then
         put the dropShadow["opacity"] field appField
         set the dropShadow["opacity"] field appField to (the dropShadow["opacity"] field appField +10)
         send "opacitéCroissante" to me in 50 milliseconds
      else
         opacitéDécroissante
      end if 
   end if 
end opacitéCroissante

command opacitéDécroissante # reduction of the opacity
   if fin is false then
      if the dropShadow["opacity"] field appField > 30 then
         put the dropShadow["opacity"] field appField
         set the dropShadow["opacity"] field appField to (the dropShadow["opacity"] field appField - 5)
         send "opacitéDécroissante" to me in 50 milliseconds 
      else 
         animationMenu
      end if 
   end if 
end opacitéDécroissante
in my card1:

Code: Select all

on preOpenCard
   put "field3" into appField
   set the dropShadow["color"] of field appField to "255,255,0"
   set the dropShadow["opacity"] field appField to 30
   put false into fin
   animationMenu
end preOpenCard

... 
go to card2
in my card2

Code: Select all

on preOpenCard
   put "field1" into appField
   set the dropShadow["color"] of field appField to "red"
   set the dropShadow["opacity"] field appField to 30
   put false into fin
   animationMenu
end preOpenCard

on CloseCard
   put true into fin
end CloseCard

... 
go to card1

When i consider one card (when i coment the code in card1 or card2), It's work correctly, the opacity scroll to 30 at 255 and 255 at 30. But when i consider the two cards the opacity block at 255.

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: factorize the code, scroll the opacity

Post by dave.kilroy » Wed Nov 25, 2015 9:48 am

Hi - are you declaring your global variables in your card scripts?
"...this is not the code you are looking for..."

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: factorize the code, scroll the opacity

Post by bn » Wed Nov 25, 2015 9:56 am

Hi problème,

I suppose from your code that you want to run the animation all the time when being on a card.

What happens in your code is that the messages are not canceled when leaving the card. Messages can pile up.
I added a handler in the stack script "cleanUpAnimation" which is called on closeCard. It removes all pending messages regarding your animation before going to the next card.

And you forgot to fully declare your global variables in the card scripts

this works for continuous animation of the dropShadow of your field. If you only want to have a one-time animation you would have to change your code.
Unfortunately you don't explain what exactly your are trying to achieve.

stack script

Code: Select all

global appField # the field i want to put the graphic effect 
global fin # When i close a card, stop the animationMenu 

Command animationMenu 
   if fin is false then
      opacitéCroissante 
   end if 
end animationMenu

command opacitéCroissante  # grow the opacity
   if fin is false then
      if the pendingMessages contains "opacitéCroissante" then exit "opacitéCroissante" -- just in case of a pileUp of messages
      if (the dropShadow["opacity"] field appField < 255) then
         put the dropShadow["opacity"] field appField
         set the dropShadow["opacity"] field appField to (the dropShadow["opacity"] field appField +10)
         send "opacitéCroissante" to me in 50 milliseconds
      else
         opacitéDécroissante
      end if 
   end if 
end opacitéCroissante

command opacitéDécroissante # reduction of the opacity
      if fin is false then
      if the pendingMessages contains "opacitéDécroissante" then exit "opacitéDécroissante" -- just in case of a pileUp of messages
      if the dropShadow["opacity"] field appField > 30 then
         put the dropShadow["opacity"] field appField
         set the dropShadow["opacity"] field appField to (the dropShadow["opacity"] field appField - 5)
         send "opacitéDécroissante" to me in 50 milliseconds 
      else 
         animationMenu
      end if 
   end if 
end opacitéDécroissante

on cleanUpAnimation
   -- cancels animation messages
   repeat for each line aLine in the pendingMessages
      if aLine contains "opacitéCroissante" or aLine contains "opacitéDécroissante" then cancel item 1 of aLine
   end repeat
end cleanUpAnimation
card 1 script

Code: Select all

global appField # the field i want to put the graphic effect 
global fin # When i close a card, stop the animationMenu 

on preOpenCard
   put "field3" into appField
   set the dropShadow["color"] of field appField to "255,255,0"
   set the dropShadow["opacity"] field appField to 30
   put false into fin
   animationMenu
end preOpenCard

on closeCard
   put true into fin
   cleanUpAnimation -- cancel all animation messages
end closeCard

... 
go to card2
card 2 script

Code: Select all

global appField # the field i want to put the graphic effect 
global fin # When i close a card, stop the animationMenu 

on preOpenCard
   put "field1" into appField
   set the dropShadow["color"] of field appField to "red"
   set the dropShadow["opacity"] field appField to 30
   put false into fin
   animationMenu
end preOpenCard

on CloseCard
   put true into fin
   cleanUpAnimation -- cancel all animation messages
end CloseCard

... 
--go to card1
Kind regards
Bernd

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: factorize the code, scroll the opacity

Post by Klaus » Wed Nov 25, 2015 1:37 pm

Don't get sloppy, mon ami! 8)
...
if the dropShadow["opacity"] OF field appField > 30 then
put the dropShadow["opacity"] OF field appField
set the dropShadow["opacity"] OF field appField to (the dropShadow["opacity"] OF field appField - 5)
etc.
...

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: factorize the code, scroll the opacity

Post by bn » Wed Nov 25, 2015 1:42 pm

Cher Klaus,
Don't get sloppy, mon ami! 8)
oops, took the code from the OP and dit not check everything. Had a hard time to understand his logic and what he wanted to do.

Livecode is so forgiving...

Kind regards
Bernd

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: factorize the code, scroll the opacity

Post by dave.kilroy » Wed Nov 25, 2015 1:47 pm

bn wrote:Livecode is so forgiving...
unlike some OF us :D

Poor Bernd - 'gets it in the neck' for missing one liddle word!
"...this is not the code you are looking for..."

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: factorize the code, scroll the opacity

Post by bn » Wed Nov 25, 2015 1:59 pm

Hi Dave,
Poor Bernd - 'gets it in the neck' for missing one liddle word!
Klaus is the Master, he did the BANANA-Slider!

Kind regards

Bernd

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: factorize the code, scroll the opacity

Post by Klaus » Wed Nov 25, 2015 2:02 pm

bn wrote:Klaus is the Master, he did the BANANA-Slider!
True, and you better never forget this! :D

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: factorize the code, scroll the opacity

Post by dave.kilroy » Wed Nov 25, 2015 2:09 pm

oh all right, I yield to the BANANA MASTER!
"...this is not the code you are looking for..."

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: factorize the code, scroll the opacity

Post by problème » Wed Nov 25, 2015 4:15 pm

Hello,
Sorry for my mistakes ^^ , the code given by "bn" do what i wanted.
Thanks for the helps

Post Reply