Question

I am working with slick to make some basic games. Currently I'm playing around with rectangles and I cannot seem to be able to control one with input. Basically I use params for x, y, and then i use size to draw the rest. When I use my "D" key I want the rectangle to translate right but instead it increases in size

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

And then the result is

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))
                        {

                        }
                }
        }

}
Was it helpful?

Solution

You're changing the width of the bounding box.

If you only want to change the position, just change the X and the Y and nothing else.

if(d.equals(Direction.RIGHT))
{
  boundingBox.setX(boundingBox.getX() + 1);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top