Here is my code:
-- require stuff local composer = require( "composer" ) local ids = require( "ids" ) local worldTable = require( "world" ) local perspective = require( "perspective" ) local physics = require( "physics" ) physics.start() physics.setDrawMode( "hybrid" ) local world = worldTable.world local camera = perspective.createView() -- add variables local x = display.contentCenterX local y = display.contentCenterY local w = display.actualContentWidth local h = display.contentHeight local scene = composer.newScene() -- creates the main scene function scene:create( event ) local view = self.view -- create bg local bg = display.setDefault( "background", 0, 0.5, 1 ) -- load up the world and show it local function loadWorld() -- make world size variables local worldWidth local worldHeight -- gets, sets, prints the world size local function getWorldSize() for i = 1, 1000 do if world[1][i] == nil then worldWidth = i - 1 print( "worldWith = "..worldWidth.."" ) break end end worldHeight = #world print( "worldHeight = "..worldHeight.."" ) end getWorldSize() -- this function creates the world from the table world local function showWorld() -- add variables local startBlockX = x - w / 2 local blockX = startBlockX local blockY = y - h / 2 local blockWidth = 40 local case -- make the blocks to the world for y = 1, worldHeight do for x = 1, worldWidth do case = ids.id[world[y][x]] if case.name ~= "air" then local rect = display.newImage( "assets/"..case.name..".png", blockX, blockY ) rect.width = blockWidth rect.height = blockWidth rect.id = case.id if case.solid == true then physics.addBody( rect, "static", { bounce = 0 } ) rect.name = "rect" end view:insert( rect ) camera:add( rect, 2 ) end blockX = blockX + blockWidth end blockX = startBlockX blockY = blockY + blockWidth end end showWorld() end loadWorld() local function createPlayer() local playerSpawnX = x local playerSpawnY = y local playerWidth = 40 local speed = 10 local dir = 0 local canJump = 1 local playerRect = display.newRect( playerSpawnX, playerSpawnY + 320, playerWidth, playerWidth ) physics.addBody( playerRect, { bounce = 0 } ) playerRect.name = "player" view:insert( playerRect ) camera:add( playerRect, 1 ) camera:setFocus( playerRect ) -- Set the focus to the player camera:track() -- Begin auto-tracking -- this function moves the player local function movePlayer( event ) local key = event.keyName if "down" == event.phase then if key == "a" then -- move left dir = -speed elseif key == "d" then -- move right dir = speed elseif key == "w" and canJump == 1 then playerRect:applyForce(0, -0.8) canJump = 0 end elseif event.phase == "up" then dir = 0 end end Runtime:addEventListener( "key", movePlayer ) local function movePlayer2( event ) playerRect.x = playerRect.x + dir playerRect.rotation = 0 end Runtime:addEventListener( "enterFrame", movePlayer2 ) local function collision( event ) if "began" == event.phase then canJump = 1 end end Runtime:addEventListener( "collision", collision) end createPlayer() end function scene:show( event ) end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
and here is video related to the problem:
so as you can see in the video, when I try to move right it moves little bit of the center of the screen to right and starts moving right, and when I release it stops moving and goes in the center of the screen. When I try to move to left it moves little bit left and starts moving and when I stop it goes in the center of the screen.
I want to make it so that when I press move right it goes right while staying in center of the screen and when I press move left it would stay in the middle of the screen.
Please help me if you can! 