Skip to content

Instantly share code, notes, and snippets.

@Mr-Coxall
Created April 24, 2018 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mr-Coxall/0815ddc336fc1299abf872ce69fb8b00 to your computer and use it in GitHub Desktop.
Save Mr-Coxall/0815ddc336fc1299abf872ce69fb8b00 to your computer and use it in GitHub Desktop.
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Jump
local physics = require( "physics" )
physics.start()
physics.setGravity( 0, 25 ) -- ( x, y )
physics.setDrawMode( "hybrid" ) -- Shows collision engine outlines only
local leftWall = display.newRect( 0, display.contentHeight / 2, 1, display.contentHeight )
-- myRectangle.strokeWidth = 3
-- myRectangle:setFillColor( 0.5 )
-- myRectangle:setStrokeColor( 1, 0, 0 )
leftWall.alpha = 0.0
physics.addBody( leftWall, "static", {
friction = 0.5,
bounce = 0.3
} )
local theGround1 = display.newImage( "./assets/sprites/land.png" )
theGround1.x = 520
theGround1.y = display.contentHeight
theGround1.id = "the ground 1"
physics.addBody( theGround1, "static", {
friction = 0.5,
bounce = 0.3
} )
local theCharacter = display.newImage( "./assets/sprites/Idle.png" )
theCharacter.x = display.contentCenterX - 200
theCharacter.y = display.contentCenterY
theCharacter.id = "the character"
physics.addBody( theCharacter, "dynamic", {
density = 3.0,
friction = 0.5,
bounce = 0.3
} )
theCharacter.isFixedRotation = true -- If you apply this property before the physics.addBody() command for the object, it will merely be treated as a property of the object like any other custom property and, in that case, it will not cause any physical change in terms of locking rotation.
local dPad = display.newImage( "./assets/sprites/d-pad.png" )
dPad.x = 150
dPad.y = display.contentHeight - 80
dPad.alpha = 0.50
dPad.id = "d-pad"
local upArrow = display.newImage( "./assets/sprites/upArrow.png" )
upArrow.x = 150
upArrow.y = display.contentHeight - 190
upArrow.id = "up arrow"
local downArrow = display.newImage( "./assets/sprites/downArrow.png" )
downArrow.x = 150
downArrow.y = display.contentHeight + 28
downArrow.id = "down arrow"
local leftArrow = display.newImage( "./assets/sprites/leftArrow.png" )
leftArrow.x = 40
leftArrow.y = display.contentHeight - 80
leftArrow.id = "left arrow"
local rightArrow = display.newImage( "./assets/sprites/rightArrow.png" )
rightArrow.x = 260
rightArrow.y = display.contentHeight - 80
rightArrow.id = "right arrow"
local jumpButton = display.newImage( "./assets/sprites/jumpButton.png" )
jumpButton.x = display.contentWidth - 80
jumpButton.y = display.contentHeight - 80
jumpButton.id = "jump button"
jumpButton.alpha = 0.5
function upArrow:touch( event )
if ( event.phase == "ended" ) then
-- move the character up
transition.moveBy( theCharacter, {
x = 0, -- move 0 in the x direction
y = -50, -- move up 50 pixels
time = 100 -- move in a 1/10 of a second
} )
end
return true
end
function downArrow:touch( event )
if ( event.phase == "ended" ) then
-- move the character up
transition.moveBy( theCharacter, {
x = 0, -- move 0 in the x direction
y = 50, -- move up 50 pixels
time = 100 -- move in a 1/10 of a second
} )
end
return true
end
function leftArrow:touch( event )
if ( event.phase == "ended" ) then
-- move the character up
transition.moveBy( theCharacter, {
x = -50, -- move 0 in the x direction
y = 0, -- move up 50 pixels
time = 100 -- move in a 1/10 of a second
} )
end
return true
end
function rightArrow:touch( event )
if ( event.phase == "ended" ) then
-- move the character up
transition.moveBy( theCharacter, {
x = 50, -- move 0 in the x direction
y = 0, -- move up 50 pixels
time = 100 -- move in a 1/10 of a second
} )
end
return true
end
function jumpButton:touch( event )
if ( event.phase == "ended" ) then
-- make the character jump
theCharacter:setLinearVelocity( 0, -750 )
end
return true
end
-- if character falls off the end of the world, respawn back to where it came from
function checkCharacterPosition( event )
-- check every frame to see if character has fallen
if theCharacter.y > display.contentHeight + 500 then
theCharacter.x = display.contentCenterX - 200
theCharacter.y = display.contentCenterY
end
end
upArrow:addEventListener( "touch", upArrow )
downArrow:addEventListener( "touch", downArrow )
leftArrow:addEventListener( "touch", leftArrow )
rightArrow:addEventListener( "touch", rightArrow )
jumpButton:addEventListener( "touch", jumpButton )
Runtime:addEventListener( "enterFrame", checkCharacterPosition )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment