none
("Solved") XNA Newbie help RRS feed

  • Question

  • In our class we are learning XNA which is some sort of plugin for Visual C# for creating games. I can't ask my IPT teacher for help since it's school holidays currently and im just doing a little revision

    In our lesson files we were taught how to move an object using the keyboard and then we were taught how to have randomly moving objects by themselves collide with the edges of the window and bounce back, but the lessons never taught us how to be able to move both the player and have its movement restricted inside the window at the same time, no one else in the class knows how to do this (our main IPT teacher decided to take a break for a year; our substitute isn't fluent in C#), but i've been trying myself and got it half working (literally). I've managed to stop players from leaving the top and left sides of the screen but I have problems with the bottom or right sides

    EDIT:: Silly me not describing the problem properly, I can't even move downwards or right, doing as joel suggested didn't help, and if i just remove the conditional for the player to be inside the window it works just fine except they can of course just walk through the screen boundaries

    public void Update() { //edges of screen: prevent player form leaving screen boundaries, gets right and bottom edge of window's positions int rightEdge = GraphicsViewport.Width - playerCurrentTex.Width; int bottomEdge = GraphicsViewport.Height - playerCurrentTex.Height; //player movement if (Keyboard.GetState().IsKeyDown(Keys.W)) { if (playerPosition.Y > 0) { playerPosition.Y = playerPosition.Y - 4; } } if (Keyboard.GetState().IsKeyDown(Keys.S)) { if (playerPosition.Y < bottomEdge) { playerPosition.Y = playerPosition.Y + 4; } } if (Keyboard.GetState().IsKeyDown(Keys.A)) { if (playerPosition.X > 0) { playerPosition.X = playerPosition.X - 4; } } if (Keyboard.GetState().IsKeyDown(Keys.D)) { if (playerPosition.X < rightEdge) { playerPosition.X = playerPosition.X + 4; } } }





    • Moved by Bob Shen Friday, April 12, 2013 8:34 AM
    • Edited by nihao123456ftw Saturday, April 13, 2013 8:36 AM Solved
    Wednesday, April 10, 2013 12:22 PM

All replies

  • Simple

    From :

    int rightEdge = GraphicsViewport.Width - playerCurrentTex.Width;
    int bottomEdge = GraphicsViewport.Height - playerCurrentTex.Height;

    To :

    int rightEdge = GraphicsViewport.Width;
    int bottomEdge = GraphicsViewport.Height;


    jdweng

    Wednesday, April 10, 2013 4:20 PM
  • sorry, but that didn't do anything when I tried it
    Wednesday, April 10, 2013 9:56 PM
  • In additiona to my previous change try this

    From : if (playerPosition.Y < bottomEdge)

    To :     if (playerPosition.Y < bottomEdge - 5)

    And this change

    From : if (playerPosition.X < rightEdge)
    To :    
    if (playerPosition.X < rightEdge - 5)




    jdweng

    Wednesday, April 10, 2013 10:43 PM
  • Two things I'm curious about, what is playerCurrentTex and do you know where your origin (0,0) is? (Should be top left)

    Secondly, is the playerPosition object's X and Y stored in the same units as the viewport height and width or are they relative?

    Wednesday, April 10, 2013 10:49 PM
  • playerCurrentTex is the player's texture, yes I know where the origin is, and also as a side note I got help of this code off a tutorial online, so i'm clueless as to why this doesn't work

    I don't really understand your second question sorry, do you mean that if they're both intergers? Because they are.

    Thursday, April 11, 2013 6:26 AM
  • Hi nihao123456ftw,

    For XNA please use : http://xboxforums.create.msdn.com/forums/default.aspx?GroupID=6

    Thanks.


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 12, 2013 8:34 AM
  • Ok i've solved the problem. I replaced

    GraphicsViewport.Height - playerCurrentTex.Height;

    with just a number

    360 - playerCurrentTex.Height;

    and vice versa for width. Strange, I altered the code above by removing rightEdge and bottomEdge and just putting the formula in the if condition, and it worked but the boundaries were out of place, not positioned at the place I wanted it to. I even set the viewport bounds manually and I still had the same problem. Writing it as a number (the same number as I set the window size) worked. Oh well, at least this worked

    Saturday, April 13, 2013 8:36 AM