Thursday 31 January 2019

How to create an animation

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
   put the filename of image thai into temp
   set itemdel to "/"
   put item 1 to -2 of temp into temp2
   set the cartella of image thai to temp2
   send fermo to image 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
   put the cartella of me into cart
   put the sprite of me into spr
   add 1 to spr
   if spr > 16 then
      put 1 into spr
   end if
   set the sprite of me to spr
   put the loc of me into temploc
   put cart & "/s" & spr & ".png" into temp
   set the filename of image thai to temp
   set the loc of me to temploc      
   send fermo to me in 0.2 sec   
end fermo

on cancellafermo
      repeat for each line templ in the pendingmessages
      if item 3 of templ is "fermo" then
         put item 1 of templ into temp
         cancel temp
      end if      
   end repeat
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 of image thai
   set the stato of image thai to "avanti"   
   put the cartella of image thai into cart
   repeat for 2 times
      repeat with spr = 1 to 6
         put cart & "/f" & spr & ".png" into temp
         put the item 1 of the loc of image thai into templocx      
         put the item 2 of the loc of the image thai into templocy
         add 5 to templocx
         set the loc of the image thai to templocx,templocy      
         set the filename of image thai to temp
         wait 0.2 sec
      end repeat
   end repeat   
   send fermo to image 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:

Tuesday 29 January 2019

Moving and detecting collisions

If you want to create games, or complex GUI, you can let the user move objects on your windows and detect collisions.
Just use the command "grab me" in the mouseDown object message:



on mouseDown
   grab me
end mouseDown

In order to detect collisions, just use the intersect() function, here it is a code and a video example:

on mouseDown
   grab me
end mouseDown

On Mousemove
   if intersect(image image1,image image2) then
      set the text of field field1 to "INTERSECTION"
      set the backgroundcolor of field field1 to 255,0,0
   else
      set the text of field field1 to "NOT INTERSECTION"
      set the backgroundcolor of field field1 to 0,255,0
   end if   
end Mousemove


Friday 25 January 2019

What's LIVECODE?

Livecode is a powerful open source programming language that runs on every system: Windows, Mac, Linux, iOS, Android, raspberryPI, with CGI Apache or CGI IIS servers, with databases, as an HTML5 web page and much more! It permits you to create apps and softwares 10x faster than any other tool or programming language.
(If you need to produce a commercial software, please visit http://livecode.com for commercial versions.)
The code is so simple that is used by children, but it's so complete that is used by many big companies. Read some success stories.
LiveCode is ideal for complete beginners who have never written a line of code before. Its easy English-like language is quick to learn and more memorable than any traditional programming language. That's why LiveCode is loved by content experts, artists, and educators who need to create interactive content but have no desire to become programmers.
But don't be fooled by simplicity. LiveCode's powerful, flexible feature set is used by professional developers creating mobile apps, shrink wrapped commercial software, business process automation tools, in-house utilities and working in enterprise teams on real-time and mission critical applications. LiveCode's high productivity environment is in indispensable tool for professionals under pressure to deliver more with tight budgets.
Top Benefits:
  • Up to 10x more productive than traditional development systems.
  • Flexible platform creates applications that run on iOS, Android, Mac, Windows, Linux and Server
  • From rich mobile applications to powerful enterprise software solutions, designed for any skill-set, platform, project or budget
  • Deliver dramatically more with less - less time, less effort
One of the unique benefits of LiveCode is its iterative workflow. Projects are edited dynamically with changes happening in real time. There is no compile-run-edit-debug cycle that takes so much time with traditional development tools. LiveCode is also ideal for using agile development to adapt quickly to changing requirements.
  • Drag and drop user interface builder
  • Run and edit live
  • Integrated visual debugger
  • Edit and debug on all supported platforms
  • Create single-file standalone executables with a single click
  • Access databases, powerful data presentation options
  • Create native or custom interfaces for all platforms
  • Edit server scripts directly on the server
  • Remote visual debugger for server scripting
Looks how you can do the same of 8 lines of Java with a single line of livecode: 
sort the lines of theText descending by the last item of each
 theText = theText.split("\n");
 theText = theText.sort(sort_item_3).join("\n");
 function sort_item_3(line1,line2){
  line1 = line1.split(",");
  line2 = line2.split(",");
  if(line1[2] == line2[2]) return 0;
  else if(line1[2] > line2[2]) return -1;
  else return 1;
 }
Here a simple video showing how it works: