Code help

Hi All,

Getting back into Corona again after a long break, trying out this ‘perspective’ camera – http://developer.coronalabs.com/code/perspective

Can’t get it to work, used a standard game template and added a left / right invisible button (touch left / right side of screen) which will move my ball respectively.

I am trying to move the ball as well as getting the camera to ‘track’ it but can’t work out why it isn’t working :frowning:

Can anyone help?

thanks in advance

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5 local ball local perspective=require("perspective") local camera=perspective.createView() camera:setBounds(false) local leftBtn local rightBtn ----------------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION -- -- NOTE: Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- ----------------------------------------------------------------------------------------- local function leftBtnTouched(event) transition.to(ball, {x = ball.x + 150, transition=easing.outExpo}) end local function rightBtnTouched(event) transition.to(ball, {x = ball.x - 150, transition=easing.outExpo}) end -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view -- create a grey rectangle as the backdrop local background = display.newRect( 0, 0, screenW, screenH ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( .5 ) -- make a crate (off-screen), position it, and rotate slightly ball = display.newCircle(halfW, display.contentHeight - 200, 50) physics.addBody( ball, "dynamic",{density = 10}) camera:add(ball, 1, false) camera:setFocus(ball) camera:track() leftBtn = display.newRect(screenW\*0.25, halfH, halfW, screenH) leftBtn.alpha = 0.01 --can't be 0 as not touchable leftBtn:addEventListener( "touch", leftBtnTouched ) rightBtn = display.newRect(screenW\*0.75, halfH, halfW, screenH) rightBtn.alpha = 0.01 --can't be 0 as not touchable rightBtn:addEventListener( "touch", rightBtnTouched ) -- add physics to the crate physics.addBody( ball, { density=1.0, friction=0.3, bounce=0.3 } ) -- create a grass object and add physics (with custom shape) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 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 } ) -- all display objects must be inserted into group group:insert( background ) group:insert( grass) group:insert( ball ) 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

Instead of transitions, I’d use a frame-based movement system. That’s done usually by giving the object xVel and yVel velocity values, and then each frame calling object:translate(object.xVel, object.yVel).

  • C

I’ve changed the transitions to

ball:translate(-30, 0) and ball:translate(30, 0)

The ball moves fine but the camera still isn’t tracking.

By the way, please get the most recent version. The code on that page is not maintained; it has been moved to the new code exchange:

http://code.coronalabs.com/code/perspective-virtual-camera-system

See if the issue still happens with the new version :slight_smile:

  • C

Ive updated to the latest version, now my main character (balloon) disappears.

If I uncomment line 84  ( camera:add(balloon, 1) ) 

