I doesn`t return the X-position of my character. What is the problem?
This is my code.
public class Character {
private TowerAttack towerattack;
private Image charR;
private Image charL;
private int x, y;
private int width, height;
private int dx;
private int faceL, faceR;
private final int CHAR_SPEED = 5;
public Character() {
towerattack = new TowerAttack();
getImages();
height = 50;
width = 20;
faceL = 0;
faceR = 0;
x = 250;
y = 270;
}
public void drawChar(Graphics2D g2d) {
if (faceL == 1)
drawCharL(g2d);
else
drawCharR(g2d);
}
public void drawCharR(Graphics2D g2d) {
g2d.drawImage(charR, getX(), getY(), null);
}
public void drawCharL(Graphics2D g2d) {
g2d.drawImage(charL, getX(), getY(), null);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void getImages() {
charR = new ImageIcon(Character.class.getResource("charR.jpg")).getImage();
charL = new ImageIcon(Character.class.getResource("charL.jpg")).getImage();
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void move() {
x += dx;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -CHAR_SPEED;
faceL = 1;
faceR = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = CHAR_SPEED;
faceR = 1;
faceL = 0;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
dx = 0;
if (key == KeyEvent.VK_RIGHT)
dx = 0;
}
}
I hope you can help me.
I want to get the X-position of my character.
SOLUTION:
Put your attackMove() in your Panel.