Created
July 21, 2022 10:28
-
-
Save Da9el00/7f46d7bc4fd87feb134199cb0ba6a157 to your computer and use it in GitHub Desktop.
JavaFX Animating sprites
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; | |
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; | |
private RunningAnimation runningAnimation; | |
@FXML | |
private ImageView sprite; | |
@FXML | |
private AnchorPane scene; | |
public void makeRunnerMovable(ImageView sprite, AnchorPane scene){ | |
this.sprite = sprite; | |
this.scene = scene; | |
movementSetup(); | |
runningAnimation = new RunningAnimation(sprite); | |
keyPressed.addListener(((observableValue, aBoolean, t1) -> { | |
if(!aBoolean){ | |
timer.start(); | |
runningAnimation.startAnimation(); | |
} else { | |
timer.stop(); | |
runningAnimation.stopAnimation(); | |
} | |
})); | |
} | |
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); | |
} | |
}); | |
} | |
} |
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.Animation; | |
import javafx.animation.KeyFrame; | |
import javafx.animation.Timeline; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.util.Duration; | |
import java.io.File; | |
public class RunningAnimation { | |
private ImageView runner; | |
int number = 1; | |
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> { | |
if(number == 1){ | |
runner.setImage(new Image(getFile("sprites/runner2.png").getAbsolutePath())); | |
number = 2; | |
} else if( number == 2){ | |
runner.setImage(new Image(getFile("sprites/runner1.png").getAbsolutePath())); | |
number = 1; | |
} | |
})); | |
public RunningAnimation(ImageView runner) { | |
this.runner = runner; | |
timeline.setCycleCount(Animation.INDEFINITE); | |
} | |
private File getFile(String fileName){ | |
return new File(getClass().getResource(fileName).getPath()); | |
} | |
public void startAnimation(){ | |
timeline.play(); | |
} | |
public void stopAnimation(){ | |
timeline.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment