Jump to content

  • Log In with Google      Sign In   
  • Create Account

Using Keyboard in C#


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Hasenjäger   Members   -  Reputation: 102

Like
0Likes
Like

Posted 25 August 2013 - 05:53 AM

Hi,
 
I'm new on this forum and after reading a while I decided to start. I choose as environment Visual Studio Express 2010 with C# and XNA 4.0, because I thought it's well documented to get better tutorials and so on.
 
I have no programming skills. I have some ressources like microsoft forum and some ebooks like O'Reillys "Learning XNA 4.0".
 
my question is.. to learn the first steps I would try to work with 2d sprites.
 
I have a KeyboardState and my sprite is showing and it works but if I want to use my keyboards, nothing happens.
 
I write this code in the Update method:
 

// this 2 lines are in my class game
Vector2 ringsPosition = Vector2.Zero;
const float ringsSpeed = 6;

// this move threerings based on keyboard input are in my update method 
KeyboardState keyboardState = Keyboard.GetState(); 
if (keyboardState.IsKeyDown(Keys.Left)) 
    ringsPosition.X -= ringsSpeed; 
if (keyboardState.IsKeyDown(Keys.Right)) 
    ringsPosition.X += ringsSpeed; 
if (keyboardState.IsKeyDown(Keys.Up)) 
    ringsPosition.Y -= ringsSpeed; 
if (keyboardState.IsKeyDown(Keys.Down)) 
    ringsPosition.Y += ringsSpeed;

 
The Microsoft.XNA.Framework.Input is in using and works. If I start to debug and test, my sprites are animated but nothing happend if I use my keyboard.
 
any ideas? Or did you need the complete code to check what I'm doing wrong?
thanks.



Sponsor:

#2 shazen   Members   -  Reputation: 282

Like
0Likes
Like

Posted 25 August 2013 - 06:29 AM

Is the line:

 

Vector2 ringsPosition = Vector2.Zero;

 

 

being called every frame or just once at the beginning of the game?



#3 Hasenjäger   Members   -  Reputation: 102

Like
0Likes
Like

Posted 25 August 2013 - 06:57 AM

just at the beginning of the game in 

public class Game1 : Microsoft.Xna.Framework.Game
{

some stuff..

Vector2 ringsPosition = Vector2.Zero;

}


#4 Prot   Members   -  Reputation: 333

Like
0Likes
Like

Posted 25 August 2013 - 08:41 AM

Try to initialize ringposition with something else. Like, new Vector2(10,10)



#5 unbird   Crossbones+   -  Reputation: 4974

Like
0Likes
Like

Posted 25 August 2013 - 08:54 AM

That code should work. Show the whole Game1 class, please.

 

PS: Are you familiar with breakpoints and inspecting variables (i.e. the interactive debugger of Visual Studio)?


Edited by unbird, 25 August 2013 - 08:56 AM.


#6 DpakoH   Members   -  Reputation: 982

Like
0Likes
Like

Posted 25 August 2013 - 08:59 AM

KeyboardState usage looks fine, but i am not sure if you can increment only X component of a vector. maybe you should try:

 

ringsPosition += new Vector(0,6); 

 

of course this is of top of my head, if you upload your code online, I will be able to determine for sure the problem. 

 

have a nice day!



#7 unbird   Crossbones+   -  Reputation: 4974

Like
0Likes
Like

Posted 25 August 2013 - 09:21 AM

...but i am not sure if you can increment only X component of a vector...


That's fine if it is an accessible field (or even as an array element). Won't work for properties, though, since then you work on a copy (but the compiler will complain then)

#8 j-locke   Members   -  Reputation: 816

Like
0Likes
Like

Posted 25 August 2013 - 10:06 AM

I would say to make sure that in the Draw method you're using the ringsPosition.X and ringsPosition.Y values for where to draw the rings. Might need to see some source code to offer up any better insight.



#9 Hasenjäger   Members   -  Reputation: 102

Like
0Likes
Like

Posted 25 August 2013 - 01:58 PM

Hi,

 

I tried and now it works.. but I didn't know why is now working.. I can't reproduce it :-( 

I need definitly work with different versions if something is not working..

 

 

PS: Are you familiar with breakpoints and inspecting variables (i.e. the interactive debugger of Visual Studio)?

 

No I didn't know.. as I said.. I'm absolute novice.. I don't have any programming skills.. It's my first try.. (in my past I did work with HTML & CSS, but this is anything else but not a programming language..)

 

Thank you very much for all the responses.. 

best-



#10 unbird   Crossbones+   -  Reputation: 4974

Like
0Likes
Like

Posted 25 August 2013 - 03:01 PM

Well, glad it's working.biggrin.png

 

For starting out so-called version control is probably overkill, but having a manual history of your progress helps.

 

Search for (video) tutorials about debugging C#. This is something you wanna learn right now.


Edited by unbird, 25 August 2013 - 03:02 PM.


#11 Hasenjäger   Members   -  Reputation: 102

Like
0Likes
Like

Posted 25 August 2013 - 03:39 PM

Search for (video) tutorials about debugging C#. This is something you wanna learn right now.


okay sounds good, will do it. thx again :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS