0

I am trying to move my mannetje. It is an ImageView placed with a scenebuilder. It says it exists except when I press an arrowkey. I was wondering if there was a problem with my imports? How can I fix it?

package Game;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;





public class controller2 extends Application { 
public Button newGame;
public Button cont;
public AnchorPane pane;
public ImageView mannetje;
public Circle monster;
public  Scene scene;
public Image boat; 
public Label hitpoints;
Hero hero;
Monster monsterChar;
private Timeline timeline;
private int countdown = 15; 


public void initialize(){

 timeline = new Timeline(new KeyFrame(Duration.seconds(0.7),ob -> puls()));
  timeline.setCycleCount(15000);

}
private void puls() {

if(mannetje.getLayoutX()<monster.getLayoutX() && mannetje.getLayoutY() ==                       
monster.getLayoutY()){ // LEFT
    monster.setLayoutX(monster.getLayoutX()-10);

    if((mannetje.getLayoutX()+mannetje.getFitWidth()+(monster.getRadius()/2))>monster.getLayoutX()){
        monsterChar.hit(hero);
    }

}
if(mannetje.getLayoutX()<monster.getLayoutX() && mannetje.getLayoutY() < monster.getLayoutY()){ // LEFT-UP
    monster.setLayoutX(monster.getLayoutX()-10);
    monster.setLayoutY(monster.getLayoutY()-10);
}
if(mannetje.getLayoutX()==monster.getLayoutX() && mannetje.getLayoutY() < monster.getLayoutY()){ // DOWN
    monster.setLayoutY(monster.getLayoutY()+10);

    if((mannetje.getLayoutY()-mannetje.getFitHeight()-(monster.getRadius()/2))>monster.getLayoutY()){
        monsterChar.hit(hero);
    }

}
if(mannetje.getLayoutX()==monster.getLayoutX() && mannetje.getLayoutY() > monster.getLayoutY()){ // UP
    monster.setLayoutY(monster.getLayoutY()-10);

    if((mannetje.getLayoutY()+mannetje.getFitHeight()+(monster.getRadius()/2))>monster.getLayoutY()){
        monsterChar.hit(hero);
    }

}
if(mannetje.getLayoutX()>monster.getLayoutX() && mannetje.getLayoutY() > monster.getLayoutY()){ // RIGHT-UP
    monster.setLayoutX(monster.getLayoutX()+10);
    monster.setLayoutY(monster.getLayoutY()+10);
}

if(mannetje.getLayoutX()>monster.getLayoutX() && mannetje.getLayoutY() < monster.getLayoutY()){ // RIGHT-DOWN
    monster.setLayoutX(monster.getLayoutX()+10);
    monster.setLayoutY(monster.getLayoutY()-10);
}
if(mannetje.getLayoutX()<monster.getLayoutX() && mannetje.getLayoutY() > monster.getLayoutY()){ //LEFT-DOWN
    monster.setLayoutX(monster.getLayoutX()-10);
    monster.setLayoutY(monster.getLayoutY()+10);
}
if(mannetje.getLayoutX()>monster.getLayoutX() && mannetje.getLayoutY() == monster.getLayoutY()){ // RIGHT
    monster.setLayoutX(monster.getLayoutX()+10);

    if((mannetje.getLayoutX()-(mannetje.getFitWidth()/2))<monster.getLayoutX()){
        monsterChar.hit(hero);
    }
}}



@Override
public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("setup.fxml"));
      primaryStage.setTitle("TFOA");
        Scene scene = new Scene(root);
        System.out.println(mannetje);

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                case UP:    up(); break;
                case DOWN:  down(); break;
                case LEFT:  left(); break;
                case RIGHT: right(); break;
                }
            }
        });
        scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                    case UP:    up(); break;
                    case DOWN:  down(); break;
                    case LEFT:  left(); break;
                    case RIGHT: right(); break;
                }
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();


}

public static void main(String[] args)  {

     launch(args);


}

public void newGame(){
    cont.setVisible(false);
    newGame.setVisible(false);
    pane.setStyle("-fx-background-image: url('map.jpg');");
    mannetje.setVisible(true);
    monster.setVisible(true);
    hitpoints.setVisible(true);
    System.out.println(mannetje);
    hero = new Hero("Klud" , 1500);
    monsterChar = new Monster("Chak Al Ghui",50,7);
    hitpoints.setText(hero.getName() + ": " + hero.getEffectiveHitpoints()+"          "+monsterChar.getName()+":  "+ monsterChar.getEffectiveHitpoints() ) ;
    Weapon lition = new Weapon(2,"Lition");
    lition.setDamage(7);
    Armor faciate = new Armor(10,"Faciate");
    faciate.setEffectiveProtection(1);
    Backpack backpack = new Backpack(1, "Backpack",500);
    hero.equip(backpack, Anchor.back);
    hero.equip(lition, Anchor.rightHand);
    hero.equip(faciate, Anchor.body); 
    timeline.playFromStart();     
    mannetje.setLayoutY(200);
    monster.setLayoutY(200);
}

public void up(){
    mannetje.setLayoutY(mannetje.getLayoutY()-25);
}
public void left(){
    mannetje.setLayoutX(mannetje.getLayoutX()-25);
}
public void right(){
    mannetje.setLayoutX(mannetje.getLayoutX()+25);
}
public void down(){
    mannetje.setLayoutY(mannetje.getLayoutY()+25);
}
public void hit(){

    if((mannetje.getLayoutX()+mannetje.getFitWidth()+monster.getRadius())>monster.getLayoutX()){
    hero.hit(monsterChar);
}
    else{

    }


    hitpoints.setText(hero.getName() + ": " + hero.getEffectiveHitpoints()+"          "+monsterChar.getName()+":  "+ monsterChar.getEffectiveHitpoints() ) ;
}

}

Alan Maene
  • 11
  • 3
  • Doesn't this just throw null pointer exceptions? Can you post your controller class? – James_D May 08 '17 at 17:43
  • Seems like a duplicate of: [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – jewelsea May 08 '17 at 17:54
  • Possibly, though there's not really enough information to tell, a duplicate of http://stackoverflow.com/questions/32081713/javafx-controller-class-not-working – James_D May 08 '17 at 17:58
  • I edited it. I putted the main in my controller so i could reach the ImageView. I thought it would work. There is a circle monster that moves ever puls to my hero. The idea is that i can move mij hero around with the arrow keys. – Alan Maene May 08 '17 at 18:09
  • As in the question I linked above, the fields defined in the FXML are only initialized in the controller, not in the `Application` instance. You should create a separate class for the controller, and define methods on it for moving the image. Then call those methods from the event handlers on the scene. – James_D May 08 '17 at 18:56
  • thanks man i will try it – Alan Maene May 08 '17 at 19:18

0 Answers0