the balloon reappears, below is my current code.

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.setGravity(0, 0.0) --physics.pause(); --physics.setDrawMode( "hybrid" ) -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5 local obstacles = display.newGroup() local balloon local leftBtn local rightBtn local score = 0 local timerSource local scoreBoard = native.newTextField( halfW, 50, 200, 50 ) scoreBoard.font = native.newFont( native.systemFontBold, 20 ) scoreBoard:setTextColor( 255,255,255 ) scoreBoard.text = "Score: 0" scoreBoard.hasBackground = false local fpsText = native.newTextField(halfW +100, 50, 200, 50 ) fpsText.text = display.fps fpsText.font = native.newFont( native.systemFontBold, 20 ) fpsText:setTextColor( 255,0,0) fpsText.hasBackground = false local perspective=require("perspective") local camera=perspective.createView() ----------------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION -- -- NOTE: Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- ----------------------------------------------------------------------------------------- local function leftBtnTouched(event) --balloon:applyLinearImpulse( 500, 0, balloon.x, balloon.y ) transition.to(balloon, {x = balloon.x + 30, transition=easing.outExpo}) --transition.to(camera, {x = camera.x + 30, transition=easing.outExpo}) --balloon:translate(30, 0) return true -- indicates successful touch end local function rightBtnTouched(event) --balloon:applyLinearImpulse( -500, 0, balloon.x, balloon.y ) transition.to(balloon, {x = balloon.x - 30, transition=easing.outExpo}) return true -- indicates successful touch end -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view -- create a grey rectangle as the backdrop --background --local background = display.newImageRect( "clouds.jpg", 2664, 1998 ) --background.anchorX = 0 --background.anchorY = 0 --background.x, background.y = 0, 0 --camera:add(background, 8, false) balloon = display.newCircle(halfW, screenH - 100, 20) balloon.myName = "Balloon" physics.addBody( balloon, "dynamic",{density = 10}) camera:add(balloon, 1) --create left / right buttons leftBtn = display.newRect(screenW\*0.25, halfH, halfW, screenH) leftBtn.alpha = 0.01 --can't be 0 as not touchable leftBtn:addEventListener( "touch", leftBtnTouched ) rightBtn = display.newRect(screenW\*0.75, halfH, halfW, screenH) rightBtn.alpha = 0.01 --can't be 0 as not touchable rightBtn:addEventListener( "touch", rightBtnTouched ) --create boundaries local baseBoundary = display.newRect(halfW, screenH, screenW, 5) --newRect( left, top, width, height ) print(screenW) baseBoundary:setFillColor( 0,255,0) baseBoundary.myName = "BaseBoundary" physics.addBody(baseBoundary, "static") -- all display objects must be inserted into group --group:insert( background ) --group:insert( balloon ) group:insert( baseBoundary ) group:insert( scoreBoard ) group:insert( leftBtn) group:insert(rightBtn) end local function addObstacles( event ) --local obstacle = display.newRect( math.random(0, screenW), -100, math.random(100,300), 10) local obstacle = display.newRect( math.random(balloon.x - screenW, balloon.x + screenW), -100, math.random(50,150), 10) --set velocities if(math.random(0,1) == 1) then obstacle.xVel = math.random(-50,50) else obstacle.xVel = 0 end obstacle.yVelMin = math.random(250, 300) obstacle.yVelMax = math.random(300, 500) obstacle:setFillColor( 255,0,0) obstacle.isBullet = true -- add physics to the crate physics.addBody( obstacle, "dynamic") --different objects obstacle:setLinearVelocity(obstacle.xVel, math.random(obstacle.yVelMin + score \* 2, obstacle.yVelMax + score \* 2)) camera:add(obstacle, 2) obstacles:insert(obstacle) obstacle.myName = "Obstacle" timerSource = event.source local function gameOverDelay(event) physics.pause() timer.cancel(timerSource) --create local options = { effect="fade", time = 200, params = { finalScore = score } } storyboard.gotoScene( "gameover", options) -- display scene overlay end local function onLocalCollision( self, event ) if(event and self) then if (self.myName == "Obstacle" and event.other.myName == "BaseBoundary") then obstacle:removeSelf() score = score + 1 scoreBoard.text = "Score: " .. score elseif (self.myName == "Obstacle" and event.other.myName == "Balloon") then camera:cancel() timer.performWithDelay( 10, gameOverDelay ) end end end obstacle.collision = onLocalCollision obstacle:addEventListener( "collision", obstacle ) 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 obstacles:removeSelf( ) 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 ----------------------------------------------------------------------------------------- --camera:setBounds(false) camera:setFocus(balloon) camera:track() timer.performWithDelay( 1000, addObstacles, -1) -- "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(). --Runtime:addEventListener( "enterFrame", onEveryFrame ) ----------------------------------------------------------------------------------------- return scene

Instead of transitions, I’d use a frame-based movement system. That’s done usually by giving the object xVel and yVel velocity values, and then each frame calling object:translate(object.xVel, object.yVel).

  • C

I’ve changed the transitions to

ball:translate(-30, 0) and ball:translate(30, 0)

The ball moves fine but the camera still isn’t tracking.

By the way, please get the most recent version. The code on that page is not maintained; it has been moved to the new code exchange:

http://code.coronalabs.com/code/perspective-virtual-camera-system

See if the issue still happens with the new version :slight_smile:

  • C

Ive updated to the latest version, now my main character (balloon) disappears.

If I uncomment line 84  ( camera:add(balloon, 1) ) 

