Guest User

Untitled

a guest
Dec 10th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. require("mobdebug").start()
  2.  
  3. system.setIdleTimer(false)
  4. display.setStatusBar(display.HiddenStatusBar)
  5. display.setDrawMode("forceRender")
  6. system.activate("multitouch")
  7.  
  8. local physics = require("physics")
  9. local widget = require("widget")
  10.  
  11. physics.setGravity(0, 32)
  12. physics.start()
  13.  
  14. local ccx = display.contentCenterX
  15. local ccy = display.contentCenterY
  16.  
  17. -- Camera
  18. local camera = display.newGroup()
  19.  
  20. -- Character
  21. local char = display.newGroup()
  22. char.x = ccx
  23. char.y = ccy
  24. camera:insert(char)
  25.  
  26. local charBody = display.newRect(0, 25, 100, 200) -- Offset the head so physics dont become messed up
  27. char:insert(charBody)
  28.  
  29. local charHead = display.newRect(0, -100, 50, 50)
  30. charHead.fill = {0.5, 0.1, 0.1}
  31. char:insert(charHead)
  32.  
  33. physics.addBody(char, {density = 0.5, friction = 0.2, bounce = 0})
  34. char.isFixedRotation = true -- This is an internal flag so char does not rotate
  35.  
  36. -- Level
  37. local levelData = {
  38. {x = ccx, y = ccy + 300, w = 5000, h = 100}, -- Floor
  39. {x = ccx + 200, y = ccy - 100, w = 200, h = 100}, -- Random block
  40.  
  41. {x = ccx + 2500, y = ccy, w = 200, h = 5000}, -- Right wall
  42. {x = ccx - 2500, y = ccy, w = 200, h = 5000}, -- Left wall
  43. }
  44.  
  45. local level = display.newGroup() -- All level objects will be inserted to this group
  46. camera:insert(level)
  47.  
  48. for index = 1, #levelData do -- Iterate through levelData and build shapes
  49. local data = levelData[index]
  50.  
  51. local shape = display.newRect(data.x, data.y, data.w, data.h)
  52. shape.fill = {0, 1, 0}
  53. physics.addBody(shape, "static", {friction = 1, bounce = 0.5})
  54. level:insert(shape)
  55. end
  56.  
  57. -- Buttons
  58. local bWidth = 200
  59. local bHeight = 100
  60.  
  61. local bColor = {default = {1, 0, 0}, over = {1, 0.1, 0.7}}
  62. local bStroke = {default = {1, 0.4, 0}, over = {0.8, 0.8, 1}}
  63.  
  64. local function onButtonEvent(event)
  65. local button = event.target
  66.  
  67. if event.phase == "began" then
  68. button.holding = true
  69. elseif event.phase == "ended" then
  70. button.holding = false
  71. end
  72.  
  73. return true
  74. end
  75.  
  76. local function createPhysicsButton(label)
  77. local lOptions = {
  78. label = label,
  79. shape = "rect",
  80. width = bWidth,
  81. height = bHeight,
  82. fillColor = bColor,
  83. strokeColor = bStroke,
  84. strokeWidth = 4,
  85. onEvent = onButtonEvent,
  86. }
  87. local button = widget.newButton(lOptions)
  88. button.holding = false
  89.  
  90. return button
  91. end
  92.  
  93. local lButton = createPhysicsButton("left")
  94. lButton.x = ccx - 400
  95. lButton.y = ccy
  96.  
  97. local rButton = createPhysicsButton("right")
  98. rButton.x = ccx + 400
  99. rButton.y = ccy
  100.  
  101. local jButton = createPhysicsButton("fly")
  102. jButton.x = ccx
  103. jButton.y = ccy + 350
  104.  
  105. Runtime:addEventListener("enterFrame", function()
  106. if lButton.holding then
  107. char:applyForce(-1000, 0, char.x, char.y)
  108. end
  109.  
  110. if rButton.holding then
  111. char:applyForce(1000, 0, char.x, char.y)
  112. end
  113.  
  114. if jButton.holding then
  115. char:applyForce(0, -500, char.x, char.y)
  116. end
  117.  
  118. -- Camera focus
  119. camera.x = -char.x + display.viewableContentWidth * 0.5
  120. camera.y = -char.y + display.viewableContentHeight * 0.5
  121. end)
Add Comment
Please, Sign In to add comment