Results 1 to 7 of 7
  1. #1
    jlennards's Avatar
    jlennards is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Problem with scope

    I need help with a scope problem. Here is my code.

    Java Code:
    public class someClass implements KeyListener {
    
    int px = 300;
    int py = 200;
    Player p1 = new Player();
    
    public someClass () {
    // Jframe made here
    jframe.add(p1);
    p1.setBounds(px, py, 45, 50);
    }
    
    public void keyPressed (KeyEvent e) {
    px += 50;
    p1.setBounds(px, py, 45, 50);
    }
    
    }
    Ok well I want to move the p1 object with a keyPressed event. It compiles but it seems to be out of scope. Any way on how to fix this?

    Note: I know this isn't valid code, but I just want to get the idea across.
    Last edited by jlennards; 07-16-2011 at 12:27 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    jlennards's Avatar
    jlennards is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    When I call the Method with the keypressed event, the object dosen't move.

    By habit I blame scope.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jlennards View Post
    When I call the Method with the keypressed event, the object dosen't move.

    By habit I blame scope.
    It's not really a scope issue but likely a reference issue or a focus issue. For a KeyListener to work, the component it is listening to must have focus. You can check to see if your key listener is active by adding some System.out.println statements in it and see if they print. If the KeyListener is working well, then again your issue could be one of reference -- that you're moving a Player object, but just not the currently visualized Player object. Understand that every time you call new Player(), you are creating a new and distinct Player object. If you want this class to act on a Player object that has been created elsewhere, you'll need to pass a reference of that Player object into this class, perhaps as the parameter of a setPlayer method.

  5. #5
    jlennards's Avatar
    jlennards is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It's not really a scope issue but likely a reference issue or a focus issue. For a KeyListener to work, the component it is listening to must have focus. You can check to see if your key listener is active by adding some System.out.println statements in it and see if they print. If the KeyListener is working well, then again your issue could be one of reference -- that you're moving a Player object, but just not the currently visualized Player object. Understand that every time you call new Player(), you are creating a new and distinct Player object. If you want this class to act on a Player object that has been created elsewhere, you'll need to pass a reference of that Player object into this class, perhaps as the parameter of a setPlayer method.
    Can anyone tell me how to do that?
    Last edited by jlennards; 07-16-2011 at 04:14 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jlennards View Post
    Can anyone tell me how to do that?
    I thought I just did:

    Quote Originally Posted by Fubarable
    If you want this class to act on a Player object that has been created elsewhere, you'll need to pass a reference of that Player object into this class, perhaps as the parameter of a setPlayer method.
    If anything confuses you, then ask please. Of course the details of your specific solution will depend on your program structure, but play around with it a bit and see what you can come up with.
    Last edited by Fubarable; 07-16-2011 at 04:35 AM.

  7. #7
    jlennards's Avatar
    jlennards is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. ArrayList going out of scope
    By meustace in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 01:14 AM
  2. Scope Problem???
    By c_walker in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 02:30 AM
  3. Problem with Session Scope while using with Java Bean
    By sulthanmytheen in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2009, 10:50 AM
  4. [SOLVED] scope problem with a servlet
    By pnolans in forum Java Servlet
    Replies: 7
    Last Post: 08-23-2008, 02:50 PM
  5. return out of scope?
    By another_steve in forum New To Java
    Replies: 5
    Last Post: 01-28-2008, 09:34 PM

Posting Permissions

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