0
\$\begingroup\$

I'm a beginner at java and I'm trying to make a simple 2D game as a practice, and i have a problem here with the player, I've tried a lot to make it move but it didn't work. These are the classes that contain the player.

Game Class :

package game;

import game.input.Controller;
import game.input.KeyInput;
import game.objects.Player;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game extends JPanel implements ActionListener{

  Timer loopTimer;
  //Objects
  Player p;
  Controller c;

  public Game(){
    addKeyListener(new KeyInput(p));
    setFocusable(true);

    loopTimer = new Timer(10, this);
    loopTimer.start();

    p = new Player(300,200);
    c = new Controller();
  }

  public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, 640, 480);
    p.draw(g2d);
    c.draw(g2d);
  }

  public void actionPerformed(ActionEvent ee){
    p.update();
    c.update();
    repaint();
  }    
}

Player Class :

package game.objects;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class Player extends GlobalPosition {

  private String playerimage = "/images/marioR.png";

  int velX = 0;
  int velY = 0;

  public Player(int x, int y) {
    super(x, y);
  }

  public void draw(Graphics2D g2d){
    g2d.drawImage(getPlayerImage(), x, y, null);
  }

  public Image getPlayerImage(){
    ImageIcon i = new ImageIcon(getClass().getResource(playerimage));
    return i.getImage();
  }

  public void update(){
    x += velX;
    y += velY;

    // Collision with outside
    if(x < 0){ 
      x = 0;
    }
    if(y < 0){
      y = 0;
    }
    if(x > 640){
      x = 640;
    }
    if(y > 480){
      y = 480;
    }
  }

  public void keyPressed(KeyEvent e){
    int c = e.getKeyCode();
    if(c == KeyEvent.VK_LEFT){
      velX = -10;
    }
    if(c == KeyEvent.VK_RIGHT){
      velX = 10;
    }
    if(c == KeyEvent.VK_UP){
      velY = -10;
    }
    if(c == KeyEvent.VK_DOWN){
      velY = 10;
    }
  }

  public void keyReleased(KeyEvent e){
    int c = e.getKeyCode();
    if(c == KeyEvent.VK_LEFT){
      velX = 0;
    }
    if(c == KeyEvent.VK_RIGHT){
      velX = 0;
    }
    if(c == KeyEvent.VK_UP){
      velY = 0;
    }
    if(c == KeyEvent.VK_DOWN){
      velY = 0;
    }
  }

  public void keyTyped(KeyEvent e){
  }
}

KeyInput Class :

package game.input;

import game.objects.Player;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter{

  private final Player p;

  public KeyInput(Player p){
    this.p = p;
  }

  public void keyPressed(KeyEvent e){
    p.keyPressed(e);
  }

  public void keyReleased(KeyEvent e){
    p.keyReleased(e);
  }
}

.

\$\endgroup\$
  • 2
    \$\begingroup\$ Please fix the formatting of this question, it's incredibly difficult to discern anything valuable from this code. \$\endgroup\$ – Charanor Jan 28 '17 at 15:40
  • 2
    \$\begingroup\$ "it didn't work" is a very non descriptive way to describe your issue. What behaviour do you expect (make character move, yeah, how??), and what did you get instead? \$\endgroup\$ – Vaillancourt Jan 28 '17 at 17:20
2
\$\begingroup\$

actionPerformed() is never called, so nothing is updated or repainted. You need to register it. To check that it's registered, simple add System.out.println("I Work!!!!") to the body of the method.

Also, a tip for a new Java programmer: place the @override annotation before methods you override. In your case, add it on the lines before keyPressed() and keyReleased in the KeyInput class.

Please take this the right way. Don't just jump into creating a game until you fully understand the language you're working with. There are tons of resources online (articles, texts, videos, etc...) that will teach you Java quickly. It's not a hard language to learn. Master it first, then developing a game will be no problem!

\$\endgroup\$
  • \$\begingroup\$ Did this fix your issue? Please give me a check if so, thank you. This forum is built on reputation. \$\endgroup\$ – Oliver Yasuna Aug 27 '17 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.