[solved]getX()

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.

What does it return then? I.e. What are you expecting, and what are you getting? It must return some value.

Are you ever running that key event? And the move method? What is X printing out? We need more information if you want help.

I have this code for my tower`s attack

if (towerattack.x == character.x)
            System.out.print(character.x)

Then it returns the assigned variable or the starting position of the image when I will play it.

x = 250;

Character.x is private. Hence it can’t be seen from other classes. Change it to:

if(towerattack.x == character.getX())

Was that your problem and did that help?

Sorry for my poor explanations

This is my tower`s attack movement.

 public void move() {
        character = new Character();       
        
        x -= dx;
        if (getY() <= character.getY() + character.getHeight()/2)
            y += dy;
        if (getX() == character.getX()) {
            System.out.println(character.getX());
            x = posX;
            y = posY;
        }
    }

new Character() :point:

What is the problem with the new Character()?

Roquen means every time your tower moves, you create a new Character. Hence resetting character’s position to (250, 270). Is that the problem?? I’m kinda lost, sorry.

I think there is no problem with the new Character() sir.

This is what “move” does:

 public void move() {
        x -= dx;
        if (getY() <= 270 +50/2)
            y += dy;
        if (getX() == 250) {
            System.out.println(250);
            x = posX;
            y = posY;
        }
    }

Is that really what you mean to do?

Nope.

I will move my character L or R.

If my character will go inside the attack perimeter of the tower, it will attack my character.

This is the LOGIC of the tower`s attack

if HIT
           x = starting_position_X;
           y = starting_position_Y;

where:

HIT = false
         if attack.x == character.x
            HIT = true;

OR

HIT = false
         if attack.getBounds().intersects.getBounds()
            HIT = true;

But my characters X is always 250. And I dont know why.

P.S. This is not the real code. It is only the ALGORITHM or LOGIC.

I think Roquen just told you why. Every time move() is called, you set the character variable to a new Character(), before you run your logic. In your Character constructor, you set x to 250. Hence x will always be 250 when you call move(). Always. Remove the character = new Character() line and see if it does what you want.

I already move it and tried it 3x

inside the move()
inside the public TowerAttack()
inside the Class TowerAttack()

Nothing changed.

charR = new ImageIcon(Character.class.getResource(“charR.jpg”)).getImage();
charL = new ImageIcon(Character.class.getResource(“charL.jpg”)).getImage();

why dont you just rotate the image in code , what lib/api are you using?
just asking =D ;D

One of two things is happening then:

  1. You are prematurely resetting x to 250, possibly by creating a new Character, prior to your logic, as we have already explored.

  2. The character is never moving from the initial 250. In this case, make sure that your keyPressed() method is being called. Have you registered the listener in the required places? Shove some println’s in your keyPressed method and make sure it is being called when expected.

PS. Slowly go through your code and follow the logic; you may well find the problem. Alternatively paste all of the code in the pastebin, and I’m sure someone will find it.

what do you mean “rotate the image”?

I am using java netbeans.

All is working with keyPressed()

because if it doesnt my character will not move, right?

Okay, so your character is moving on screen? Clearly your keyPressed method is working, and the characters x variable is correct, as you are using this to draw the character on screen.

This leads me to believe that your ‘tower’ is not referencing the same Character that is drawn on screen and receiving the KeyEvents. Given that you had a bit of an ‘out of place’ new Character() your code, I have a sneaking suspicion you are referencing a new Character at the default position of 250, which is never updated. Make sure your tower is referencing the same Character object that you are updating and rendering on-screen.

We seem to be going around in circles a bit. I think if you need any further help than what has been given, you’ll have to show us some more code. As mentioned, you can put it all in the pastebin if required.

I already solved it. Thanks.