Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("mobdebug").start()
- system.setIdleTimer(false)
- display.setStatusBar(display.HiddenStatusBar)
- display.setDrawMode("forceRender")
- system.activate("multitouch")
- local physics = require("physics")
- local widget = require("widget")
- physics.setGravity(0, 32)
- physics.start()
- local ccx = display.contentCenterX
- local ccy = display.contentCenterY
- -- Camera
- local camera = display.newGroup()
- -- Character
- local char = display.newGroup()
- char.x = ccx
- char.y = ccy
- camera:insert(char)
- local charBody = display.newRect(0, 25, 100, 200) -- Offset the head so physics dont become messed up
- char:insert(charBody)
- local charHead = display.newRect(0, -100, 50, 50)
- charHead.fill = {0.5, 0.1, 0.1}
- char:insert(charHead)
- physics.addBody(char, {density = 0.5, friction = 0.2, bounce = 0})
- char.isFixedRotation = true -- This is an internal flag so char does not rotate
- -- Level
- local levelData = {
- {x = ccx, y = ccy + 300, w = 5000, h = 100}, -- Floor
- {x = ccx + 200, y = ccy - 100, w = 200, h = 100}, -- Random block
- {x = ccx + 2500, y = ccy, w = 200, h = 5000}, -- Right wall
- {x = ccx - 2500, y = ccy, w = 200, h = 5000}, -- Left wall
- }
- local level = display.newGroup() -- All level objects will be inserted to this group
- camera:insert(level)
- for index = 1, #levelData do -- Iterate through levelData and build shapes
- local data = levelData[index]
- local shape = display.newRect(data.x, data.y, data.w, data.h)
- shape.fill = {0, 1, 0}
- physics.addBody(shape, "static", {friction = 1, bounce = 0.5})
- level:insert(shape)
- end
- -- Buttons
- local bWidth = 200
- local bHeight = 100
- local bColor = {default = {1, 0, 0}, over = {1, 0.1, 0.7}}
- local bStroke = {default = {1, 0.4, 0}, over = {0.8, 0.8, 1}}
- local function onButtonEvent(event)
- local button = event.target
- if event.phase == "began" then
- button.holding = true
- elseif event.phase == "ended" then
- button.holding = false
- end
- return true
- end
- local function createPhysicsButton(label)
- local lOptions = {
- label = label,
- shape = "rect",
- width = bWidth,
- height = bHeight,
- fillColor = bColor,
- strokeColor = bStroke,
- strokeWidth = 4,
- onEvent = onButtonEvent,
- }
- local button = widget.newButton(lOptions)
- button.holding = false
- return button
- end
- local lButton = createPhysicsButton("left")
- lButton.x = ccx - 400
- lButton.y = ccy
- local rButton = createPhysicsButton("right")
- rButton.x = ccx + 400
- rButton.y = ccy
- local jButton = createPhysicsButton("fly")
- jButton.x = ccx
- jButton.y = ccy + 350
- Runtime:addEventListener("enterFrame", function()
- if lButton.holding then
- char:applyForce(-1000, 0, char.x, char.y)
- end
- if rButton.holding then
- char:applyForce(1000, 0, char.x, char.y)
- end
- if jButton.holding then
- char:applyForce(0, -500, char.x, char.y)
- end
- -- Camera focus
- camera.x = -char.x + display.viewableContentWidth * 0.5
- camera.y = -char.y + display.viewableContentHeight * 0.5
- end)
Add Comment
Please, Sign In to add comment