Вы можете посмотреть полный исходный код в моем репозитории github, но я дам точный фрагмент, который дает мне странную ошибку. Ошибка происходит в следующей строке внутри метода moveWorldObject().
worldObjects.get(0).setPosition(299,200);
аргументы, переданные как (299,200), не работают, если значение x в формате (x, y) соответствует аргументам исходной заданной позиции в createWorldObject(), которые являются (300 300). например (300 200), ничего не сделает, кроме (299 200). Другими словами, x arg in (x, y) не может равняться начальному значению, переданному в setPosition(), или код будет работать неправильно.
Если я изменил исходные значения setPosition() внутри createWorldObject() на (100,100) вместо (300,300) и аналогичным образом изменил setPosition() внутри moveWorldObject(), чтобы сказать (100,200), ничего не происходит, но если я вместо этого установил (99,200), он работает, как и предполагалось.
Я не уверен, почему, и это не имеет смысла и заставляет меня думать, что есть поврежденный файл в моем netbeans или java config, но после переустановки обоих это, похоже, не так, поэтому мне интересно, можете ли вы, ребята, воспроизвести ошибку или дать мне некоторую обратную связь.
package world;
import cortex_java.Direction;
import java.awt.Point;
import java.util.ArrayList;
public class World {
ArrayList<WorldListener> worldListeners;
ArrayList<WorldObject> worldObjects;
public World() {
worldListeners = new ArrayList<WorldListener>();
worldObjects = new ArrayList<WorldObject>();
}
public void addWorldListener(WorldListener wl) {
worldListeners.add(wl);
}
public void worldHasChanged() {
for (WorldListener listener : worldListeners) {
listener.onWorldEvent(new WorldEvent());
}
}
public void createWorld() {
System.out.println("World: new world has been created");
createWorldObject();
worldHasChanged();
}
public void createWorldObject() {
WorldObject worldObject = new WorldObject();
worldObject.setSize(0, 0, 32, 32);
worldObject.setPosition(300, 300);
worldObject.setTexture("../res/images/spritesheet_1.png", 0, 0, 96, 128, 0, 0, 32, 32);
worldObject.setCollidable(true);
worldObjects.add(worldObject);
worldHasChanged();
}
public WorldObject getWorldObject() {
return worldObjects.get(0);
}
public void moveWorldObject() {
//left x-size
//right x+size
//up
Direction dir = worldObjects.get(0).getDirection();
switch (dir) {
case NORTH:
//worldObjects.get(0).setPosition((int) (worldObjects.get(0).getXPosition()), (int) (worldObjects.get(0).getYPosition() - worldObjects.get(0).getHeight()));
// worldObjects.get(0).setPosition(0, 332);
// worldObjects.get(0).setTextureFrame(0, 32, 32, 64);
worldObjects.get(0).setPosition(299,200);
// worldObjects.get(0).setPosition(132, 100);
break;
case SOUTH:
//worldObjects.get(0).setPosition((int) (worldObjects.get(0).getXPosition()), (int) (worldObjects.get(0).getYPosition() + worldObjects.get(0).getHeight()));
// worldObjects.get(0).setTextureFrame(0, 0, 32, 32);
break;
case WEST:
//worldObjects.get(0).setPosition((int) (worldObjects.get(0).getXPosition() - worldObjects.get(0).getWidth()), (int) (worldObjects.get(0).getYPosition()));
// worldObjects.get(0).setTextureFrame(0, 0, 32, 32);
break;
case EAST:
//worldObjects.get(0).setPosition((int) (worldObjects.get(0).getXPosition() + worldObjects.get(0).getWidth()), (int) (worldObjects.get(0).getYPosition()));
// worldObjects.get(0).setTextureFrame(0, 0, 32, 32);
break;
}
worldHasChanged();
}
}
Код World Obect ниже для каждого запроса пользователя в комментарии ниже:
public class WorldObject {
//position stores coordinates in x,y format
Point position;
//size stores bounding information related to the size of the world object in the x1,y1,x2,y2 format
Rectangle size;
//collidable is a flag used for collision detection
boolean collidable;
//texture stores a filename and the x1,x2,y1,y2 source information about a bitmap that represents the world object
Texture texture;
//direction stores where an world object is facing in a north,west,south, or east format
Direction direction;
public WorldObject() {
//contructor initialises all of the properties of the world object
position = new Point();
size = new Rectangle();
collidable = false;
texture = new Texture();
}
public boolean isCollidable() {
return collidable;
}
public Rectangle getSize() {
return size;
}
public double getWidth() {
return size.getWidth();
}
public double getHeight() {
return size.getHeight();
}
public Point getPosition() {
return position;
}
public int getXPosition() {
return position.x;
}
public int getYPosition() {
return position.y;
}
public Texture getTexture() {
return texture;
}
public Direction getDirection() {
return direction;
}
public void setCollidable(boolean arg) {
collidable = arg;
}
public void setSize(Rectangle arg) {
size = arg;
}
public void setSize(int i, int i0, int i1, int i2) {
size.setBounds(new Rectangle(i, i0, i1, i2));
}
public void setWidth(int w) {
size.setSize(w, (int) size.getHeight());
}
public void setHeight(int h) {
size.setSize((int) size.getWidth(), h);
}
public void setPosition(int i, int i0) {
position.setLocation(i, i0);
}
public void setPosition(Point p) {
position = p;
}
public void setXPosition(int x) {
position.setLocation(x, position.getY());
}
public void setYPosition(int y) {
position.setLocation(position.getX(), y);
}
public void setTexture(String filename, int source_x1, int source_y1, int source_x2, int source_y2, int frame_x1, int frame_y1, int frame_x2, int frame_y2) {
texture.setTextureAddress(filename);
texture.setTextureSource(source_x1, source_y1, source_x2, source_y2);
texture.setTextureFrame(frame_x1, frame_y1, frame_x2, frame_y2);
}
public void setTextureFrame(int x1, int y1, int x2, int y2) {
texture.setTextureFrame(x1, y1, x2, y2);
}
public void setDirection(Direction dir) {
direction = dir;
}
}
У вашего рендерера есть ошибка копирования/вставки
Renderer.java
private void render(World world) {
composite_GO.setColor(Color.black);
composite_GO.fillRect(0, 0, 800, 600);
composite_GO.drawImage(worldLayer[1],
world.getWorldObject().getXPosition(),
world.getWorldObject().getXPosition(), null);
}
должно быть
private void render(World world) {
composite_GO.setColor(Color.black);
composite_GO.fillRect(0, 0, 800, 600);
composite_GO.drawImage(worldLayer[1],
world.getWorldObject().getXPosition(),
world.getWorldObject().getYPosition(), null);
}
Вместо x/y вы используете x-часть своей позиции дважды.