Results 1 to 4 of 4
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Changing variables in if statements

    Hey I'm trying to work out how to make a JLabel move around the screen with the wasd keys.
    To do this I'm doing something like this:

    int x = 0;
    int y = 0;
    if (input == 'w') {
    y -= 5;
    }
    else if (input == 'a') {
    x -= 5;
    }
    label.setLocation(x, y);

    Only problem is, since I'm changing the value of the x and y variables inside an if statement, its not actually changing the value of the variable outside.
    How do I make it do that?

    Sorry for the dumb question, bit of a newbie

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Guy View Post
    Hey I'm trying to work out how to make a JLabel move around the screen with the wasd keys.
    To do this I'm doing something like this:

    int x = 0;
    int y = 0;
    if (input == 'w') {
    y -= 5;
    }
    else if (input == 'a') {
    x -= 5;
    }
    label.setLocation(x, y);

    Only problem is, since I'm changing the value of the x and y variables inside an if statement, its not actually changing the value of the variable outside.
    How do I make it do that?
    But you defined those variables outside of those if-statements so those are the variables that are changed by those if-statements. What made you think otherwise?

    kind regards,

    Jso
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Hey. I'm sorry I phrased my question very badly and just realized the answer was very obvious. The part of code I posted was actually inside a method and each time I called the method it set the x and y variables to 0.
    Sorry for wasting your time, but thanks for the quick reply!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Guy View Post
    Hey. I'm sorry I phrased my question very badly and just realized the answer was very obvious. The part of code I posted was actually inside a method and each time I called the method it set the x and y variables to 0.
    Sorry for wasting your time, but thanks for the quick reply!
    No need to say sorry; if you defined those variables in a method (they're local to that method then) they're initialized over and over again when that method gets executed. Better define them as member variables (outside of any method) so they can keep their value assigned to them in that method (that's what you want here).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. if statements for multiple variables
    By dookie1293 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 09:30 AM
  2. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  3. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Changing color of variables view details
    By mtoledo in forum Eclipse
    Replies: 0
    Last Post: 03-25-2008, 02:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •