Advertisement
br1wr2el3

Code 12c - tween pause

Apr 30th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Code 12c - tween Pause Change Destination
  4.  
  5. -- Bruce Elliott
  6. -- April 2013
  7.  
  8. -- Set tween to move from lower right to upper left corners
  9. -- Tap will pause (stop) the tween capture time and location
  10.  
  11. -- Tap will also change destination to touch location
  12.  
  13. -- Next tap starts tween from stop location to dest in remaining time
  14.  
  15. -- Set display to full screen
  16. displayMode(FULLSCREEN)
  17. -- Prevent screen from rotating
  18. supportedOrientations(LANDSCAPE_RIGHT)
  19.  
  20. -- Use this function to perform your initial setup
  21. function setup()
  22. print("Tap screen to pause tween")
  23. print("Destination will change to")
  24. print("tap location")
  25.  
  26. -- Temp strt table
  27. tstrt = {}
  28. -- Temp dest table
  29. tdest = {}
  30.  
  31. fillRed = 227
  32. fillGrey = 115
  33. fillColor = fillGrey
  34. -- Set start time
  35. strtTime = ElapsedTime
  36. -- Sync strt stop time
  37. stopTime = strtTime
  38. -- Initial time to run
  39. ttime = 10
  40.  
  41. -- Starting point and size
  42. strt = {x = 100, y = 100, size = 50}
  43. -- Will capture new strt values after stopped
  44. stopVal = strt
  45.  
  46. -- Destination point and size
  47. dest = {x = WIDTH, y = HEIGHT, size = 200}
  48.  
  49. -- Moving/stopped flag
  50. stopped = false
  51.  
  52. -- Create tween time is ttime
  53. createTween(strt, dest)
  54.  
  55. end
  56.  
  57. function createTween(tstrt, tdest)
  58. -- Compute time difference
  59. -- max of stopTime - strtTime or 0.001
  60. -- should start at 0.001
  61. diffTime = math.max(stopTime - strtTime, 0.001)
  62.  
  63. -- Compute new tween time
  64. -- max of ttime - diffTime or 0.01
  65. ttime = math.max(ttime - diffTime, 0.01)
  66.  
  67. if ttime < 0.1 then
  68. print("Done")
  69. fillColor = fillRed
  70. end
  71. -- Set/reset strtTime
  72. strtTime = ElapsedTime
  73. strt = tstrt
  74.  
  75. -- Create new t1 tween
  76. t1 = tween(ttime, tstrt, tdest)
  77. end
  78.  
  79. -- This function gets called once every frame
  80. function draw()
  81. -- This sets a dark background color
  82. background(40, 40, 50)
  83. fill(fillColor)
  84. -- This sets the line thickness
  85. strokeWidth(5)
  86.  
  87. ellipse(strt.x, strt.y, strt.size)
  88. end
  89.  
  90. function pause()
  91. -- When you stop a tween you can
  92. -- examine the current values
  93. tween.stop(t1)
  94.  
  95. -- Set flag to true
  96. stopped = true
  97.  
  98. -- Save current location
  99. stopVal.x = strt.x
  100. stopVal.y = strt.y
  101. stopVal.size = strt.size
  102.  
  103. -- There does not seem to be a way
  104. -- to ask the tween for its dest
  105. -- values
  106.  
  107. end
  108.  
  109. function touched(touch)
  110.  
  111. if touch.state == BEGAN or
  112. touch.state == MOVING then
  113.  
  114. if touch.tapCount == 1 then
  115. -- Change destination
  116. if stopped == false then
  117. -- Capture current elapsed time
  118. stopTime = ElapsedTime
  119.  
  120. -- Stop tween
  121. pause()
  122.  
  123. -- Set new destination to
  124. -- current touch position
  125. dest.x = touch.x
  126. dest.y = touch.y
  127.  
  128. elseif stopped == true then
  129.  
  130. stopped = false
  131. -- Create tween to start at
  132. -- stop values and new dest values
  133. strt = stopVal
  134. createTween(strt, dest)
  135. end
  136. end
  137. end
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement