Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- display.setStatusBar( display.HiddenStatusBar )
- local physics = require("physics")
- physics.start(true)
- physics.setDrawMode("hybrid")
- local w,h = display.contentWidth, display.contentHeight
- local moveGroup = display.newGroup()
- -- Ground
- local bump = display.newImage("ramp.png")
- bump:setReferencePoint(display.CenterDownReferencePoint)
- bump.x = w-240
- bump.y = h-150
- moveGroup:insert(bump)
- local ground1 = display.newRect(moveGroup, 0, h-120, w, 20)
- ground1:setFillColor(255,0,0)
- physics.addBody(ground1, "static", {friction = 0.5, bounce = 0.5, density=2.0})
- physics.addBody(bump, "dynamic", {friction = 0.5, bounce = 0.5, density=2.0, shape={-40, 20, 40, -20, 40, 20}})
- -- Car
- local frame = display.newRect(50, h-200, 60, 40)
- frame:setFillColor(0,128,0)
- local leftWheel = display.newCircle(50, h-160, 20)
- leftWheel:setFillColor(0,255,0)
- local rightWheel = display.newCircle(110, h-160, 20)
- rightWheel:setFillColor(0,255,0)
- physics.addBody(leftWheel, "dynamic", {friction = 2, bounce = 0.4, radius = 20})
- physics.addBody(rightWheel, "dynamic", {friction = 2, bounce = 0.4, radius = 20})
- physics.addBody(frame, "dynamic", {friction = 0.5, bounce = 0.1})
- local jointLeftWheelFrame = physics.newJoint("pivot", leftWheel, frame, 50, h-160)
- local jointRightWheelFrame = physics.newJoint("pivot", rightWheel, frame, 110, h-160)
- -- Controls
- --======================
- -- throttle button
- local applyingForce = 0
- local buttonTouched = function(event)
- if (event.target.name == "throttle") then
- if (event.phase == "began") then
- applyingForce = 1
- elseif (event.phase == "ended") then
- applyingForce = 0
- end
- elseif(event.target.name == "2") then
- if (event.phase == "began") then
- applyingForce = -1
- elseif (event.phase == "ended") then
- applyingForce = 0
- end
- elseif(event.target.name == "3") then
- if (event.phase == "began") then
- frame:applyForce(5,0,frame.x, frame.y)
- end
- end
- end
- local controlThrottle = display.newRoundedRect(10, 230, 30, 70, 10)
- controlThrottle.strokeWidth = 2
- controlThrottle:setFillColor(0,0,200)
- controlThrottle:setStrokeColor(0,0,150)
- controlThrottle.name = "throttle"
- controlThrottle:addEventListener("touch", buttonTouched)
- local controlThrottleLabel = display.newText("<", 20, 250, nil, 20)
- local control2 = display.newRoundedRect(40, 230, 30, 70, 10)
- control2.strokeWidth = 2
- control2:setFillColor(0,0,200)
- control2:setStrokeColor(0,0,150)
- control2.name = "2"
- control2:addEventListener("touch", buttonTouched)
- local control2Label = display.newText(">", 45, 250, nil, 20)
- local control3 = display.newRoundedRect(70, 230, 30, 70, 10)
- control3.strokeWidth = 2
- control3:setFillColor(0,0,200)
- control3:setStrokeColor(0,0,150)
- control3.name = "3"
- control3:addEventListener("touch", buttonTouched)
- local control3Label = display.newText("^", 80, 250, nil, 20)
- local gameLoop = function(event)
- moveGroup.x = moveGroup.x - 5 * applyingForce
- --ground1.x = ground1.x - 5 * applyingForce
- end
- Runtime:addEventListener("enterFrame", gameLoop)
- --physics.stop()
Add Comment
Please, Sign In to add comment