Camera movement with transition.to

So I have a problem. I'm making a game, and when stuff moves on the screen, I want the camera to follow the player.
I've done that by putting everything in a display group, and setting the coordinates of that display group relative to the players'. Kind of like this:

1
displayGroup.x = - player.x + 200 -- [or any constant]

I'll bump this, maybe someone can help me... Anybody? :)

Have you looked at the Egg Breaker example code? It does similar to what you're looking for. You'll want to just use an enterFrame listener to update the position, rather than a transition.to .

On a second note, it sounds like you're also wanting to sort of ease the movement? You'll want to do that programmatically as well. Check out some "programmatic animation" tutorials if you need help with this.

Thanks Adam. I'm just looking at this for the first time. For Florin, it looks like egg breaker is doing the following...

1
2
3
4
5
6
7
8
-- Camera follows bolder automatically
local function moveCamera()
        if (boulder.x > 80 and boulder.x < 1100) then
                game.x = -boulder.x + 80
        end
end
 
Runtime:addEventListener( "enterFrame", moveCamera )

Yes, I did that, that wasn't a problem. While my player moves in one direction, the display group behind it containing all the other items moves in the opposite direction to create the "moving camera" effect. That's ok, works. But my problem is that it feels too rigid. How can I make it smooth?
Like, when my player moves, the display group behind it behaves somewhat elastic, as opposite to "glued to the player", like it feels now.
To better understand what I said, imagine that everything behind the player is linked to it through some kind of distance physics joint, so that when the player moves, the display group starts to move slowly, then gains speed as it approaches it's "destination", and when it's about to reach the player's position, slows down again. Do I make any sense?... :)

The most common (and easiest) way to accomplish this is by using the difference between the player (or the object you want to track) and the current camera position by X, and then adding that to the camera position.

For this example we say that X is 10, and all coordinates are on the X axis. For the y axis, you basicly do the same thing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Frame 1:
Player is at 500, camera is at 0.
500 - 0 = 500
500 / 10 = 50
camerapos = 0 + 50 = 50 <- new camera position
 
Frame 2:
Player is at 500, camera is at 50.
500 - 50 = 450
450 / 10 = 45
camerapos = 50 + 45 = 95 <- new camera position
 
...
 
Frame 3:
Player is at 300 (he moved left), camera is at 95.
300 - 95 = 205
205 / 10 = 20.5
camerapos = 95 + 20.5 = 115.5 <- new camera position

Hi can you give an example ? It will be nice to see how to use it..

Take a look at the Ghosts vs. Monsters sample app ... it shows camera movement (left and right) via transition.to as well as manual camera movement with your finger.

Here's the link:

http://developer.anscamobile.com/code/ghosts-vs-monsters

views:1639 update:2011/10/3 8:06:12
corona forums © 2003-2011