how do you keep the camera zoomed in on a character for side scroller games like mario

i was wondering how you keep the camera on the player instead of showing the entire level … like in the mario games you only see mario and some of the level not the whole thing and the camera follows him how do they do that cause all i know is that you setup a sensor so it moves with him but what about makeing it so that you follow the character … basicly a mario style camera setup

im not asking for someone to write a script (even though that would be awesome ) i just want a few hints like “use this command and set the coordinate” something like that

i noticed someone said to use a runtime event listener in this other post

http://developer.anscamobile.com/forum/2011/05/31/view-or-cam

im guessing that would be so when mario moves forward to a certain point the camera would follow him, right?
[import]uid: 47760 topic_id: 10757 reply_id: 310757[/import]

You will need an event listener on the “enterFrame” event but it really doesn’t have much to do with the logic.

For the basic Mario game, he usually starts on the left hand side and then as you move to the right when you get near the center, the background starts moving instead of mario. When you stop or go backwards, then you move to another point and the background starts scrolling again.

local gameBg = display.newImage("background.png", display.contentHeight/2, 0, true) no scaling  
local mario = display.newImageRect("mario.png", 32, 32)  
mario.x = 16  
mario.y = display.contentHeight/2  

Then when you need to move mario,

if moveRight then  
 if mario.x \< display.contentWidth / 0.67 then  
 mario.x = mario.x + 1  
 else  
 background.x = background.x - 1  
 end  
end  
  
if moveLeft then  
 if mario.y \> display.contentWidth / 0.33 then  
 mario.x = mario.x - 1  
 else  
 background.x = background.x + 1  
 end  
end  

Now of course that doesn’t cover all possibilities… You would still need to make sure that when the background is as far left as it can go, mario then moves to the edge of the screen (or as far right, etc.)

That should, if the actor is i the middle 3rd of the screen, move the actor. If the actor tries to get outside of that center 3rd, then you scroll the background until you can’t scroll the background any longer and in that case, let the actor out of the middle 3rd.

[import]uid: 19626 topic_id: 10757 reply_id: 39075[/import]

@need420spweed,

old school way…

If you have a side scroller, then the background is composed of smaller tiles not a gigantic image, and the way it works is that the background is rendered around the point the user is located. So each time the user moves, the background is updated around his co-ordinates giving the illusion of a scrolling background.

hope that helps,

?:slight_smile: [import]uid: 3826 topic_id: 10757 reply_id: 39088[/import]

ok i get it but im still confused lol

i get the main idea of how its supposed to work but what i dont get is the code im looking everything up through here right now

http://developer.anscamobile.com/reference/index

ok just to have something simple to start with, without bricks and stuff floating in the air

the background and the ground image would be separate but move at the same time right? cause the ground would need to have physics but have to be static? and the background would just be a background or am i wrong?
[import]uid: 47760 topic_id: 10757 reply_id: 39133[/import]

Well you might have to move the background and a display group that contains the foreground objects independently. In fact this is a good time to add a cool feature called parallex where the background and the foreground move at different rates giving a little bit of a 3D look to things. [import]uid: 19626 topic_id: 10757 reply_id: 39182[/import]