the balloon reappears, below is my current code.

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.setGravity(0, 0.0) --physics.pause(); --physics.setDrawMode( "hybrid" ) -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5 local obstacles = display.newGroup() local balloon local leftBtn local rightBtn local score = 0 local timerSource local scoreBoard = native.newTextField( halfW, 50, 200, 50 ) scoreBoard.font = native.newFont( native.systemFontBold, 20 ) scoreBoard:setTextColor( 255,255,255 ) scoreBoard.text = "Score: 0" scoreBoard.hasBackground = false local fpsText = native.newTextField(halfW +100, 50, 200, 50 ) fpsText.text = display.fps fpsText.font = native.newFont( native.systemFontBold, 20 ) fpsText:setTextColor( 255,0,0) fpsText.hasBackground = false local perspective=require("perspective") local camera=perspective.createView() ----------------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION -- -- NOTE: Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- ----------------------------------------------------------------------------------------- local function leftBtnTouched(event) --balloon:applyLinearImpulse( 500, 0, balloon.x, balloon.y ) transition.to(balloon, {x = balloon.x + 30, transition=easing.outExpo}) --transition.to(camera, {x = camera.x + 30, transition=easing.outExpo}) --balloon:translate(30, 0) return true -- indicates successful touch end local function rightBtnTouched(event) --balloon:applyLinearImpulse( -500, 0, balloon.x, balloon.y ) transition.to(balloon, {x = balloon.x - 30, transition=easing.outExpo}) return true -- indicates successful touch end -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view -- create a grey rectangle as the backdrop --background --local background = display.newImageRect( "clouds.jpg", 2664, 1998 ) --background.anchorX = 0 --background.anchorY = 0 --background.x, background.y = 0, 0 --camera:add(background, 8, false) balloon = display.newCircle(halfW, screenH - 100, 20) balloon.myName = "Balloon" physics.addBody( balloon, "dynamic",{density = 10}) camera:add(balloon, 1) --create left / right buttons leftBtn = display.newRect(screenW\*0.25, halfH, halfW, screenH) leftBtn.alpha = 0.01 --can't be 0 as not touchable leftBtn:addEventListener( "touch", leftBtnTouched ) rightBtn = display.newRect(screenW\*0.75, halfH, halfW, screenH) rightBtn.alpha = 0.01 --can't be 0 as not touchable rightBtn:addEventListener( "touch", rightBtnTouched ) --create boundaries local baseBoundary = display.newRect(halfW, screenH, screenW, 5) --newRect( left, top, width, height ) print(screenW) baseBoundary:setFillColor( 0,255,0) baseBoundary.myName = "BaseBoundary" physics.addBody(baseBoundary, "static") -- all display objects must be inserted into group --group:insert( background ) --group:insert( balloon ) group:insert( baseBoundary ) group:insert( scoreBoard ) group:insert( leftBtn) group:insert(rightBtn) end local function addObstacles( event ) --local obstacle = display.newRect( math.random(0, screenW), -100, math.random(100,300), 10) local obstacle = display.newRect( math.random(balloon.x - screenW, balloon.x + screenW), -100, math.random(50,150), 10) --set velocities if(math.random(0,1) == 1) then obstacle.xVel = math.random(-50,50) else obstacle.xVel = 0 end obstacle.yVelMin = math.random(250, 300) obstacle.yVelMax = math.random(300, 500) obstacle:setFillColor( 255,0,0) obstacle.isBullet = true -- add physics to the crate physics.addBody( obstacle, "dynamic") --different objects obstacle:setLinearVelocity(obstacle.xVel, math.random(obstacle.yVelMin + score \* 2, obstacle.yVelMax + score \* 2)) camera:add(obstacle, 2) obstacles:insert(obstacle) obstacle.myName = "Obstacle" timerSource = event.source local function gameOverDelay(event) physics.pause() timer.cancel(timerSource) --create local options = { effect="fade", time = 200, params = { finalScore = score } } storyboard.gotoScene( "gameover", options) -- display scene overlay end local function onLocalCollision( self, event ) if(event and self) then if (self.myName == "Obstacle" and event.other.myName == "BaseBoundary") then obstacle:removeSelf() score = score + 1 scoreBoard.text = "Score: " .. score elseif (self.myName == "Obstacle" and event.other.myName == "Balloon") then camera:cancel() timer.performWithDelay( 10, gameOverDelay ) end end end obstacle.collision = onLocalCollision obstacle:addEventListener( "collision", obstacle ) 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 obstacles:removeSelf( ) 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 ----------------------------------------------------------------------------------------- --camera:setBounds(false) camera:setFocus(balloon) camera:track() timer.performWithDelay( 1000, addObstacles, -1) -- "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(). --Runtime:addEventListener( "enterFrame", onEveryFrame ) ----------------------------------------------------------------------------------------- return scene