Jesseh, here’s a quick mock up. By no means is it perfect but should get you going to the right direction. There are tons of physics options/settings I recommend reading up on them and get a basic understanding how to use them. By no means would I use my d-pad controller I would rate it as a F. Take note how I use velocity ‘setLinearVelocity vs applyForce’. Play around with all the setting to get a better understanding of the physics world. Friction,bounce,density,Force,Gravity…………. And so on.
Gl- Darkmod
— config.lua —
[lua]application = {
content = {
width = 320,
height = 480,
scale = “letterBox”,
fps = 30,
},
}[/lua]
– main.lua –
[lua]display.setStatusBar( display.HiddenStatusBar )
physics = require “physics”
physics.start(true)
–~ physics.setContinuous( false )
physics.setDrawMode (“hybrid”)
physics.setGravity( 0,15 )
physics.setVelocityIterations( 24 )
physics.setPositionIterations( 32 )
– Local VARS --------------------------------------------------
local posX, posY = display.screenOriginX, display.screenOriginY;
local SCRN_W, SCRN_H = 480 + (posX * -1) * 2, 320 + ( posY * -1)*2
– create bounds --------------------
local leftBounds = display.newRect( posX, posY,2,SCRN_H)
physics.addBody( leftBounds , “static”, { friction=0, bounce=0, density=0 , filter = { categoryBits = 1, maskBits =1 }} )
local rightBounds = display.newRect( 480 + (posX * -1) -2 , posY,2,SCRN_H)
physics.addBody( rightBounds , “static”, { friction=0, bounce=0, density=0 , filter = { categoryBits = 1, maskBits =1 }} )
local botBounds = display.newRect( posX ,318 ,SCRN_W,2)
physics.addBody( botBounds , “static”, { friction=2, bounce=0, density=0 , filter = { categoryBits = 1, maskBits =1 }} )
– create Player ---------------------
local player = display.newRect( 240, 160 ,32,64)
– Addbody
physics.addBody( player, “dynamic”, { friction=1, bounce=0, density=7, filter = { categoryBits = 1, maskBits = 1} } )
player.isSleepingAllowed = false
player.isFixedRotation = true
– boxes
local box1 = display.newRect( 300, botBounds.y - 32,32,32)
physics.addBody( box1, “static”, { friction=0.2, bounce=0, density=0, filter = { categoryBits = 1, maskBits = 1} } )
local letsMove
local function playerFrame(evt)
– Get state is on ground or floating
local vx, vy = player:getLinearVelocity()
– isGrounded
if vy == 0 then
if letsMove == “r” then
player:setLinearVelocity( 200, 0)
elseif letsMove == “l” then
player:setLinearVelocity( -200, 0)
end
– isFloating
else
if letsMove == “r” then
player:applyForce( 200, 0, player.x, player.y)
elseif letsMove == “l” then
player:applyForce( -200, 0, player.x, player.y)
end
end
end
local function buttonTouch(evt)
local phase = evt.phase
local _x, _y = evt.x, evt.y
local ID = evt.target.id
if (phase == “began”) then
if ID == “leftBut” then
letsMove = “l”
elseif ID == “rightBut” then
letsMove = “r”
elseif ID == “jumpBut” then
player:applyForce( 0, -4000 , player.x, player.y)
end
else
letsMove = nil
end
end
– create Movment Buttons ------------
local leftBut = display.newCircle( posX + 40, SCRN_H - 40, 24)
leftBut.id= “leftBut”
leftBut:addEventListener(“touch”, buttonTouch)
local rightBut = display.newCircle( posX + 95, SCRN_H - 40, 24)
rightBut.id = “rightBut”
rightBut:addEventListener(“touch”, buttonTouch)
local jumpBut = display.newCircle( posX + 67, SCRN_H - 80, 20)
jumpBut.id = “jumpBut”
jumpBut:addEventListener(“touch”, buttonTouch)
Runtime:addEventListener(“enterFrame”, playerFrame)[/lua]
[import]uid: 7177 topic_id: 30344 reply_id: 122388[/import]