Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScaleTo not working in Libgdx

Tags:

android

libgdx

I'm trying to scale my Actor using actions. But it does not work. I just want my actor to increase/decrease its size over a given time.

The Actor will just wait for the 2 seconds duration I have given the ScaleTo. It correctly moves based on the MoveTo action I gave it.

public class SpriteTest extends Actor {
       private Sprite sprite;
       private TextureAtlas atlas;
       Rectangle boundsd = new Rectangle();

       public SpriteTest(FirstGame game) {
           //super(game);
           Gdx.app.log( FirstGame.LOG, "spritetest's costructor" );
           atlas = new TextureAtlas(Gdx.files.internal("pages-info.atlas"));
           sprite = atlas.createSprite("Plus");
       }

       public void draw(SpriteBatch batch,float parentAlpha) {
            batch.draw(sprite, x, y);
       }

// We are adding the actor to the stage in another class

public class LevelScreen extends AbstractScreen {
   private Jumper2D jumper2d;
   SpriteTest obstacled = new SpriteTest(game); 

   public LevelScreen(FirstGame game) {
      super(game);
   }

   @Override
   protected boolean isGameScreen() {
      return true;
   }  

   @Override
   public void show() {
      super.show();

      stage.addActor(obstacled); 
      obstacled.action  
      (Forever.$
            (Sequence.$
               (ScaleTo.$(1.4f, 1.4f, 2),(MoveTo.$(100,120, 3f) ))
                         ));
      jumper2d = Jumper2D.create(getAtlas());
      stage.addActor(jumper2d);
      stage.draw();
   }

   public void render () {
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
      stage.draw();
   }
}

Any help is appreciated

like image 641
Silentasssasssin Avatar asked Feb 18 '23 09:02

Silentasssasssin


1 Answers

Your draw() method for SpriteTest is ignoring any scale or rotation settings on the Actor. You need to draw the sprite scaled/rotated/sized appropriately. (You may also need to set the Actor's x, y, width, and height --- see setBounds)

public void draw(SpriteBatch batch, float parentAlpha)
{
     final Color c = getColor();
     batch.setColor(c.r, c.g, c.b, c.a * parentAlpha);
     batch.draw(sprite, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}
like image 116
P.T. Avatar answered Feb 20 '23 00:02

P.T.