I have just recently started using Java, literally 2 days ago, and I have run into an issue. While I have been easily able to figure out most of the syntax and methods, because I have used multiple other languages, I was not able to solve this problem.
Essentially, I have placed a box (it aspires to be great things one day) on the screen, what I want this box to do, is when I click anywhere on the screen, he will move to that position. In other languages I would be able to do this fairly simply, but since I am not used to Java's particular method of doing things, it has become an issue.
Basically this is what I was trying to do:
Inside my Mouse Input class (extending MouseInputAdapter)
public void mousePressed(MouseEvent e) {
while(this.mouseIsPressed && playerObject != null){
double newX = e.getXOnScreen();
double newY = e.getYOnScreen();
double distX = newX - playerObject.getX();
double distY = newY - playerObject.getY();
double length = Math.sqrt(((distX * distX) + (distY * distY)));
if(length > 0){
playerObject.setVelocityX(distX/length);
playerObject.setVelocityY(distY/length);
}else{
playerObject.setVelocityX(0);
playerObject.setVelocityY(0);
}
}
}
}
this just seems to make the object go crazy and move, god knows where.
I tried something similar with the mousemove method, because I want the object to continually follow the mouse while the mouse is pressed.
I am sure I am missing a simple fix to this, and if someone can help me with this, that would be great. Maybe there is a more efficient method in Java (this is just the method i used in other languages). I am looking more for the steps to take, than the exact code.
EDIT
So this is what I have now... In my player class:
public void Update(){
if(mouseInput != null){
boolean mousePressed = mouseInput.checkIfPressed();
if(mousePressed){
double newX = mouseInput.showX();
double newY = mouseInput.showY();
double distX = newX - this.x;
double distY = newY - this.y;
double length = Math.sqrt(((distX * distX) + (distY * distY)));
if(length >= 1){
this.velocityX = distX/length;
this.velocityY = distY/length;
}else{
this.velocityX = 0;
this.velocityY = 0;
}
}
}
x += this.velocityX;
y += this.velocityY;
}
and in my Mouse Input Class:
public void mousePressed(MouseEvent e) {
mouseIsPressed = true;
System.out.println(mouseIsPressed);
this.e = e;
}
public void Update(){
if(mouseIsPressed){
X = this.e.getX();
Y = this.e.getY();
System.out.println(X);
System.out.println(Y);
}
}
public void mouseReleased(MouseEvent e){
mouseIsPressed = false;
this.e = null;
System.out.println(mouseIsPressed);
}
If there is another way to do this, it would be great, or a proper way to do this.
Thanks in advance
if(length >= 1)
this ? Also please tell us which framework you are using. Maybe Libgdx ? – dimitris93 Jun 17 '15 at 15:27if(length >= 1)
is not the logical condition you are looking for. If you had read my answer in the question I posted, you would have seen the "// if we passed the target" part, which is one way to check if you are going to pass the target on the next frame, so that you set the position of the object manually, straight on the target location you want to move it towards and then set the velocity to 0. – dimitris93 Jun 17 '15 at 19:22