从主控制器中调用方法

时间:2017-05-08 20:09:11

标签: java javafx imageview scenebuilder eventhandler

我正在尝试移动imageView。即使脉冲仍在将我的怪物移动到我的mannetje,我总是得到一个空指针异常。我认为问题是我制作了一个新的控制器。如何在不创建新控制器的情况下调用方法或修复空指针异常。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

public class main2 extends Application{

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

        primaryStage.setScene(scene);
        primaryStage.show();

        controller control = new controller();

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                case UP:    control.up(); break;
                case DOWN:  control.up(); break;
                case LEFT:  control.up(); break;
                case RIGHT: control.up(); break;
                }
            }
        });
        scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                    case UP:    control.up(); break;
                    case DOWN:  control.up(); break;
                    case LEFT:  control.up(); break;
                    case RIGHT: control.up(); break;
                }
            }
        });


}

public static void main(String[] args)  {

     launch(args);

}

这是我的主要

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
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.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.util.Duration;

public class controller{ 
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);
    }

}
}

public void newGame(){
    cont.setVisible(false);
    newGame.setVisible(false);
    pane.setStyle("-fx-background-image: url('map.jpg');");
    //System.out.println(mannetje);
    mannetje.setVisible(true);
    monster.setVisible(true);
    hitpoints.setVisible(true);
    //moveCircleOnKeyPress();
    System.out.println(mannetje);
    //boat.setImage(null);
    //boat.setStyle("-fx-text-fill: white;");
    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() ) ;

}

}

这是控制器类。

0 个答案:

没有答案