I made a Camera class for my game since i couldn't find one i liked. I'm really pleased with how it turned out so i decided to share it here. The camera can follow an object, be dragged around with your finger and zoom with pinching.
How to use:
If you want the camera to follow something, like the player:
local world = Sprite.new()
local camera = Camera.new(world)
camera:setFollowMode()
local player = Sprite.new()
function onEnterFrame()
camera:setTarget(player:getPosition())
camera:update()
end
world:addEventListener(Event.ENTER_FRAME, onEnterFrame)
world:addChild(player)
stage:addChild(world) |
If you just want to drag around the camera with your finger:
local world = Sprite.new()
local camera = Camera.new(world)
--set the starting place of the camera if you want
camera:setTarget(100, 100)
--addsome random game object
local Ball = Bitmap.new(Texture.new("ball.png"))
function onEnterFrame()
camera:update()
end
world:addEventListener(Event.ENTER_FRAME, onEnterFrame)
world:addchild(Ball)
stage:addChild(world) |
So basicly make a sprite that is your world or your scene, create the camera with the sprite in the constructor.
Then add every object as child to that sprite.
And run Camera:update() on every frame.
Right now the camera has no limit on how far it can go in any direction, if you add that or any other improvement please post it here