Ich bin ziemlich neu in JavaFX, und ich bin dabei, ein Spiel mit der Eclipse-IDE und JavaFX zu machen.Objekt Bewegung in JavaFX
Meine Frage ist, ich möchte meine "Eimer" mit den Pfeiltasten nach rechts und links bewegen, und ich verstehe, um dies zu tun, muss ich einen EventHandler und KeyListener verwenden.
Jedoch in meinen Versuchen, das zu implementieren, bin ich erfolglos gewesen, ich suche nach Zeigern darauf, wo ich falsch gehe und eine bessere Erklärung, wie man es benutzt. Ich benutze das Designmuster "Factory".
Es tut mir leid im Voraus für meinen chaotischen Code. Ich werde versuchen, es effizienter zu machen, wenn ich die Funktionalität sortiert habe.
Hier ist meine Hauptszene Code:
import java.util.ArrayList;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.*;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.*;
public class ScienceCatchApp extends Application {
Pane root;
Scene scene;
Canvas canvas;
GraphicsContext gc;
ArrayList<GameObject> list = new ArrayList<GameObject>();
Random rnd = new Random(System.currentTimeMillis());
int count = 0;
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
GameObject newObj = null;
GameObject objToRemove = null;
boolean isChemistryActive = false;
boolean isBiologyActive = false;
boolean isPhysicsActive = false;
for (GameObject obj : list) {
obj.update();
if (obj instanceof Chemistry) {
if (((Chemistry) obj).dropChemMark() && newObj == null) {
newObj = factory.createProduct("chemMark", obj.getX(), obj.getY());
}
}
if (obj instanceof Biology) {
if (((Biology) obj).dropBioMark() && newObj == null) {
newObj = factory.createProduct("bioMark", obj.getX(), obj.getY());
}
}
if (obj instanceof Physics) {
if (((Physics) obj).dropPhysMark() && newObj == null) {
newObj = factory.createProduct("physMark", obj.getX(), obj.getY());
}
}
if (obj.killObject())
objToRemove = obj;
if (obj instanceof Chemistry)
isChemistryActive = true;
if (obj instanceof Biology)
isBiologyActive = true;
if (obj instanceof Physics)
isPhysicsActive = true;
}
if (newObj != null)
list.add(newObj);
if (objToRemove != null)
list.remove(objToRemove);
if (!isChemistryActive && rnd.nextInt(10000) > 9900)
list.add(factory.createProduct("chemistry", 0, 0));
if (!isBiologyActive && rnd.nextInt(10000) > 9900)
list.add(factory.createProduct("biology", 0, 0));
else if (!isPhysicsActive && rnd.nextInt(10000) > 9900)
list.add(factory.createProduct("physics", 0, 0));
}
};
Factory factory;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
root = new Pane();
scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
canvas = new Canvas(800, 600);
gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
root.getChildren().add(canvas);
factory = new Factory(gc);
list.add(factory.createProduct("bucket", 0, 0));
timer.start();
EventHandler<KeyEvent> keyListener = new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT) {
//code for moving ship goes here?
}
event.consume();
}
};
}
}
Hier ist mein Objektcodes (ich habe Objekte ausschneiden, die, wie ich erwarten, dass sie zu arbeiten):
class GameObject {
protected Image img;
protected double x, y;
protected GraphicsContext gc;
public GameObject(GraphicsContext gc, double x, double y) {
this.gc = gc;
this.x = x;
this.y = y;
}
public void update() {
if (img != null)
gc.drawImage(img, x, y, 30, 30);
}
public void updateBucket() {
if (img != null)
gc.drawImage(img, x, y, 50, 50);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public boolean killObject() {
return false;
}
}
class Bucket extends GameObject {
public Bucket(GraphicsContext gc, double x, double y) {
super(gc, 0, 550);
img = new Image("res/Bucket.png");
update();
}
@Override
public void update() {
super.updateBucket();
}
}
class ChemMark extends GameObject {
public ChemMark(GraphicsContext gc, double x, double y) {
super(gc, x, y);
img = new Image("res/Chemistry.png");
update();
}
@Override
public void update() {
y += 1;
x += 1;
super.update();
}
public boolean killObject() {
if (y > 600 && x > 550)
return true;
else
return false;
}
}
Lesen Sie [Einführung in JavaFX für Spieleentwicklung] (https://gamedevelopment.utsplus.com/tutorials/introduction-to-javafx-for-game-development-cms-23835). Schauen Sie sich dann Beispiel 5 auf 'GitHub' an – Sedrick