
Share Your Code
Browse Code
- All (914)
-
(95)
-
(87)
-
(45)
-
(4)
-
(42)
-
(136)
-
(22)
-
(40)
-
(13)
-
(66)
-
(153)
-
(211)
Camera
Just a simple Camera module that might be of use to some people. I wrote this pretty quickly so some more testing will still need to be done, please comment on here if you find anything.
Usage is very simple, just require the library, create a new Camera and then add some objects to it. You can then scale, rotate and move it as well as use a transitionTo function on it.
Parallax layers are also supported and are demonstrated in the main.lua file included in the repo.
Basic Usage
1 2 3 4 5 6 7 8 9 | require( "camera" ) local camera = Camera:new() camera:addObject( display.newImage( "player.png" ) ) camera:move( 50, 20 ) camera:rotate( 30 ) camera:transitionTo{ x = 100, alpha = 0.5, time = 1000 } |
Replies
Ah yea that is the issue about having physics objects in different groups. You should still be ok as long as you only add your physics objects to the same layer ( if using parallax ) or if you are not using parallax then just add them to the camera without specifying a layer.
how do I add physics object to the front layer or how I can make my character dynamic on a static platform without it falling off the platform
thank you
You will need to add ALL physics objects to the same layer.
could you give me an example please.
if you don't mind I can send the zip file of what I have
Feel free to send the zip to graham AT grahamranson DOT co DOT uk
I will look at it as soon as I can.
graham,
did you get my mail?
Thanks, Graham. Works great!
This is an awesome bit of code, thanks a lot for putting it out there for others to use. I found it at just the right time to save myself a bit of work on the prototype I'm working on.
Awesome, glad to hear it!
Bad work for negative values, eg:
setClampingBounds( -200, -200, width, height)
True, it's by no means completely perfect but it is completely free and open source so you are welcome to contribute :-)
how do you code?
Can this camera work like this:
I have a screenheight * 2. I want my camera to follow the player and center around him. But I don't want the camera to go off screen?
Joakim
Yea it can do that.
Thats great, I will try to implement it then!
Is there anything special I have to think of - or is it straight forward as in the sample code?
Joakim
In theory it should all be pretty straight forward. Famous last words.
Ok, I will give it a try :)
Joakim
Hmmm, that was harder then I could imagine ;)
Problem is that I am using storyboard and the parallax class from levelhelper.org. I had big problems getting the basic camera to work so I got some help from the dude behind levelhelper to make it work. Somehow he is assigning the parallax to a new group inherited from the group storyboard uses - and I guess that this could be a problem?
Joakim
Fixed, I tweaked my own camera function so now I am fine. Next project, I will try this from the beginning - it looks sweet.
Joakim
I'm glad you got it working. I've never used LevelHelper or Storyboard so doubt I'd have been much use :-)
I'm using this with a player that I touch to drag around the scene, but after a little bit of movement, the player graphic lags behind where the finger/mouse is in the scene.
Any suggestions for working with touch/drag?
Is the player visual added to the camera view?
Yes, the player is added to the camera. I'm using storyboard and here is the relevant add code:
1 2 3 4 | camera:addObject(player) camera:setFocus( player ) group:insert(camera:getView()) |
Then I have tried a camera:drag(event) inside my touch handler and without. I've tried the camera:drag(event) in my event handler as the only code too.
I do have a camera:update() in my enterFrame gameLoop function as well.
The camera works well except that I can't keep my player under my finger. I think I have a sample project that demonstrates it.
A sample project would be great if you have it yea, please send it over to graham AT grahamranson DOT co DOT uk
Trying to use this in a game.
Basically I have a zoom button which changes the scale, so it starts at 1 and zooms to 6, then back to 1.
Added the camera and instead of changing all objects using xScale/yScale, am just using camera:setScale.
Everything is zooming but the camera doesn't work.
EDIT: Just to explain what I would like to do -
Race car on a track, top view.
Track image is same size as iPad 1 screen.
Config.lua is width = 768, height = 1024.
So when game starts you can see all of the track. Then if you zoom I want the track to scroll following the player (car).
Dave
Are you able to make a simple demo to reproduce the issue and then send it to graham AT grahamranson DOT co DOT uk ?





Great module, but does not work properly with physics. The problem is described here:
http://developer.anscamobile.com/forum/2011/06/13/local-vs-content-coordinates