Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.animation.AnimationTimer;
- import javafx.application.Application;
- import javafx.geometry.Rectangle2D;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.KeyCode;
- import javafx.scene.input.KeyEvent;
- import javafx.stage.Stage;
- public class Esercitazione extends Application{
- Group gPersonaggio= new Group();
- Group gSfondo= new Group();
- Group gPadre=new Group();
- ImageView iSfondo= new ImageView("https://previews.123rf.com/images/lianella/lianella1412/lianella141200024/34318523-Sfondo-Foresta-Gioco-per-l-applicazione-del-gioco-2d-Disegno-vettoriale-Piastrellabile-in-orizzontal-Archivio-Fotografico.jpg");
- ImageView iPersonaggio= new ImageView("http://www.sibeg.it/pics/976_r_900_550.jpg");
- AnimationTimer sposta;
- int iContatore=0;
- double posX=0;
- public void start(Stage finestra){
- gPadre.getChildren().addAll(gPersonaggio,gSfondo);
- gSfondo.getChildren().add(iSfondo);
- gPersonaggio.getChildren().add(iPersonaggio);
- Rectangle2D rettangolo= new Rectangle2D(posX,0,500,500);
- iSfondo.setViewport(rettangolo);
- Scene scena=new Scene(gPadre,500,500);
- finestra.setScene(scena);
- finestra.setTitle("esercitazione");
- finestra.show();
- scena.setOnKeyPressed(e-> tastoPremuto(e));
- }
- public void tastoPremuto(KeyEvent e){
- if(e.getCode()== KeyCode.UP){
- sposta= new AnimationTimer(){
- public void handle(long ora){
- su();
- }
- };
- sposta.start();
- }
- if(e.getCode()==KeyCode.DOWN){
- sposta = new AnimationTimer(){
- public void handle(long ora){
- giu();
- }
- };
- sposta.start();
- }
- if(e.getCode()==KeyCode.SPACE){
- sposta= new AnimationTimer(){
- public void handle(long ora){
- scorrimento();
- }
- };
- sposta.start();
- }
- }
- public void su(){
- double posY= gPersonaggio.getTranslateY();
- posY -= 2.0;
- gPersonaggio.setTranslateY(posY);
- iContatore++;
- if(iContatore==5){
- sposta.stop();
- iContatore=0;
- }
- }
- public void giu(){
- double posY= gPersonaggio.getTranslateY();
- posY += 2.0;
- gPersonaggio.setTranslateY(posY);
- iContatore++;
- if(iContatore==5){
- sposta.stop();
- iContatore=0;
- }
- }
- public void scorrimento(){
- posX += 3.0;
- Rectangle2D rettangolo= new Rectangle2D(posX,0,500,500);
- iSfondo.setViewport(rettangolo);
- if(posX >= 1499){
- sposta.stop();
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement