Search code examples
javaandroidlibgdx2d-games

Making A Sprite Bounce Off The Edge


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

Solution

  • why bounds in your child class, Sprite having already bounds, use that if you're interested in collision with other objects. Same for position and for size, I don't think you need these extra data member in your Child class, use parent x,y for position and width and height for dimension.

    public class CentipedeBody extends Sprite {
    
        enum State{
            LEFT,RIGHT,DOWN
        }
    
        State currentState,previousState ;
    
        public static final float DOWN_MOVEMENT=50;
        public float downMovCounter;
        public float speed;
    
        public CentipedeBody(TextureRegion image, Vector2 position, Vector2 size) {
            super(new TextureRegion(image));
            setPosition(position.x,position.y);
            setSize(size.x,size.y);
            currentState=State.LEFT;
            previousState=State.LEFT;
            speed=50;
        }
    
        public void update() {
    
            float delta=Gdx.graphics.getDeltaTime();
            if(currentState ==State.LEFT){
                setPosition(getX()-speed*delta,getY());
                if(getX()<0) {
                    previousState=currentState;
                    currentState = State.DOWN;
                }
            }
    
            if(currentState ==State.RIGHT){
                setPosition(getX()+speed*delta,getY());
                if(getX()> Gdx.graphics.getWidth()-getWidth()) {
                    previousState=currentState;
                    currentState = State.DOWN;
                }
            }
    
            if(currentState ==State.DOWN){
                setPosition(getX(),getY()+speed*delta);
                downMovCounter++;
                if(downMovCounter>DOWN_MOVEMENT){
                    downMovCounter=0;
                    currentState =previousState==State.LEFT?State.RIGHT:State.LEFT;
                }
    
            }
        }
    
    }
    

    In render method

    batch.begin();
    batch.draw(centipedeBody,centipedeBody.getX(),centipedeBody.getY(),centipedeBody.getWidth(),centipedeBody.getHeight());
    batch.end();
    centipedeBody.update();
    

    May be you need bounds, size and position in CentipedeBody, I can't judge your game requirement by one class code so you can easily integrate your variable in my code.