XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Simple newb question - answer needed

Last post 2/23/2008 2:17 PM by GD Chidiko. 4 replies.
  • 2/23/2008 1:08 PM

    Simple newb question - answer needed

    I am just starting to learn how to program so forgive me if I don't make any sense lol.

    I just finished the Your First Game: Microsoft XNA Game Studio in 2D tutorial and I am experimenting with it now.

    What I wanted to know was how to get my sprite drawn somewhere other than the top left corner of the screen when I first run it. For example, the top right corner, the center, or even specific coordinates.

    Any help is always greatly appreciated. Thanks =]

  • 2/23/2008 1:22 PM In reply to

    Re: Simple newb question - answer needed

    If you've finished the tutorial, you should already have a sprite drawn somewhere other than the top left corner of the screen. Step 5 in the tutorial shows how to make the sprite move and bounce...

     

  • 2/23/2008 1:49 PM In reply to

    Re: Simple newb question - answer needed

    correct. I have modified the code so that I can move the sprite around with the arrows on the keyboard, and the sprite bounces off the walls of the screen and head in the opposite direction. When I first run the program though, the sprite is drawn on the top left corner of the screen. What I want is for it to be drawn on lets say... the bottom left corner of the screen when I first start it up.
  • 2/23/2008 2:11 PM In reply to

    Re: Simple newb question - answer needed

    Answer
    Reply Quote
    One way to do it is to set its position in the Initialize() method. Something like:

            protected override void Initialize()
    {
    spritePosition = new Vector2(100, 100);
    base.Initialize();
    }

    to give it a position of (100, 100).

    If you want it in the center of the screen, you can use the Viewport property from the GraphicsDevice to get the dimensions of the screen, and use that to position it in the center:

            protected override void Initialize()
    {
    int screenWidth = GraphicsDevice.Viewport.Width;
    int screenHeight = GraphicsDevice.Viewport.Height;

    spritePosition = new Vector2(screenWidth / 2.0f, screenHeight / 2.0f);
    base.Initialize();
    }

  • 2/23/2008 2:17 PM In reply to

    Re: Simple newb question - answer needed

    That makes perfect sense. I can't believe I didn't see that before! Thank you very much =)
Page 1 of 1 (5 items) Previous Next