thank you for the link provided Tech,
I’ve implemented this concept in a side project to fully grasp the understanding here and came across in the comments someone has devised up a pointToZoom function.
I have the camera following crate.x and y in the physics based game sample and the camera follows fine.
I’ve used the pointToZoom function straight up in runtime and it zooms to the crate just fine then the crate drops as there is a delay.
I’ve now since then created 2 buttons, one for zooming in and one for zooming out. however when I zoom in it zooms in to to the top left corner no longer following the crates position anymore. continuing to zoom in it now zooms to bottom right (still not following the crate)
here’s what I’ve devised:
[lua]
–
– level1.lua
–
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local perspective = require(“perspective”)
– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
– BEGINNING OF YOUR IMPLEMENTATION
–
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
–
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
– creating a camera
local camera = perspective.createView()
camera.x, camera.y = 0,0
– create a grey rectangle as the backdrop
local background = display.newRect( 0, 0, screenW, screenH )
background:setFillColor( 128 )
camera:add(background, 8, false)
– make a crate (off-screen), position it, and rotate slightly
local crate = display.newImageRect( “crate.png”, 90, 90 )
crate.x, crate.y = 160, -100
crate.rotation = 15
– add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )
camera:add(crate, 1, true)
– create a grass object and add physics (with custom shape)
local grass = display.newImageRect( “grass.png”, screenW, 82 )
grass:setReferencePoint( display.BottomLeftReferencePoint )
grass.x, grass.y = 0, display.contentHeight
– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)
local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )
camera:add(grass, 2, false)
– all display objects must be inserted into group
camera.damping = 0
– 1 is default zoom
zAmount = 1
local zoomOut = display.newRect(15, 25, 25, 25)
function zMinus()
zAmount = zAmount / 1.1
camera:pointAndZoom(camera.x, camera.y, zAmount, 500, function() print(zAmount) end, nil)
end
zoomOut:addEventListener(“tap”, zMinus)
local zoomIn = display.newRect(40, 25, 25, 25)
function zPlus()
zAmount = zAmount * 1.1
camera:pointAndZoom(camera.x, camera.y, zAmount, 500, function() print(zAmount) end, nil)
end
zoomIn:addEventListener(“tap”, zPlus)
camera:track()
--Params:
--x - number, the x of the point
--y - number, the y of the point
--xScale and yScale, assumes aspect ratio is locked
--time - number, how long it takes to get to the point
--onComplete - function, what happens when it’s at the point
--ease - function, the easing effect for the transition
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
physics.start()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene
[/lua]
and this is the pointToZoom function provided by comments that is required to go in the perspective.lua file (I assume you are familiar with)
[lua]
–point and zoom
function view:pointAndZoom(x, y, zoom, time, onComplete, t)
local x=x or display.contentWidth/2
local y=y or display.contentHeight/2
local zoom=zoom or 1
local time=time or 1000
local onComplete=onComplete or nil
local t=t or nil
[/lua]