1

I was looking for tutorials on the net for a 2d camera that follows a sprite and I found a reply from this site:

(XNA 2D Camera Engine That Follows Sprite)

I made a simple game where in the sprite is loaded at the center of the game screen and it moves according to the direction I press from my directional key pad.

I tried implementing this code and added the Spritebatch instruction to my game. There seem to be no syntax error, but once I run the game, I only see the blue screen. Also, the sprite I used didn't appear.

(I'd like to imitate the player control of Tasty Planet where in the goo is controlled by the mouse and the screen follows it around. - trailer of the game: http://www.youtube.com/watch?v=az4VgetA_n0

Community
  • 1
  • 1
carl flor
  • 21
  • 1
  • 2

1 Answers1

1

Game development is sometimes best accomplished step by step. Especially when one is first learning :-)

What you want to do is first get some basics onto the screen 1) the player's avatar, and b) some static component like a wall, or floor.

With those in place and rendering on the screen, then you implement your camera component. This will be a class that takes the player's position in the world and offsets any "world" item by that much.

For example, if the player is at 10,10 in the world, and there is a tree at 5,5 ... the tree should be drawn at -5,-5, and the player drawn at 0,0 (assuming your coordinate grid's 0,0 is in the middle of the screen). And as the player's position moves, just subtract that from static world object that you draw.

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • Is that seriously the best way of doing this? – Mathias Lykkegaard Lorenzen Sep 09 '11 at 21:46
  • yes :-) though the camera is one of those awesome abstractions that you can write once and then not have to worry about it from that point on. Just give the camera an "AttachTo" method that takes one of your game entities ... that way as that entity moves about in the world, it can use it's coordinates to draw the rest of the world accordingly. The world is a complex place, you'll find that you don't spend a lot of time worrying about the interconnections and reactions of the atoms that make up our bodies, our tools, and our world ... this is the same kind of thing :-) – Joel Martinez Sep 12 '11 at 14:18