Sign in to follow this  

XNA question

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites



// 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this