Hi, could anyone lend a hand with a question about player position?
This is my code:
----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) local physics = require "physics" physics.start(); physics.pause() local textureFilter = "nearest" display.setDefault("minTextureFilter", textureFilter) display.setDefault("magTextureFilter", textureFilter) -------------------------------------------- -- forward declarations and other locals -------------------------------------------- local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 local crate local grass local button local map local dusk = require("Dusk.Dusk") map = dusk.buildMap("map.json") ------------------------------------------- function scene:create( event ) ------------------------------------------- local sceneGroup = self.view local crate = display.newImageRect( "crate.png", 90, 90 ) crate.x, crate.y = display.contentWidth/2, display.contentHeight/2 crate.rotation = 0 crate.speed = 250 crate.isFixedRotation=true crate.id = "crate" crate.title = "crate" crate.anchorY = 0 crate.anchorX = 0.5 map.layer[1]:insert(crate) physics.addBody( crate, "dynamic", { density=1.0, friction=0.3, bounce=0.3 } ) map.setCameraBounds({ xMin = display.contentWidth/2, yMin = display.contentCenterY, xMax = map.data.width-(display.contentWidth/2), yMax = display.contentHeight-display.contentHeight/2}) map.setCameraFocus(crate) map.setTrackingLevel(0.1) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 grass.x, grass.y = 0, display.contentHeight local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) local button = display.newRect( 0, screenH-30, screenW, 30 ) button.anchorX = 0 button.anchorY = 0 button:setFillColor( 0.2, 1, 0.2 ) button.id = "button" local function onObjectTouch( event ) if event.phase == "ended" then crate:applyLinearImpulse( 10, 0, 0.5, 0.5 ) crate.angularVelocity = 0 print("button hit") return true end end button:addEventListener( "touch", onObjectTouch ) function onEveryFrame( e ) map.updateView() end Runtime:addEventListener( "enterFrame", onEveryFrame ) sceneGroup:insert(1, map) sceneGroup:insert(2, grass) sceneGroup:insert(3, crate) sceneGroup:insert(4, button) end ------------------------------------------- function scene:show( event ) ------------------------------------------- local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then physics.start() end end ------------------------------------------- function scene:hide( event ) ------------------------------------------- local sceneGroup = self.view local phase = event.phase if event.phase == "will" then physics.stop() elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene
The tiled background moves within limits set by map.setCameraBounds but the crate moves across the screen rather than being in a fixed position with the background tiles moving. I’m using ‘map.setCameraFocus(crate)’ and I’ve tried it in a number of positions but I can’t fix the crate in the centre.
I’m using composer here too and I’m not sure if I’ve put dusk/tile-related elements in the correct places.
Thanks for any help - I’m pretty stuck.