Hey nml, see if this is helpful. I found some old code and just tweaked it a bit. It’s very basic but can give you a general idea of what I meant. Put these 2 files in the same folder and run main.lua. Press the white circle to jump. I temporarily hosted “guy.png” on my website but it won’t be there forever. Can substitute it with any image: http://appnews.30belowstudios.com/guy.png
--main.lua
display.setStatusBar( display.HiddenStatusBar )
require("physics")
physics.start()
-- Create a background colour just to make the map look a little nicer
local back = display.newRect(0, 0, display.contentWidth, display.contentHeight)
back:setFillColor(165, 210, 255)
local ground = display.newRect (0, display.contentHeight-20,display.contentWidth,20)
ground:setFillColor(0,50,0)
physics.addBody( ground, "static", {density=1} )
ground.type = "ground"
local player = display.newImage("guy.png")
player.x = display.contentWidth/2; player.y = display.contentHeight-30;
physics.addBody( player, {density=1, friction=0.2} )
local startJump = 0
local playerX = 0
local jumpFactor
local onTouch = function(event)
if startJump == 1 then
player.y = player.y - jumpFactor
else
if player.canJump == false and jumpFactor \> 0.1 then
jumpFactor = jumpFactor \* 0.9
player.y = player.y - jumpFactor
end
end
end
Runtime:addEventListener("enterFrame", onTouch)
local function onCollision(self, event )
if ( event.phase == "began" ) then
if event.other.type == "ground" then
player.canJump = true
end
elseif ( event.phase == "ended" ) then
player.canJump = false
end
end
local button = display.newCircle( display.contentWidth-50, display.contentHeight-50, 30)
local function makePlayerJump( event )
if event.phase == "began" then
if player.canJump then
jumpFactor = 7 --Change this to increase/decrease jump height
playerX = player.x
startJump = 1
end
elseif event.phase == "ended" or event.phase == "canceled" then
startJump = 0
end
end
button:addEventListener("touch", makePlayerJump)
player.collision = onCollision
player:addEventListener( "collision", player )
[import]uid: 31262 topic_id: 25134 reply_id: 102266[/import]