views:

96

answers:

1

Hello, I am currently trying to write a simple shoot them up game with top down perspective. I am using SurfaceView and Canvas to draw the graphics on the screen, but I cannot find out how to make the "camera" scrollable. The player character is always in the center of the screen and the camera(SurfaceView's canvas essentially) is supposed to be following him all the time(for example, like in Alien Swarm). However, I cannot figure out how to make that happen in a regular manner. The way I am doing it right now is by moving every other object in the game world according to the player's input - and drawing them if their coordinates happen to be on screen. The player stays on the same coordinates all the time. Is there any way to actually move(scroll) the SurfaceView itself? I saw the Scroll widget and the method SurfaceView.scrollBy(int x, int y), but my attempts to implement either of those result in Force Close. Any help is appreciated, thanks in advance!

P.S. Just a note - I am not interested in using open GL for this project.

A: 

You can use Translation to slide the contents of the screen after you've drawn it. If the character is to remain in the middle, you'll write code that looks like this:

draw_the_screen();
canvas.translate(x,y);
draw_the_avatar();

When you translate, everything up until that point will be translated. When you draw the avatar, the translation (sliding) will have already taken place and it'll end up wherever you draw it (i.e.: the center of the screen).

Hope this helps.

mattbasta
Thanks, that was helpful, but I had to draw the player firs, translate and then draw the screen in order to make it work. I find that quite weird. Here is my code: px = px + accelerometer_x; py = py + accelerometer_y; canvas.drawBitmap(mObj4, 50, 350, null); canvas.translate((float)px, (float)py); canvas.drawCircle(218, 368, 10, p);This does not work, as the scenery (mObj) stays static, but the character(the circle) moves in the screen. It is supposed to be the opposite and when I switch the places of drawObj and drawCircle it works. Weird.
Kiril