mardi 31 mai 2016

Processing click on node as pane

first time poster here!

I'm trying to create a game that has 2 super classes, 1 for good foods and 1 for bad (even though only 1 is present). To test this, I'm printing what the mouse is selecting into the console. I'm having the issue that it is only seeing the pane and never the node. I've been stuck on this for a long time, so help would be greatly appreciated.

Code is below:

    import java.util.ArrayList;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.ImageCursor;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;


public class DentistLearningApp extends Application {
    FlowPane root;
    Pane game;
    Scene scene;
    Factory f = new Factory();
    int count=0;
    ArrayList<Sweets> sweets = new ArrayList<Sweets>();
    //Image cur = new Image("/res/x.png");


    EventHandler<MouseEvent> eh = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {



            event.getSceneX();
            event.getSceneY();

            Boolean correct = false;



            if(correct==true) {
                System.out.println("crash");
                correct=false;
            }

            if(event.getSource() instanceof Pane) {
                System.out.println("pane");
                return;
            }


            for(Sweets s : sweets) { 
                if(((Node) event.getSource()).getBoundsInParent().intersects(s.getBoundsInParent())) {
                correct=true;
                System.out.println("????");
                }
            }




        }

    };

     EventHandler<MouseEvent> click = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {

        }

    };

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long arg0) {

            //scene.setCursor(new ImageCursor(cur));


            count++;
            if(count<2)
                return;
            for(Sweets invader : sweets)
                invader.move();
            count=0;

        } 
    };



    public static void main(String[] args) {
        launch(args);

    }

    @Override
    public void start(Stage stage) throws Exception {


        root = new FlowPane();

        sweets.add(f.createProduct("Nice1", 10, 10));
        sweets.add(f.createProduct("Nice2", 100, 110));
        sweets.add(f.createProduct("Nice3", 400, 210));
        sweets.add(f.createProduct("Nice4", 200, 310));

        game = new Pane();
        game.setPrefSize(800,600);
        game.setStyle("-fx-background-color: #fffff0");
        scene = new Scene(root, 800, 600);


        //game.getChildren().add(testInvader);
        game.getChildren().addAll(sweets);

        timer.start();

        /*P1 P1 = new P1(0, 0, game);

        players.add(P1);

        game.getChildren().add(P1);*/
        game.setOnMouseClicked(eh);

        stage.setScene(scene);
        stage.show();
        root.getChildren().addAll(game);

    }

}

abstract class Sweets extends ImageView {



    Sweets(String imageFile, double x, double y)
    {
        this.setX(x);
        this.setY(y);
        this.setImage(new Image(DentistLearningApp.class.getResource("res/"+imageFile).toExternalForm()));
    }
    void update() {

    }
    public abstract void move();
}

class Cake extends Sweets {

    int dx=5,dy=4;

    Cake(String imageFile, double x, double y) {
        super(imageFile, x, y);
    }


    @Override
    public void move() {
        this.setX(this.getX()+dx);

        if(this.getX()>749 || this.getX()<-20) {
            dx=-dx;
        }

        this.setY(this.getY()+dy);

        if(this.getY()>530 || this.getY()<0) {
            dy=-dy;
        }


    }

}


class Chocolate extends Sweets {

    int dx=5,dy=-2;

    Chocolate(String imageFile, double x, double y) {
        super(imageFile, x, y);
    }

    @Override
    public void move() {
        this.setX(this.getX()+dx);

        if(this.getX()>749 || this.getX()<-20) {
            dx=-dx;
        }

        this.setY(this.getY()+dy);

        if(this.getY()>530 || this.getY()<0) {
            dy=-dy;
        }

    }
}

class Donut extends Sweets {

    int dx=1,dy=3;

    Donut(String imageFile, double x, double y) {
        super(imageFile, x, y);
    }

    @Override
    public void move() {
        this.setX(this.getX()+dx);

        if(this.getX()>749 || this.getX()<-20) {
            dx=-dx;
        }

        this.setY(this.getY()+dy);

        if(this.getY()>530 || this.getY()<0) {
            dy=-dy;
        }

    }
}

class Drink extends Sweets {

    int dx=8,dy=5;

    Drink(String imageFile, double x, double y) {
        super(imageFile, x, y);
    }

    @Override
    public void move() {

        this.setX(this.getX()+dx);

        if(this.getX()>749 || this.getX()<-20) {
            dx=-dx;
        }

        this.setY(this.getY()+dy);

        if(this.getY()>530 || this.getY()<0) {
            dy=-dy;
        }

    }

}



interface SweetFactory
{
    Sweets createProduct();

}
class Factory implements SweetFactory
{
    Sweets createProduct(String discrim, double x, double y)
    {
        if(discrim.equals("Nice1"))
            return(new Cake("cake.jpg",x,y));
        else if(discrim.equals("Nice2"))
            return(new Chocolate("chocolate.jpg",x,y));
        else if(discrim.equals("Nice3"))
            return(new Donut("donut.png",x,y));
        else if(discrim.equals("Nice4"))
            return(new Drink("drink.jpg",x,y));
        else
            return(null);
    }
    @Override
    public Sweets createProduct() {
        return null;
    }

Aucun commentaire:

Enregistrer un commentaire