Cześć, proszę o pomoc!! Próbuje kontynuować projekt w JavaFX utknąłem na kolejnej rzeczy, próbuje dodać zjawisko kolizji do moich obiektów, kod się oczywiście kompiluje ale efektów brak, teraz po zmianach to już w ogóle żadna kolizja nie występuje, wcześniej udawało mi się ją wywołać, ale "kwadrat kolizji" był zdecydowani w niewłaściwym miejscu, próbowałem zmienić jego pozycje itd itp, , może ktoś rzucić okiem i powie mi gdzie robię błąd? mam ogólnie 4 klasy zaangażowane w to wszystko Creature, Game Controler, Rectangle i Vectror, reszta programu odnosi się do innej częsci działania
wstawiam wszystki klasy tutaj, ale ponieważ program wykorzystuje jeszcze image, w klasie Game Controler występują do tworzenia obiektów, żeby się nie rozdrabniać dodaje od razu cały "projekt" zzipowany...
plik FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="borderpane" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.rpgfxmaven.GameControler">
<children>
<Canvas fx:id="canvaspane" height="600" width="600" />
</children>
</BorderPane>
Klasa Game Controler
package com.example.rpgfxmaven;
import java.util.ArrayList;
import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class GameControler {
public static int oryginalnyRozmiarLudzika = 16; // 16x16 tile
public static int skala = 3;
public static int rozmiarLudzika = skala * oryginalnyRozmiarLudzika;
final int dlugoscPlanszy = 20;
final int szerokoscPlanszy = 15;
final int dlugoscEkranu = rozmiarLudzika*dlugoscPlanszy;
final int szerokoscEkranu = rozmiarLudzika*szerokoscPlanszy;
@FXML
BorderPane borderpane;
@FXML
Canvas canvaspane;
public static MediaPlayer mediaPlayer;
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("maingame-window.fxml"));
borderpane = loader.load();
Scene scene = new Scene(borderpane);
stage.setScene(scene);
//ustawienia canvasa
canvaspane = new Canvas(dlugoscEkranu, szerokoscEkranu);
GraphicsContext content = canvaspane.getGraphicsContext2D();
borderpane.setCenter(canvaspane);
content.setFill(Color.GREEN);
content.fillRect(0,0,dlugoscEkranu,szerokoscEkranu);
//tworzymy bohaterq i przeciwników
String css_monster = getClass().getResource("media/test2.png").toString();
String css_castle = getClass().getResource("media/test3.png").toString();
String css_hero = getClass().getResource("media/test.png").toString();
Towns castle = new Towns();
castle.position.set(300, 200);
castle.setImage(css_castle);
castle.draw(content);
Creature hero = new Creature();
hero.position.set(500, 500);
hero.setImage(css_hero);
hero.render(content);
ArrayList<Creature> skeletons = new ArrayList<Creature>();
int skeletonsCounter = 5;
for (int n=0; n<skeletonsCounter; n++)
{
Creature skeleton = new Creature();
double x = (Math.random()*121)+100;
double y = (Math.random()*264)+231;
skeleton.position.set(x,y);
skeleton.setImage(css_monster);
skeletons.add(skeleton);
}
//lista do której będziemy wrzucać który przycisk przycisneliśmy
ArrayList<String> inputList = new ArrayList<String>();
//eventy przycisków
scene.setOnKeyPressed(
(KeyEvent event) ->
{
String keyName= event.getCode().toString();
System.out.println(keyName);
if(!inputList.contains(keyName))
inputList.add(keyName);
}
);
scene.setOnKeyReleased(
(KeyEvent event) ->
{
String keyName= event.getCode().toString();
inputList.remove(keyName);
}
);
//gameloop
AnimationTimer gameloop = new AnimationTimer() {
@Override
public void handle(long nanotime) {
hero.movement_vector.set(0,0);//kiedy żaden przcysik ni jest wciśnięty postać stoi w miejscu
if(inputList.contains("LEFT"))
hero.movement_vector.add(-hero.moveSize,0);
if(inputList.contains("RIGHT"))
hero.movement_vector.add(+hero.moveSize,0);
if(inputList.contains("UP"))
hero.movement_vector.add(0,-hero.moveSize);
if(inputList.contains("DOWN"))
hero.movement_vector.add(0,+hero.moveSize);
hero.movement_vector.multiply(hero.speed);
hero.position.add(hero.movement_vector);
//sprawdzanie czy bohater spotkał przeciwnika
for(int i=0; i<skeletons.size(); i++) {
Creature skeleton = skeletons.get(i);
if(hero.overlaps(skeleton)) {
System.out.println("COLISION");
skeletons.remove(skeleton);
}
}
//odświeżeni canvasa
content.setFill(Color.GREEN);
content.fillRect(0,0,dlugoscEkranu,szerokoscEkranu);
//dodanie potworów
for(int i=0; i<skeletons.size();i++)
{
skeletons.get(i).render(content);
}
//rysowanie obiektów
castle.draw(content);
//rysowanie bohatera
hero.render(content);
}
};
gameloop.start();
// show na końcu
stage.show();
}
}
Klasa Creature
package com.example.rpgfxmaven;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class Creature
{
public GameControler gameControler;
public int speed;// jak szybko robbi kroki
double moveSize = 50;//jak duże "kroki" robi hero
public Vector position;
public Vector movement_vector;
public Image image;
public Rectangle colision_rectangle;
public Creature() {
position = new Vector(0, 0);
movement_vector = new Vector(0, 0);
colision_rectangle = new Rectangle(0,0,0,0);
speed = 3;
}
public void setImage(String path)
{
image = new Image(path);
colision_rectangle.width = image.getWidth(); //image.getWidth();
colision_rectangle.height = image.getHeight(); //image.getHeight();
}
public Rectangle getColision_rectangle()
{
colision_rectangle.x = position.x;
colision_rectangle.y = position.y;
return colision_rectangle;
}
public void setPosition(double x, double y)
{
position.set(x,y);
}
public boolean overlaps(Creature other_creature) {
return this.getColision_rectangle().overlaps(other_creature.getColision_rectangle());
}
//renderowanie
public void render(GraphicsContext content)
{
content.drawImage(image, position.x, position.y);
}
}
Klasa Rectangle
package com.example.rpgfxmaven;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class Rectangle {
public double x;
public double y;
public double width;
public double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public boolean overlaps(Rectangle other) {
boolean noOverlap = (this.x + this.width < other.x) || (other.x < other.width + this.x) || (this.y + this.height < other.y) || (other.y + other.height < this.y);
return !noOverlap;
}
}
Klasa Vector
package com.example.rpgfxmaven;
public class Vector
{
public double x;
public double y;
public Vector(double x, double y)
{
this.set(x,y);
}
public void set(double x, double y)
{
this.x = x;
this.y = y;
}
public void add(double x, double y)
{
this.x += x;
this.y += y;
}
public void add(Vector other)
{
this.add(other.x, other.y);
}
public void multiply(double number)
{
this.x *= number/60;
this.y *= number/60;
}
}
- RPG-fx-maven.zip (1 MB) - ściągnięć: 11