矩形移动不正常

时间:2014-02-09 19:00:10

标签: java input slick2d rectangles

我正在使用光滑制作一些基本游戏。目前我正在玩矩形,我似乎无法用输入控制一个。基本上我使用params for x,y,然后我用size来绘制其余的。当我使用“D”键时,我希望矩形向右平移,但它的大小会增加

if(d.equals(Direction.RIGHT))
{
    boundingBox.setX(boundingBox.getX() + 1);
    boundingBox.setWidth(boundingBox.getWidth() + 1);
}

然后结果是

My code

package com.mra;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;

public class Block
{

        public enum Direction
        {
                LEFT,
                RIGHT,
                UP,
                DOWN;
        }

        float size;
        Color color;
        Rectangle boundingBox;

        public Block(int x, int y, float size, Color color)
        {
                this.size = size;
                this.color = color;
                boundingBox = new Rectangle(x, y, x + size, y + size);
        }

        public float getSize()
        {
                return size;
        }

        public Color getColor()
        {
                return color;
        }

        public void render(Graphics g)
        {
                g.drawRect(boundingBox.getX(), boundingBox.getY(), boundingBox.getWidth(), boundingBox.getHeight());
        }

        public void inputMove(Input input, int KEY, Direction d)
        {
                if(input.isKeyDown(KEY))
                {
                        if(d.equals(Direction.LEFT))
                        {

                        }
                        if(d.equals(Direction.RIGHT))
                        {
                                boundingBox.setX(boundingBox.getX() + 1);
                                boundingBox.setWidth(boundingBox.getWidth() + 1);
                        }
                        if(d.equals(Direction.UP))
                        {

                        }
                        if(d.equals(Direction.DOWN))
                        {

                        }
                }
        }

}

1 个答案:

答案 0 :(得分:1)

您正在更改边界框的宽度。

如果您只想更改位置,只需更改X和Y即可。

if(d.equals(Direction.RIGHT))
{
  boundingBox.setX(boundingBox.getX() + 1);
}