Last active
January 21, 2023 09:45
-
-
Save Da9el00/421d6f02d52093ac07a9e65b99241bf8 to your computer and use it in GitHub Desktop.
Moving Spites in JavaFX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.image.Image?> | |
<?import javafx.scene.image.ImageView?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<AnchorPane fx:id="scene" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.movingsprites.HelloController"> | |
<children> | |
<ImageView fx:id="runner" fitHeight="100.0" fitWidth="100.0" layoutX="58.0" layoutY="150.0" pickOnBounds="true" preserveRatio="true"> | |
<image> | |
<Image url="@sprites/runner1.png" /> | |
</image> | |
</ImageView> | |
</children> | |
</AnchorPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.animation.AnimationTimer; | |
import javafx.beans.binding.BooleanBinding; | |
import javafx.beans.property.BooleanProperty; | |
import javafx.beans.property.SimpleBooleanProperty; | |
import javafx.fxml.FXML; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.input.KeyCode; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.shape.Rectangle; | |
public class MovementController { | |
private BooleanProperty wPressed = new SimpleBooleanProperty(); | |
private BooleanProperty aPressed = new SimpleBooleanProperty(); | |
private BooleanProperty sPressed = new SimpleBooleanProperty(); | |
private BooleanProperty dPressed = new SimpleBooleanProperty(); | |
private BooleanBinding keyPressed = wPressed.or(aPressed).or(sPressed).or(dPressed); | |
private int movementVariable = 2; | |
@FXML | |
private ImageView sprite; | |
@FXML | |
private AnchorPane scene; | |
public void makeMovable(ImageView sprite, AnchorPane scene){ | |
this.sprite = sprite; | |
this.scene = scene; | |
movementSetup(); | |
keyPressed.addListener(((observableValue, aBoolean, t1) -> { | |
if(!aBoolean){ | |
timer.start(); | |
} else { | |
timer.stop(); | |
} | |
})); | |
} | |
AnimationTimer timer = new AnimationTimer() { | |
@Override | |
public void handle(long timestamp) { | |
if(wPressed.get()) { | |
sprite.setLayoutY(sprite.getLayoutY() - movementVariable); | |
} | |
if(sPressed.get()){ | |
sprite.setLayoutY(sprite.getLayoutY() + movementVariable); | |
} | |
if(aPressed.get()){ | |
sprite.setLayoutX(sprite.getLayoutX() - movementVariable); | |
} | |
if(dPressed.get()){ | |
sprite.setLayoutX(sprite.getLayoutX() + movementVariable); | |
} | |
} | |
}; | |
private void movementSetup(){ | |
scene.setOnKeyPressed(e -> { | |
if(e.getCode() == KeyCode.W) { | |
wPressed.set(true); | |
} | |
if(e.getCode() == KeyCode.A) { | |
aPressed.set(true); | |
} | |
if(e.getCode() == KeyCode.S) { | |
sPressed.set(true); | |
} | |
if(e.getCode() == KeyCode.D) { | |
dPressed.set(true); | |
} | |
}); | |
scene.setOnKeyReleased(e ->{ | |
if(e.getCode() == KeyCode.W) { | |
wPressed.set(false); | |
} | |
if(e.getCode() == KeyCode.A) { | |
aPressed.set(false); | |
} | |
if(e.getCode() == KeyCode.S) { | |
sPressed.set(false); | |
} | |
if(e.getCode() == KeyCode.D) { | |
dPressed.set(false); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment