Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: java game, both players move for player 2 but not player 1

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java game, both players move for player 2 but not player 1

    hey guys im making my first java game and everythign was going smoothly till i added the movements, player 1 moves with W and S (up and down obvs) and player 2 moves with up arrow and down arrow, when player 1 moves up or down he moves fine but when player too moves so down player one, i will post the code and can someone see if they can solve this ty

    THIS IS FROM THE MAIN SCREEN
    @Override
    public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_UP) {
    player2up = true;
    } else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
    player2down = true;
    } else if(e.getKeyCode() == KeyEvent.VK_W) {
    player1up = true;
    } else if(e.getKeyCode() == KeyEvent.VK_S) {
    player1down = true; }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_UP) {
    player2up = false;
    } else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
    player2down = false;
    } else if(e.getKeyCode() == KeyEvent.VK_W) {
    player1up = false;
    } else if(e.getKeyCode() == KeyEvent.VK_S) {
    player1down = false; }
    }


    @Override
    public void keyTyped(KeyEvent c) {
    ;

    }

    THIS IS FROM THE PLAYER FILE
    @Override
    void update(final shooter shooter, int id) {
    if (id == 1) {
    if(shooter.isPlayer1up()) {
    if(!(ypos < 26)) {
    ypos--;
    }

    } else if(shooter.isPlayer1down()) {
    if(!(ypos > shooter.getHeight() -96)) {
    ypos++;

    }

    }

    } else if(id == 2) {}
    if(shooter.isPlayer2up()) {
    if(!(ypos < 26)) {
    ypos--;
    }

    } else if(shooter.isPlayer2down()) {
    if(!(ypos > shooter.getHeight() -96)) {
    ypos++;
    }
    }


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java game, both players move for player 2 but not player 1

    nvm my silly mistake i noticed the problem it was "} else if(id == 2) {}" it should be "} else if(id == 2) {"

Similar Threads

  1. 2 Player Poker Game Troubles
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 01:20 PM
  2. [SOLVED] Platform game player doesn't respond to keyboard
    By jesamjasam in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 09:44 AM
  3. Java Mp3 player.
    By alex901 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 18th, 2010, 09:12 AM
  4. Music player problem
    By abhinavhardikar in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2010, 08:41 PM
  5. [ASK] JMF Class Player
    By bocahTuaNakalzz in forum Java SE APIs
    Replies: 2
    Last Post: December 8th, 2009, 03:40 AM