When you have to move an object along a path or do an animation, many programmers lose days or weeks to calculate the formula that mathematically describes the path or animation.
Livecode allows you to avoid this and simply do it by hand!
As an example, I'll show you how to make a ball bounce in less than a minute.
First draw the ball directly in Livecode, just press the oval button, make a circle and then indicate the colors of the gradients, to have something like this:
Let's call it "ball".
Then we create a path, which we will call "mypath", by pressing the freehand button and drawing the trajectory of the ball by hand, obtaining something similar to this:
Make invisible the path and then create a Start button with this code:
on mouseUp movegraphic"ball"tothepointsofgraphic"mypath"in10sec end mouseUp
Creating an animated scrolling text can be achieved by livecode in two ways:
text manipulation
graphic manipulation
We'll see both ways.
TEXT MANIPULATION
Just create a field and put this code in it:
on movingtext mytext, myspeed setthe realtext ofmeto mytext setthe realtext2 ofmeto mytext stripspaces setthetextofmetoempty createspaces putthe realtext2 ofmeinto temp putthetextofmebefore temp #put the text of me after temp setthe realtext2 ofmeto temp setthetextofmeto temp setthe cyclet ofmetotrue cycleText myspeed end movingtext on stopcycle setthe cyclet ofmetofalse end stopcycle on stripspaces putthe realtext2 ofmeinto temp ifthelastcharof temp isspacethen deletethelastcharofme setthe realtext2 ofmeto temp stripspaces endif end stripspaces on cycletext myspeed putthe realtext2 ofmeinto temp putthenumberofcharsof temp into nt setthetextofmeto temp repeatfor nt times deletechar1ofme ifnotthe cyclet ofmethenexitrepeat wait mySpeed secwithmessages endrepeat ifthe cyclet ofmethensend"cycletext " & myspeed tomein0sec end cycletext on createspaces setthedontwrapofmetotrue putspaceafterme iftheformattedwidthofme < thewidthofmethen createspaces endif end createspaces
Now you have the following messages to use:
movingtext TEXT,SPEED: you use this message to change text, start scrolling setting the scroll speed
stopcycle to stop scrolling text
GRAPHIC MANIPULATION
Create a field and put this code inside it:
on cycleText myText, mySpeed lockscreen setthelocktextofmetotrue setthedontwrapofmetotrue setthe pcontinue ofmetotrue setthetextofmeto myText clonemeas"clonefield" puttheIDofthelastfieldinto tID setthe pcloneid ofmeto tID setthewidthoffldid tid totheformattedwidthoffldid tid setopaqueoffieldid tid tofalse setshowFocusBorderoffieldid tid tofalse setthreeDoffieldid tid tofalse setshowborderoffieldid tid tofalse setlockTextoffieldid tid totrue settheleftoffldid tid totherightofme setthetopoffldid tid tothetopofme creategraphic"cycleR" puttheidofthelastgraphicinto tID2 setthe pgr ofmeto tID2 setthestyleofgraphicid tid2 to"rectangle" settherectofgraphicid tid2 totherectoffldid tid settheopaqueofgraphicid tid2 totrue setthebackgroundcolorofgraphicid tid2 tothebackgroundcolorofthiscard setthelinesizeofgraphicid tid2 to0 clonegraphicid tid2 puttheidofthelastgraphicinto tid3 setthe pgL ofmeto tid3 settherightofgraphicid tid3 totheleftofme setthetopofthegraphicid tid3 tothetopofme setthe prealText ofmetothetextofme setthetextofmetoempty setthe pmyspeed ofmeto myspeed unlockscreen cycle2 mySpeed end cycleText on cycle2 mySpeed putthe pcloneid ofmeinto temp ifexists(fieldid temp) then settheleftoffldid temp to (theleftoffieldid temp - 1) wait myspeed sec iftherightoffldid temp < theleftofmethen settheleftoffldid temp totherightofme endif endif ifthe pcontinue ofmethensend"cycle2 "& mySpeed tomein0sec end cycle2 on StopCycle setthe pcontinue ofmetofalse setthetextofmetothe pRealText ofme putthe pcloneid ofmeinto tid putthe pgr ofmeinto tid2 putthe pGL ofmeinto tid3 deletefieldid tid deletegraphicid tid2 deletegraphicid tid3 end StopCycle
RESULTS
You can see results in the following video, the text mode is confinde in it's space but it sn't constant due the difference in size of each letter, on the other hand the gaphic mode is smother but needs on the left and on the righe no other object, because we have to cover the text:
You can create an animation with the drawing commands of livecode, for example see this script:
on mouseUp settherectofthetemplateImageto100,100,400,400 createimage"test" choosebrushtool setthebrushto8 setthebrushColorto red -- could use an RGB triplet here setthedragSpeedto20-- very slow #H drag from 120,120 to 120,300 dragfrom120,200to150,200 dragfrom150,300to150,120 #i dragfrom180,120to180,150 dragfrom180,180to180,300 end mouseUp
this is the result:
The example is simple, but you can use all geometric shapes of livecode, so you can create very complex animations.
I needed a LOT of time to write this post, this time I'll show you how to create animated menu collapsing/expanding.
You are free to change anything of the code and animations.
You can change buttons in the examples below with any combinations of items you like for graphic.
It's a little complex, so I'll write a plugin to do this automatically and better.
The final result will be like this demo:
Please note the will need to do a lot of GROUPS. Here is the hierarchy of what we'll see, keep it in mind for the rest of this post:
First of all, create a button called main, then another button called +. Resize them to get this result:
Now put this code in button + script:
on mouseUp ifthelabelofmeis"+"then showMe setthelabelofmeto"-" else hideMe setthelabelofmeto"+" endif end mouseUp
Group them together and call the group Main.
Create another button call it slider, resize it below Main like this:
Now group slider with itself (or other item you like in slider), and call the group Slider.
Now group group slider with group main, call this group element 1.
Set the lockLocation of group "element 1" to true.
Put this code in group element 1:
Clone it has many times you like, for example 5 times.
Put any clone below the preceding clone, change the numbers in names, so we have: element 1 on top, element 2 down element 1, element 3 down element 2 and so on.
Group all and call it List.
Set the lockLocation of group "list" to true.
Put this code in group list script:
on layoutChanged pGroup, pSize lockmessages local tElementCount if pGroup is"Element Template"thenexit layoutChanged putword2of pGroup into tElementCount add1to tElementCount -- look for the next one repeatwhilethereisagroup ("Element" && tElementCount) setthetopofgroup("Element" && tElementCount)tothetopofgroup ("ElemenT" && tElementCount) + pSize-- add1to tElementCount endrepeat setthevscrollofmetothevscrollofme ifthebottomofgroup pGroup ofme > thebottomofmethen setthescrollofmetothescrollofme + pSize endif setthevScrollBarofgroup"List"totheformattedHeightofgroup"List" > theheightofgroup"List" setthevScrollofgroup"List"tothevScrollofgroup"List" unlockmessages end layoutChanged
If
you want to create a video game or animation for your program, you need
first of all a series of images to be replaced in order to create the
animation. I
created three different sequences for a character: one in which is
stopped (s1-s18), one in which advances (f1-f6) and one in which recoils
(b1-b6).
We
put an image, called "Thai" as an example, in our window and create a
custom property that contains the folder with all the animation
sequences (called frames or sprites ).
Single frame
I put the image and then I added the following code:
on opencard putthefilenameofimage thai into temp setitemdelto"/" putitem1to -2of temp into temp2 setthe cartella ofimage thai to temp2 send fermo toimage thai end opencard
Then I wrote the code for the animation when it stays in place, I called the message standstill. I created a custom properties sprite that makes me as a counter to the correct order of images. If
the images are not all the same size the center of gravity shifts and
the animation may begin to move, so it is best to store the position
(property loc ) and reset it after updating the sprite. We
must be careful to create a message that calls itself, not a repeated
sequence in a message, otherwise you will block in an infinite loop the program. Here is the correct code into the image:
on fermo putthe cartella ofmeinto cart putthe sprite ofmeinto spr add1to spr if spr > 16then put1into spr endif setthe sprite ofmeto spr putthelocofmeinto temploc put cart & "/s" & spr & ".png"into temp setthefilenameofimage thai to temp setthelocofmeto temploc send fermo tomein0.2sec end fermo on cancellafermo repeatforeachline templ inthependingmessages ifitem3of templ is"fermo"then putitem1of templ into temp cancel temp endif endrepeat end cancellafermo
As
you can see the image contains both the message to be repeated to make
the whole cycle of the sequence and the message to block it. Now add the buttons to move your character, here is the code to move forward:
on mouseUp call cancellafermo ofimage thai setthe stato ofimage thai to"avanti" putthe cartella ofimage thai into cart repeatfor2 times repeatwith spr = 1to6 put cart & "/f" & spr & ".png"into temp puttheitem1ofthelocofimage thai into templocx puttheitem2ofthelocoftheimage thai into templocy add5to templocx setthelocoftheimage thai to templocx,templocy setthefilenameofimage thai to temp wait0.2sec endrepeat endrepeat send fermo toimage thai end mouseUp
Because I don't want interruptions during the sequence of movement, I used a single message with a loop repeat inside. But do it only if you want something that can never be broken. It is usually best to do as in the case of the stationary sequence. You can also use the same code for the button to go back (just replace 5 with -5) and you will done. This is the result and there isn't a line of code more: