Kevin Bryan
Kevin Bryan

Reputation: 1858

How to set BoundingRectangle Position

I tried to set the bounding rectangle position.

spr.getBoundingRectangle().setPosition(100,100);
Sytem.out.println(spr.getBoundingRectangle.getX() + " " + spr.getBoundingRectangle.getX());
// and the output is always 0,0(position)

I also tried to set it's position whenever condition == true

if(justTouched == true){
     spr.getBoundingRectangle().setPosition(100,100);
    Sytem.out.println(spr.getBoundingRectangle.getX() + " " + spr.getBoundingRectangle.getX());
}// but the result is the same

Upvotes: 0

Views: 1036

Answers (2)

Johnathon Havens
Johnathon Havens

Reputation: 730

The bounding Rectangle is generated and/or updated when you call getBoundingRectangle(). This means that you aren't modifying the sprite vertices by using setX or setY of the Rectangle class. Instead use Sprite.setPosition(x,y); Moving the sprite this way moves the bounding rectangle.

There are also the following methods that alter the bounding rectangle of a sprite (don't confuse this setX and setY with the Rectangle classes setX and setY, this one modifies the sprite's vertices):

setX(x)
setY(y)
setBounds(x,y,width,height)
setSize(width,height)
translate(diffX,diffY)
translateX(diffX)
translateY(diffY)

The way that seems to fit your question best would be the following.

spr.setPosition(100,100);
Rectangle sprBounds = spr.getBoundingRectangle();
Sytem.out.println(sprBounds.getX() + " " + sprBounds.getY());

Upvotes: 1

Taner Seytgaziyev
Taner Seytgaziyev

Reputation: 93

First off I just want to point out that in your System.out.println() your printing out two X coordinates. Anyway, the X and Y coordinate are at the bottom left corner of your sprite, so if your Sprite is 100 x 100 then the result is right. I would recommend making your own rectangle method called like getBounds() or something and defining the x, y, height, and width and then making it follow your sprite. I usually do this in my player character and create his own bounding boxes. Maybe its just me :/

Upvotes: 0

Related Questions