Is there a simple code that when an object is moves off-screen, the camera follows it? I’ve been looking on the forums but can’t find what I’m looking for [import]uid: 116264 topic_id: 20750 reply_id: 320750[/import]
You probably have to build it manually? The only shortcut I can think of is using one of the touch/scroll functions from the code exchange and re-write it so that the touch events are the object’s movement events. [import]uid: 54716 topic_id: 20750 reply_id: 81543[/import]
Well how i do it is i add everything in a group, then when, say a ball, moves offscreen, the group follows the ball.[code]
local physics = require “physics”
physics.start() – physics portion
local localGroup = display.newGroup() – remember this for farther down in the code
local ball = display.newCircle(100, 100, 20) – make ball
physics.addBody(ball, “dynamic”, {bounce=.2, friction=.2, density=.2}) --add ball to physics
ball:setLinearVelocity(200, 0) – move ball
local ground = display.newRect(10, 320, 100000, 10) – make ground
physics.addBody(ground, “static”, {bounce=.2, friction=.2, density=.2}) --add ground to physics
local obstacle = display.newRect(400, 200, 430, 10) – make obstacle to show that ball is moving
– be sure you add everything in the group you created
localGroup:insert(ball)
localGroup:insert(ground)
localGroup:insert(obstacle)
local function moveCamera() – move camera function
if ball.x > 200 then – when ball x position is greater than 200 then move the camera
localGroup.x = -ball.x + 200 – don’t forget to make the target a negative
end
end
Runtime:addEventListener(“enterFrame”, moveCamera) [import]uid: 38977 topic_id: 20750 reply_id: 81549[/import]
Well how i do it is i add everything in a group, then when, say a ball, moves offscreen, the group follows the ball.[code]
local physics = require “physics”
physics.start() – physics portion
local localGroup = display.newGroup() – remember this for farther down in the code
local ball = display.newCircle(100, 100, 20) – make ball
physics.addBody(ball, “dynamic”, {bounce=.2, friction=.2, density=.2}) --add ball to physics
ball:setLinearVelocity(200, 0) – move ball
local ground = display.newRect(10, 320, 100000, 10) – make ground
physics.addBody(ground, “static”, {bounce=.2, friction=.2, density=.2}) --add ground to physics
local obstacle = display.newRect(400, 200, 430, 10) – make obstacle to show that ball is moving
– be sure you add everything in the group you created
localGroup:insert(ball)
localGroup:insert(ground)
localGroup:insert(obstacle)
local function moveCamera() – move camera function
if ball.x > 200 then – when ball x position is greater than 200 then move the camera
localGroup.x = -ball.x + 200 – don’t forget to make the target a negative
end
end
Runtime:addEventListener(“enterFrame”, moveCamera) [import]uid: 38977 topic_id: 20750 reply_id: 81550[/import]
Thanks this works except for when I use it with the director class, as I have to put the ball into two groups. Is there any way around this? [import]uid: 116264 topic_id: 20750 reply_id: 81578[/import]
Don’t worry I’ve worked out how to get it to work with director class, thanks for your help [import]uid: 116264 topic_id: 20750 reply_id: 81687[/import]