Best way to have the camera follow my hero


(Nate ) #1

Hey guys! I have been posting here left and right lately and have yet another question!

I have a simple world, filled with blocks and my hero. My physics are there for the most part in terms of the hero moving, gravity and acceleration.

My next question is, after reviewing some camera code and stage scrolling code here and elsewhere on the internet, what would be the best bet for simply having a camera follow my hero and or a scroll of the stage.

More that I think about it, I think it would be more valuable to me to scroll the stage.

What would be the simplest, most effective way to do this?

Thank you!


(Kevin Graham) #2

If no one has given you an answer by the time i’m on here later tonight, i’ll post the code i’m using to scroll the camera in the game i’m currently working on.


(Nate ) #3

Okay thank you very much! It is very much appreciated :smiley:


(Kevin Graham) #4

I realized I have an old simple platform game I made that has better camera movement than the game I’m currently working on. Here’s a demo, tell me if this is the kind thing you want and I can send you the camera code.


(Ultima2876) #5

Check this demo out. It has great camera motion!


(Kevin Graham) #6

So is this the kind of thing your looking for?


(Nate ) #7

Ultima2876, I did already check out that demo I do enjoy the camera motion! However I think I am going to go with basic scrolling which I can see in ShadowRider’s demo!

ShadowRider, I would very much appreciate access to your code! :smiley: Thank you!

Basically what I want to do IN A NUTSHELL is… I have a hero… and a 1280x720 screen, I want to make an even WIDER image for the level, and I would like the screen to scroll as my hero walks! Like in your demo!


(Kevin Graham) #8

OK, I’ll send you the code to the game when I’m back on my comp :smile:


(Nate ) #9

Okay sounds good! I just beat Smilyes Day Off! :smiley:


(Kevin Graham) #10

Haha there really wasn’t much to that game, just something I was playing around with to get better at coding in FP


(Kevin Graham) #11

But i’m glad you liked it! :smile:


(Nate ) #12

I did like it! :smiley: Do you know of any books or other resources that are good for building FP knowledge? Also are you going to message me the scrolling code etc? Or how did you want to do this?


(Kevin Graham) #13

Pop this in your update method of your games world, and tweak the values and you should be good to go! Let me know if you have any more questions :smiley:

  /************************* Camera ********************/
FP.camera.x = (player.x - (FP.halfWidth - 16));// Follow the player
FP.camera.y = (player.y - (FP.halfHeight - 16));// with the camera
			
FP.camera.x = FP.clamp(FP.camera.x, 0, (mapWidth - FP.width));
FP.camera.y = FP.clamp(FP.camera.y,0, (mapHeight - FP.height));
/*****************************************************/

(Nate ) #14

Okay I am about to plug all of this in! What should I do for the mapWidth and mapHeight values?


(Nate ) #15

Also this is a very noob question… but I am currently adding my player in the world class with this line… what would I have to do to be able to access the X and Y value of the player from the world class?

add(new thePlayer);

(Kevin Graham) #16

The only difference is mine is called player, and yours is thePlayer, so replace the player accordingly.


(Kevin Graham) #17

thePlayer.x and thePlayer.y


(Nate ) #18

Okay I have addressed all of the player issues, but I am not sure what to put for mapWidth and mapHeight. I tried manually putting in the stage information but that then made my character move from spawn.


(Nate ) #19

Okay everything is compiling but I am not sure what to do for mapWidth and mapHeight still! D: My character is moving and such, the camera doesn’t follow him though.


(Kevin Graham) #20

You could do it manually if all your levels are going to be the same size, but what i’ve done is set the variables from the ogmo level file when I load it.


World States [several questions]?