所以我在我的简单游戏中实现了滑动机制,如果玩家按下向下箭头,则向右箭头,玩家将“滑动”直到向右箭头或向下箭头被释放。我一直试图让游戏变成这样:
玩家按下DOWN,然后按下右键 玩家角色(精灵)将“滑动”为“x(例如2)”秒 然后玩家必须按下DOWN,然后按下RIGHT KEY再次开始滑动
我的代码:
public void start(Stage primaryStage) throws Exception {
final Group root = new Group();
Scene scene = new Scene(root, 640, 480, Color.ALICEBLUE);
final Image runningninjap1 = new Image(getClass().getResourceAsStream("runningninja.png"));
final ImageView ninja1 = new ImageView(runningninjap1);
final Image runningninjaleftp1 = new Image(getClass().getResourceAsStream("runningninjaleft.png"));
Image tempground = new Image(getClass().getResourceAsStream("ground.png"));
Image tempground2 = new Image(getClass().getResourceAsStream("ground.png"));
final Image imgjumpingninja = new Image(getClass().getResourceAsStream("jumpingninja.png"));
final Image imgjumpingninjaleft = new Image(getClass().getResourceAsStream("jumpingninjaleft.png"));
final Image imgcrouchingninja = new Image(getClass().getResourceAsStream("crounchingninja.png"));
final Image imgcrouchingninjaleft = new Image(getClass().getResourceAsStream("crounchingninjaleft.png"));
final Image imgninjafalling = new Image(getClass().getResourceAsStream("fallingninja.png"));
final Image imgninjafallingleft = new Image(getClass().getResourceAsStream("fallingleft.png"));
final Image test = new Image(getClass().getResourceAsStream("nj.png"));
final Image imgninjaslidingright = new Image(getClass().getResourceAsStream("slidingninja_1.png"));
final Rectangle r = new Rectangle();
r.setX(rectx);
r.setY(recty);
r.setWidth(50);
r.setHeight(100);
r.setArcWidth(20);
r.setArcHeight(20);
Button button = new Button();
ninjarun1 = new ImageView(runningninjap1);
ninjarun1.setLayoutX(ninjax);
ninjarun1.setLayoutY(ninjay);
iview2 = new ImageView(tempground);
iview2.setLayoutX(backgroundx);
iview2.setLayoutY(backgroundy);
iview3 = new ImageView(tempground2);
iview3.setLayoutX(555);
iview3.setLayoutY(backgroundy);
iview4 = new ImageView(imgjumpingninja);
iview4.setLayoutX(aix);
iview4.setLayoutY(aiy);
final Group ninja;
ninja = new Group(ninja1);
root.getChildren().addAll(iview2, iview3, button, ninjarun1);
primaryStage.setTitle("A basic window");
primaryStage.setScene(scene);
primaryStage.show();
//keypressed
button.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.LEFT) {
directionleft = true;
dashleft = true;
ninjax = ninjax - 10;
ninjarun1.setLayoutX(ninjax);
}
if (event.getCode() == KeyCode.DOWN) {
crouching = true;
}
if (event.getCode() == KeyCode.UP) {
if (ninjay > 340) {
jumpforce = -15;
}
ninjarun1.setLayoutY(ninjay);
}
if (event.getCode() == KeyCode.RIGHT) {
ninjax = ninjax + 10;
directionleft = false;
runningframes = runningframes + 1;
}
if (event.getCode() == KeyCode.RIGHT && crouching == true) {
sliding = true;
} else {
sliding = false;
}
if (runningframes > 0) {
rightcycle = true;
}
ninjarun1.setLayoutY(ninjax);
if (runningframes > 16) {
runningframes = 0;
}
}
});
//keyreleased
button.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.DOWN) {
crouching = false;
}
if (event.getCode() == KeyCode.RIGHT) {
sliding = false;
}
}
});
//physics engine
new AnimationTimer() {
@Override
public void handle(long now) {
if (ninjarun1.getBoundsInParent().intersects(iview2.getBoundsInParent())) {
groundforce = -gravity;
} else {
groundforce = 0;
}
if (jumpforce < 0) {
ninjay = ninjay - (-gravity) + jumpforce + groundforce;
if (directionleft == true) {
ninjax = ninjax - 3;
} else if (directionleft == false) {
ninjax = ninjax + 3;
}
jumpforce = jumpforce + 1;
jumping = true;
} else if ((ninjay < 340)) {
ninjay = ninjay - (gravity) + jumpforce + groundforce;
if (directionleft == true) {
ninjax = ninjax - 3;
} else if (directionleft == false) {
ninjax = ninjax + 3;
}
falling = true;
} else {
jumping = false;
}
ninjarun1.setLayoutY(ninjay);
ninjarun1.setLayoutX(ninjax);
//directionleft
if (directionleft == true) {
ninjarun1.setImage(runningninjaleftp1);
//leftjumping
if ((jumping == true)) {
ninjarun1.setImage(imgjumpingninjaleft);
if (jumpforce == 0) {
ninjarun1.setImage(imgninjafallingleft);
}
} //leftrunning
else if ((jumping == false)) {
ninjarun1.setImage(runningninjaleftp1);
//leftcrouching
if (crouching == true) {
ninjarun1.setImage(imgcrouchingninjaleft);
} else if (crouching == false) {
ninjarun1.setImage(runningninjaleftp1);
}
}
} //directionright
else if (directionleft == false) {
//rightjumping
if ((jumping == true)) {
ninjarun1.setImage(imgjumpingninja);
if (jumpforce == 0) {
ninjarun1.setImage(imgninjafalling);
}
} //rightrunning
else if ((jumping == false)) {
//rightcouching
if (crouching == true) {
ninjarun1.setImage(imgcrouchingninja);
if (sliding == true) {
ninjarun1.setImage(imgninjaslidingright);
ninjax = ninjax + 10;
slidingtime = slidingtime - 1;
System.out.println("ninjax: " + ninjax);
}
}
if ((crouching == false)) {
ninjarun1.setImage(runningninjap1);
}
}
}
}
}.start();
}