The Easing library is very easy to use, the only thing you need to keep track in your code is the delta time, this is needed to calculate exactly the transition position and to guarantee a smooth experience. Fortunatly this is quiet easy in Hollywood, you need to setup a timer and to keep track of the time passed between each frame rendering.

The general program structure to use efficienty the Easing library is showed below:

  1. If you need to, initialize your script
  2. Setup some tweens using tween.start()
  3. Start a timer to keep track of the delta time
  4. Start an interval to update your scene
  5. Setup a never ending loop with the WaitEvent() function

Here is a very simple working example, we will move a text around the screen using a tween.

; Working with tweens
; 1. Include the Easing library
@INCLUDE "Easing.hws"
; 2. Setup a tween for a text string that will cross the screen in 10 seconds
; We need a table to hold the text position
Local textPos = { x = 0, y = 320 }
; Let's start the transition
tween.start(10000,          ; 10 seconds = 10*1000 milliseconds
            textPos,        ; The table where we have our variable we want to smoothly change
            { x = 600 },    ; One or more targets : in this case we want to reach x=640 in 10 seconds
            "inoutbounce",  ; The name of the easing function we want to use
            Function()      ; A function to call when the transition ends, here we are using an anonymous function to print a message
              NPrint("Transition Ended! Click Left Mouse Button")

; 3. We need a function we will call regurarly to update and render
;    the scene, we also need a variable to store the previous execution
;    time so we can calculate the delta time.
Local previousTime = 0
Local timer = StartTimer(Nil)

Function renderer()
  ; Calculate the delta time
  Local currentTime = GetTimer(timer)
  Local deltaTime = currentTime - previousTime
  ; Update previousTime variable
  previousTime = currentTime
  ; Clear the screen
  ; Update the tweens
  ; Render the object
  TextOut(textPos.x, textPos.y, "HELLO!")


; 4. Setup a Interval function to update & render the tweens @ 100FPS
;    meaning every 10 milliseconds.
Local rendererInterval = SetInterval(Nil, renderer, 10)

; 5. Setup an infinite loop and watch the result

The source code is available here.

hollywoodlibs/easing/howitworks.txt · Ultima modifica: 2016/10/06 09:17 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0