Help with the Perspective Library

Hi I’m just starting to get into using Corona/Lua, and I have been trying to get a camera set up to track a box around a map I’ve made. I used the Composer to make a basic background (sky picture with grass, grass has physics enabled and is static), and in code made a box and set it up with a touch joint. When I run it, all of this works fine.

I want to set a camera to follow the box around using the perspective library. I required the library, created a new camera, and added my box to it, then try to track it during the “did” section of scene:show but I can’t get it to follow the box.

Extra parts of code have been cut out (Touch joint function, etc.),

local perspective = require( "perspective" ) local cam = perspective.createView() function scene:create( event ) local sceneGroup = self.view local crate = display.newImageRect( "Pictures/crate.png", 90, 90 ) crate.x, crate.y = 160, -100 crate.rotation = 15 cam:add(crate, 1, true) function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then cam:setParallax( 1, .6 ) elseif phase == "did" then -- Called when the scene is now on screen cam:track() end end

If it helps, here’s the rest of the code.

------------------------------------------------------------------------------- -- -- \<scene\>.lua -- ------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) local physics = require( "physics" ) local perspective = require( "perspective" ) local cam = perspective.createView() ------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view local crate = display.newImageRect( "Pictures/crate.png", 90, 90 ) crate.x, crate.y = 160, -100 crate.rotation = 15 cam:add(crate, 1, true) -- add physics to the crate physics.addBody( crate, "dynamic", { density=3.0, friction=0.3, bounce=0.2 } ) --making it move with mouse local function touchCrate ( event ) if event.phase == "began" then crateJoint = physics.newJoint( "touch", crate, event.x, event.y ) return true elseif event.phase == "moved" then crateJoint:setTarget( event.x, event.y ) return true elseif event.phase == "ended" or event.phase == "cancelled" then crateJoint:removeSelf() crateJoint = nil return false end end Runtime:addEventListener( "touch", touchCrate ) sceneGroup:insert( crate ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is off screen and is about to move on screen cam:setParallax( 1, .6 ) elseif phase == "did" then -- Called when the scene is now on screen cam:track() 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 -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- camera:cancel() 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

If it helps, here’s the rest of the code.

------------------------------------------------------------------------------- -- -- \<scene\>.lua -- ------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) local physics = require( "physics" ) local perspective = require( "perspective" ) local cam = perspective.createView() ------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view local crate = display.newImageRect( "Pictures/crate.png", 90, 90 ) crate.x, crate.y = 160, -100 crate.rotation = 15 cam:add(crate, 1, true) -- add physics to the crate physics.addBody( crate, "dynamic", { density=3.0, friction=0.3, bounce=0.2 } ) --making it move with mouse local function touchCrate ( event ) if event.phase == "began" then crateJoint = physics.newJoint( "touch", crate, event.x, event.y ) return true elseif event.phase == "moved" then crateJoint:setTarget( event.x, event.y ) return true elseif event.phase == "ended" or event.phase == "cancelled" then crateJoint:removeSelf() crateJoint = nil return false end end Runtime:addEventListener( "touch", touchCrate ) sceneGroup:insert( crate ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is off screen and is about to move on screen cam:setParallax( 1, .6 ) elseif phase == "did" then -- Called when the scene is now on screen cam:track() 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 -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- camera:cancel() 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