XNA question

Started by
3 comments, last by NickGravelyn 15 years, 11 months ago
Im working with XNA on a 2D game I am creating and I want to make it so that when the player has his or her ship on the top of the visible screen, the screen will shift up with a player to reveal more of the map. I tried to shift the screen up by changing the X and Y values of the Graphics Device's Viewport but The compiler gives me an error saying that these values cannot be modified because they are not variables. Is there an easy way to do this for a newbie like me.
J.W.
Advertisement
Post the code that gives you the error and the exact error.

            // in my Game's Update function            if (Keyboard.GetState ().IsKeyDown ( Keys.Up ))            {                this.GraphicsDevice.Viewport.X++;            }


error:

Cannot modify the return value of 'Microsoft.Xna.Framework.Graphics.GraphicsDevice.Viewport' because it is not a variable



I know that I cannot modify the X and Y values of viewport and I think I am getting the wrong Idea by trying to. I just tested it because I thought it might work.
J.W.
Try changing it to:
Viewport vp = GraphicsDevice.Viewport;vp.X++;


That might eliminate the compiler error, but it probably won't do what you want (I'm not very familiar with XNA).

To answer your question, I would need to see how you're drawing your map.
Your error is related to the idea of reference types and value types. See this for more explanation and links: http://forums.xna.com/forums/t/7655.aspx.

As to your real problem of scrolling the map, changing the viewport will not do what you want. The viewport is simply what area of the screen should be rendered to. You can use smaller viewports to do things like split-screen, but it isn't how you scroll a map.

To scroll the map, you should create a simple 2D camera and move that camera's position. Then you can use the camera to offset any of your objects in the game. There are lots of ways to make a 2D camera, but here's one I found doing a quick Google search: http://www.paradeofrain.com/?page_id=32.

This topic is closed to new replies.

Advertisement