简体   繁体   中英

Java fx 2D scrolling game

I want to create a 2D game using java fx but i have an issue trying to make scrolling works when the mouse approach the edge of the screen because it works for when the mouse enter the border i defined but when i try to loop that process to make it work until the mouse comes out of the border it just crash the app. The scrolling system i want to en up with is more or less like in strategy games like warcraft or Empire earth etc... here is the code i work with:

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); }  
}

I hope someone can help me with it thanks !

here is the part i try to loop:

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);
}

i tried a while loop but i simply get the app crashing without any java exception on console it simply stopped working.

I too tried your code and it seemed to run fine. You could try putting it into a new file and rerunning it to "reset the file paths". Interesting that you got no stack trace. That tells me it could be a memory leak problem with your loop. If you are using NetBeans, you could try installing NetBeans: StatusLine Memory Usage to see if your while loop is running infinitely. Alternatively you could output some text every loop to see how many times it is running through.

If it does end up being a memory problem with your loop, try to create a set of circumstances where it only enters the loop when an action is performed, and then exits after an action is performed. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM