在Netbeans java项目中获得不稳定的行为

时间:2014-07-10 12:20:03

标签: java netbeans

您可以在my github repository查看完整的源代码,但我会提供给我一个奇怪错误的确切代码段。该错误发生在moveWorldObject()方法中的以下行。

  worldObjects.get(0).setPosition(299,200);

如果(x,y)格式中的x值与createWorldObject()中的初始设置位置参数匹配,则传递为(299,200)的参数不起作用,恰好是(300,300)。例如(300,200)除了(299,200)之外什么都不做。换句话说,(x,y)中的x arg不能等于传递给setPosition()的初始值,或者代码不能正确运行。

如果我将createWorldObject()内的初始setPosition()值更改为(100,100)而不是(300,300),并且类似地将moveWorldObject()内的setPosition()更改为说(100,200)没有任何反应,但是如果我把它设置为说(99,200),它运行就像它应该的那样。

我不确定为什么,这没有任何意义,让我觉得我的netbeans或java配置中有一个损坏的文件,但重新安装后,这似乎不是它,所以我和#39;我想知道你们是否可以重现错误或给我一些反馈。

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;
}

}

1 个答案:

答案 0 :(得分:1)

您的渲染器有复制/粘贴错误 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。