Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Code 12c - tween Pause Change Destination
- -- Bruce Elliott
- -- April 2013
- -- Set tween to move from lower right to upper left corners
- -- Tap will pause (stop) the tween capture time and location
- -- Tap will also change destination to touch location
- -- Next tap starts tween from stop location to dest in remaining time
- -- Set display to full screen
- displayMode(FULLSCREEN)
- -- Prevent screen from rotating
- supportedOrientations(LANDSCAPE_RIGHT)
- -- Use this function to perform your initial setup
- function setup()
- print("Tap screen to pause tween")
- print("Destination will change to")
- print("tap location")
- -- Temp strt table
- tstrt = {}
- -- Temp dest table
- tdest = {}
- fillRed = 227
- fillGrey = 115
- fillColor = fillGrey
- -- Set start time
- strtTime = ElapsedTime
- -- Sync strt stop time
- stopTime = strtTime
- -- Initial time to run
- ttime = 10
- -- Starting point and size
- strt = {x = 100, y = 100, size = 50}
- -- Will capture new strt values after stopped
- stopVal = strt
- -- Destination point and size
- dest = {x = WIDTH, y = HEIGHT, size = 200}
- -- Moving/stopped flag
- stopped = false
- -- Create tween time is ttime
- createTween(strt, dest)
- end
- function createTween(tstrt, tdest)
- -- Compute time difference
- -- max of stopTime - strtTime or 0.001
- -- should start at 0.001
- diffTime = math.max(stopTime - strtTime, 0.001)
- -- Compute new tween time
- -- max of ttime - diffTime or 0.01
- ttime = math.max(ttime - diffTime, 0.01)
- if ttime < 0.1 then
- print("Done")
- fillColor = fillRed
- end
- -- Set/reset strtTime
- strtTime = ElapsedTime
- strt = tstrt
- -- Create new t1 tween
- t1 = tween(ttime, tstrt, tdest)
- end
- -- This function gets called once every frame
- function draw()
- -- This sets a dark background color
- background(40, 40, 50)
- fill(fillColor)
- -- This sets the line thickness
- strokeWidth(5)
- ellipse(strt.x, strt.y, strt.size)
- end
- function pause()
- -- When you stop a tween you can
- -- examine the current values
- tween.stop(t1)
- -- Set flag to true
- stopped = true
- -- Save current location
- stopVal.x = strt.x
- stopVal.y = strt.y
- stopVal.size = strt.size
- -- There does not seem to be a way
- -- to ask the tween for its dest
- -- values
- end
- function touched(touch)
- if touch.state == BEGAN or
- touch.state == MOVING then
- if touch.tapCount == 1 then
- -- Change destination
- if stopped == false then
- -- Capture current elapsed time
- stopTime = ElapsedTime
- -- Stop tween
- pause()
- -- Set new destination to
- -- current touch position
- dest.x = touch.x
- dest.y = touch.y
- elseif stopped == true then
- stopped = false
- -- Create tween to start at
- -- stop values and new dest values
- strt = stopVal
- createTween(strt, dest)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement