Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | None | 0 0
  1. local physics = require('physics')
  2. physics.start()
  3. physics.setGravity(0, 0)
  4. local image = display.newImageRect( "testimage.png" ,40,40)
  5. _H = display.contentHeight
  6. _W = display.contentWidth
  7. image.x = 40
  8. image.y = 40
  9. local isheld = false
  10. local isheld2 = false
  11. physics.addBody(image)
  12. local widget = require( "widget" )
  13.  
  14. -- Function to handle button events
  15. local function goRight(event)
  16.  
  17.     if ( "began" == event.phase ) then
  18.         isheld = true
  19.     end
  20.     if ( "ended" == event.phase  or "cancelled" == event.phase) then
  21.         isheld = false
  22.     end
  23. end
  24.  
  25. local function goLeft(event)
  26.  
  27.     if ( "began" == event.phase or "moved" == event.phase ) then
  28.         isheld2 = true
  29.     end
  30.     if ( "ended" == event.phase  or "cancelled" == event.phase) then
  31.         isheld2 = false
  32.     end
  33. end
  34. -- loop to handle moving
  35. local function move( event )
  36.     if (isheld) then
  37.         image.x = image.x + 3
  38.     end
  39.    
  40.     if (isheld2) then
  41.         image.x = image.x - 3
  42.     end
  43. end
  44.  
  45. -- button widgets
  46. local button1 = widget.newButton
  47. {
  48.     left = 100,
  49.     top = 400,
  50.     id = "button1",
  51.     onEvent = goRight,
  52.     defaultFile = "testimage.png"
  53. }
  54.  
  55. local button2 = widget.newButton
  56. {
  57.     left = 50,
  58.     top = 400,
  59.     id = "button1",
  60.     onEvent = goLeft,
  61.     defaultFile = "testimage.png"
  62. }
  63.  
  64. Runtime:addEventListener( "enterFrame", move)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement