问题
I am creating a game in which you use guns and play against the computer/someone else. You can orient that gun with a slider. But I don't manage to orient the muzzle of the gun. I tried in different ways, nothing worked as expected. I googled a bit but I found nothing that worked and nothing that seems to have a chance of working. So I ask you, what haven't I tried yet ? What should I do different ?
Here is a picture explaining what I said :
Expectations
Here is the concerned code :
package canons;
import javafx.scene.Parent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
public class Canon extends Parent {
private int posX = 500; // x position of the gun
private int posY = 250; // y position of the gun
private int tailleCorps = 20; // size of the body
private Rectangle buse; // muzzle
private Circle corps; // body
public Canon(Color couleurCorps, Color couleurBuse) {
this.getChildren().addAll(getBuse(), getCorps());
this.setTranslateX(posX);
this.setTranslateY(posY);
}
public Rectangle getBuse() {
if(buse == null) { // verify if they aren't created yet. Security.
buse = new Rectangle();
buse.setWidth(70);
buse.setHeight(20);
buse.setArcWidth(15); // round extremities
buse.setArcHeight(30);
buse.setFill(Color.BLACK);
buse.setTranslateX(posX);
buse.setTranslateY(posY + tailleCorps/4);
}
return buse;
}
public Circle getCorps() {
if(corps == null) { // verify if they aren't created yet. Security.
corps = new Circle(tailleCorps);
corps.setTranslateX(posX);
corps.setTranslateY(posY);
}
return corps;
}
}
If I haven't been clear enough, please feel free to ask me any other questions on my problem/code. Thanks.
EDIT:
Using buse.getTransforms.add(new Rotate(angle, x, y));
EDIT 2:
I still didn't find the way to make it work... I've post an answer where I show what I've now. Thanks.
回答1:
I've found another solution, and it is for the moment the best I've found. It's not what I want but it is whats look like it the most.
So this is the code now :
package canons;
import javafx.scene.Parent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
public class Canon extends Parent {
private int posX = 500, posY = 250;
private double valeurAngle;
private Rectangle buse;
private Circle corps, obus;
public Canon(Color couleurCorps, Color couleurBuse) {
this.getChildren().addAll(getBuse(), getCorps());
this.setTranslateX(posX);
this.setTranslateY(posY);
}
public Rectangle getBuse() {
if(buse == null) { // sécurité, évite de recréer plusieurs fois le même objet
buse = new Rectangle();
buse.setWidth(60);
buse.setHeight(20);
buse.setArcWidth(15); // extrémités arrondies
buse.setArcHeight(30);
buse.setFill(Color.BLACK);
buse.setTranslateX(posX);
buse.setTranslateY(posY - buse.getHeight() / 2);
}
return buse;
}
public Circle getCorps() {
if(corps == null) { // sécurité, évite de recréer plusieurs fois le même
corps = new Circle(20);
corps.setTranslateX(posX);
corps.setTranslateY(posY);
}
return corps;
}
public void orienter(double angle) {
Rotate rotate = new Rotate();
rotate.setPivotX(buse.getX()); //Pivot X Top-Left corner
rotate.setPivotY(buse.getY()); //Pivot Y
rotate.setAngle(angle); //Angle degrees
buse.getTransforms().add(rotate);
valeurAngle = angle;
}
And this is what I have :
First exemple
Second exemple
Third exemple
Fourth exemple
Another one
Last one
I hope you now have a better idea of what I mean. Thanks.
来源:https://stackoverflow.com/questions/53178512/how-to-orient-the-muzzle-of-a-gun-javafx