Reputation: 75
You can look at the full source code at my github repository but i'll provide the exact snippet that is giving me the weird bug. The bug happens on the following line inside the moveWorldObject() method.
worldObjects.get(0).setPosition(299,200);
the arguments passed in as (299,200) do not work if the x value in the (x,y) format match the initial set position arguments in the createWorldObject(), which happen to be (300,300). for example (300,200) will not do anything but (299,200) will. In other words the the x arg in (x,y) cannot equal the initial value passed into the setPosition(), or the code won't run correctly.
If I change the initial setPosition() values inside of the createWorldObject() to (100,100) instead of (300,300) and similarly change the setPosition() inside of the moveWorldObject() to say (100,200) nothing happens, but if I instead set it to say (99,200), it runs like it is supposed to.
I'm not sure why and this makes no sense and makes me think there is a corrupt file in my netbeans or java config, but after reinstalling both, that doesn't seem to be it, so i'm wondering if you guys can reproduce the error or give me some feed back.
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 code below per users request in comment below:
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;
}
}
Upvotes: 1
Views: 44
Reputation: 578
Your renderer has a copy/paste error
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);
}
needs to be
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);
}
You use the x part of your position twice instead of x/y.
Upvotes: 1