Move Camera

Posted by Gigabook, Posted on November 10, 2011

0 votes

This is a very very very very simple module, just for those people who are just beginning, I remember when i was having struggles with moving the camera with objects outside of the screen. Go to sites.google.com/site/myplace2go2/corona-sdk and download the attachment camera.lua
How to use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
---------------------
-- require camera
---------------------
local camera = require("camera")
local physics = require("physics")
physics.start()
-----------------------------
-- objects
------------------------------
local object1 = display.newRect(100, 600, 100, 100)
object1:setFillColor(255, 100, 255)
 
local ground = display.newRect( 50,700, 1000, 700 )
ground.rotation = 2
ground:setFillColor(250,40,200)
 
local ball = display.newCircle(210, 200, 30)
 
----------------------
-- adding physics
----------------------
physics.addBody(ground, "static", {friction =.1, bounce = .2})
physics.addBody(ball, "dynamic", {friction =.1, bounce = .2, density = 1})
physics.addBody(object1, "static", {friction =.1, bounce = .2})
 
-------------------------------------
-- Here is the important part
-- for moving the camera
----------------------------------------
-- you must insert all objects you created in camera like so:
camera:insert(ball)
camera:insert(object1)
camera:insert(ground)
 
 
 
--now to move the camera:
local function moveCamera(event)
if ball.x > 200 then
camera.x = -ball.x +200
end
end
Runtime:addEventListener("enterFrame", moveCamera)


Replies

nicholasclayg's picture
nicholasclayg
User offline. Last seen 13 hours 27 min ago. Offline
Joined: 16 May 2011

I was building a copter like game as well. This works a lot better :)

I like the feel of the floatiness of the ball, I had a hard time fine tuning that to act "right"

You nailed it. :)

ng

LeivaGames's picture
LeivaGames
User offline. Last seen 2 weeks 5 days ago. Offline
Joined: 28 Jan 2011

Great Job this couldnt had come at a better time.

nicholasclayg's picture
nicholasclayg
User offline. Last seen 13 hours 27 min ago. Offline
Joined: 16 May 2011

I didn't find the camera.lua necessary.

The easy fix was comment out the require on the camera, and then take the stuff from camera

local camera = display.newGroup()

--the rest of main.lua here

return camera -- at the bottom

--That is coming from the DL'd source code from yourwebsite. I actually haven't tried the above code, I went straight for the good stuff.

Obviously that's just to get it working. If you wanted to REALLY get proper then you would be doing it differently with some kind of modular coding approach, oop etc.

I don't like using the module package seeall stuff, i'm superstitious :)

ng