您可以在我的github存储库中查看完整的源代码,但我会提供给我奇怪bug的确切代码片段。这个错误发生在moveWorldObject()方法的下面一行。
worldObjects.get(0).setPosition(299,200);
如果(x,y)格式中的x值与createWorldObject()中的初始设置位置参数匹配(300,300),则作为(299,200)传入的参数不起作用。例如(300,200)不会做任何事情,但(299,200)会。换句话说,(x,y)中的x参数不能等于传递给setPosition()的初始值,否则代码将无法正确运行。
如果我将createWorldObject()内部的初始setPosition()值更改为(100,100)而不是(300,300),并且类似地将moveWorldObject()内部的setPosition()更改为(100,200),没有任何事情发生,但如果我将其设置为(99,200),它会像应该的那样运行。
我不知道为什么,这没有意义,让我认为有一个损坏的文件在我的netbeans或java配置,但重新安装两者后,这似乎不是它,所以我想知道如果你们能重现错误或给我一些反馈。
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();
}
}
世界对象代码下面每个用户请求在下面的评论:
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部分而不是x/y。