Java fx 2D滚动游戏

时间:2017-06-28 08:12:47

标签: java javafx scroll mouse

我想用java fx创建一个2D游戏但我有一个问题,当鼠标接近屏幕边缘时尝试滚动工作,因为它适用于鼠标进入我定义的边界但当我尝试循环该过程使其工作,直到鼠标离开边界它只是崩溃应用程序。 我想要的滚动系统或多或少像战术游戏或帝国地球等战略游戏...... 这是我使用的代码:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javax.swing.JScrollPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/** Constructs a scene with a pannable Map background. */
public class PannableView extends Application {
 private Image backgroundImage;

@Override public void init() {
backgroundImage = new Image("http://www.narniaweb.com/wp-content/uploads/2009/08/NarniaMap.jpg");
 }

@Override public void start(Stage stage) {
stage.setTitle("Drag the mouse to pan the map");

// construct the scene contents over a stacked background.
StackPane layout = new StackPane();
layout.getChildren().setAll(
  new ImageView(backgroundImage),
  createKillButton()
);

// wrap the scene contents in a pannable scroll pane.
ScrollPane scroll = createScrollPane(layout);

// show the scene.
Scene scene = new Scene(scroll);
stage.setScene(scene);
stage.show();
scroll.setOnMouseMoved(e->{
    /*
     * screen movement
     * if cursor close to border:
     *  detect wich border and move in concequences
     */

int marge = 10;

    //TODO make camera move
    //left top corner
    if(marge > e.getX() && marge > e.getY()){
        scroll.setVvalue(scroll.getVvalue() -0.001);
        scroll.setHvalue(scroll.getHvalue() -0.001);
    }//left bottom corner
    else if(marge > e.getX() && scroll.getHeight()-marge < e.getY()){
        scroll.setVvalue(scroll.getVvalue() +0.001);
        scroll.setHvalue(scroll.getHvalue() -0.001);
    }//right top corner
    else if(scroll.getWidth()-marge < e.getX() && marge > e.getY()){
        scroll.setVvalue(scroll.getVvalue() -0.001);
        scroll.setHvalue(scroll.getHvalue() +0.001);
    }//right bottom corner
    else if(scroll.getWidth()-marge < e.getX() && scroll.getHeight()-marge < e.getY() ){
        scroll.setVvalue(scroll.getVvalue() +0.001);
        scroll.setHvalue(scroll.getHvalue() +0.001);
    }//top
    else if(marge > e.getY()){
        scroll.setVvalue(scroll.getVvalue() -0.001);
    }//right
    else if(scroll.getWidth()-marge < e.getX()){
        scroll.setHvalue(scroll.getHvalue() +0.001);
    }//bottom
    else if(scroll.getHeight()-marge < e.getY()){
        scroll.setVvalue(scroll.getVvalue() +0.001);
    }//left
    else if(marge > e.getX()){
        scroll.setHvalue(scroll.getHvalue() -0.001);
    }


});
// bind the preferred size of the scroll area to the size of the scene.
scroll.prefWidthProperty().bind(scene.widthProperty());
scroll.prefHeightProperty().bind(scene.widthProperty());

// center the scroll contents.
scroll.setHvalue(scroll.getHmin() + (scroll.getHmax() - scroll.getHmin()) / 2);
scroll.setVvalue(scroll.getVmin() + (scroll.getVmax() - scroll.getVmin()) / 2);
}

/** @return a control to place on the scene. */
private Button createKillButton() {
final Button killButton = new Button("Kill the evil witch");
killButton.setStyle("-fx-base: firebrick;");
killButton.setTranslateX(65);
killButton.setTranslateY(-130);
killButton.setOnAction(new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent t) {
    killButton.setStyle("-fx-base: forestgreen;");
    killButton.setText("Ding-Dong! The Witch is Dead");
  }
});
return killButton;
}

/** @return a ScrollPane which scrolls the layout. */
private ScrollPane createScrollPane(Pane layout) {
ScrollPane scroll = new ScrollPane();
scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scroll.setPannable(true);
scroll.setPrefSize(800, 600);
scroll.setContent(layout);
return scroll;
}


public static void main(String[] args) { launch(args); }  
}

我希望有人可以帮助我,谢谢!

这是我尝试循环的部分:

if(marge > e.getX() && marge > e.getY()){
    scroll.setVvalue(scroll.getVvalue() -0.001);
    scroll.setHvalue(scroll.getHvalue() -0.001);
}//left bottom corner
else if(marge > e.getX() && scroll.getHeight()-marge < e.getY()){
    scroll.setVvalue(scroll.getVvalue() +0.001);
    scroll.setHvalue(scroll.getHvalue() -0.001);
}//right top corner
else if(scroll.getWidth()-marge < e.getX() && marge > e.getY()){
    scroll.setVvalue(scroll.getVvalue() -0.001);
    scroll.setHvalue(scroll.getHvalue() +0.001);
}//right bottom corner
else if(scroll.getWidth()-marge < e.getX() && scroll.getHeight()-marge < e.getY() ){
    scroll.setVvalue(scroll.getVvalue() +0.001);
    scroll.setHvalue(scroll.getHvalue() +0.001);
}//top
else if(marge > e.getY()){
    scroll.setVvalue(scroll.getVvalue() -0.001);
}//right
else if(scroll.getWidth()-marge < e.getX()){
    scroll.setHvalue(scroll.getHvalue() +0.001);
}//bottom
else if(scroll.getHeight()-marge < e.getY()){
    scroll.setVvalue(scroll.getVvalue() +0.001);
}//left
else if(marge > e.getX()){
    scroll.setHvalue(scroll.getHvalue() -0.001);
}

我尝试了一个while循环,但我只是让应用程序在控制台上没有任何java异常而崩溃,它只是停止工作。

1 个答案:

答案 0 :(得分:0)

我也尝试了你的代码,它似乎运行良好。您可以尝试将其放入新文件并重新运行以“重置文件路径”。有趣的是你没有堆栈跟踪。这告诉我,你的循环可能是内存泄漏问题。如果您使用的是NetBeans,则可以尝试安装NetBeans:StatusLine Memory Usage以查看您的while循环是否无限运行。或者,您可以在每个循环中输出一些文本,以查看它运行的次数。

如果它最终成为循环的内存问题,请尝试创建一组环境,它只在执行操作时进入循环,然后在执行操作后退出。希望这可以帮助。