I'm trying to make a sprite bounce off the edge of the screen when it is less than 0. Right now it just zooms across the screen into the void. Here is my code-note that CentipedeBody is a class that extends Sprite. In the render
method I call ex.update();
which is an object of the class. Then in between the batch.begin()
and batch.end()
I have batch.draw(ex,ex.getPosition().x,ex.getPosition().y,ex.getSize().x,ex.getSize().y);
public class CentipedeBody extends Sprite
{
public CentipedeBody(TextureRegion image,Vector2 position,Vector2 size) {
super(new TextureRegion(image));
this.position = position;
this.size=size;
bounds=new Rectangle(position.x,position.y,8,8);
left=true;
}
public void update() {
bounds.set(getPosition().x,getPosition().y,8,8);
if (left==true) {
position.x-=(.5f);
up=false;
down=false;
right=false;
left=true;
}
if (right==true) {
position.x+=.5f;
left=false;
right=true;
down=false;
up=false;
}
if (down==true) {
position.y-=(.5f);
right=false;
left=false;
down=true;
up=false;
if(position.x<0)
{
left=false;
right=true;
}
}
}