1
\$\begingroup\$

How to use moveTo?

I do so:

actor.addAction(Actions.moveTo(500, 500, 10));

but he does not move

Thanks you

UPDATE

does not move

my code:

public class MyActions extends ApplicationAdapter {
    private Stage stage;
    private Actor actor;

    @Override
    public void create () {

        stage = new Stage();
        actor = new Actor(){
            {
                setSize(100, 100);
            }
            Sprite actorSprite = new Sprite(new Texture(Gdx.files.internal("badlogic.jpg")), 57, 10, (int)getWidth(), (int)getHeight());

            @Override
            public void draw(Batch batch, float parentAlpha) {
                actorSprite.draw(batch, parentAlpha);
            }
        };

        stage.addActor(actor);
        actor.addAction(Actions.moveTo(500, 500, 1));
    }


    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        actor.act(Gdx.graphics.getDeltaTime());
        //stage.act();
        stage.draw();
    }
}

what's wrong?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You commented out stage.act(). No acting will be done on actors on the stage. \$\endgroup\$
    – Madmenyo
    Commented Jun 1, 2017 at 11:01

2 Answers 2

0
\$\begingroup\$

Answer doesn't make sense after edit.

You should use Texture, not Sprite here. Your code should be like this :

public class MyActions extends ApplicationAdapter {
  private Stage stage;
  private Actor actor;
  private Texture texture;

  @Override
  public void create () {
      texture = new Texture(Gdx.files.internal("badlogic.jpg"));
      stage = new Stage();
      actor = new Actor(){
          {
              setSize(100, 100);
          }

        @Override
        public void draw(Batch batch, float parentAlpha) {
            batch.draw(texture,(int)getX(), (int)getY());
        }
      };

      stage.addActor(actor);
      actor.addAction(Actions.moveTo(500, 500, 1));
  }


  @Override
  public void render () {
      Gdx.gl.glClearColor(1, 1, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      stage.act(Gdx.graphics.getDeltaTime());
      stage.draw();
  }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Perhaps you could elaborate more? This answer has been flagged as low quality due to its size. \$\endgroup\$
    – Gnemlock
    Commented Dec 21, 2016 at 12:57
  • \$\begingroup\$ Does not move. Question update. \$\endgroup\$
    – ravend
    Commented Dec 21, 2016 at 13:22
  • \$\begingroup\$ It's been a while since I've tinkered with libgdx, but Sprite does have a constructor that takes a Texture. This is simply removing that layer of abstraction. \$\endgroup\$
    – Jax
    Commented Dec 31, 2016 at 5:15
0
\$\begingroup\$

Your problem is that you're drawing your sprite but you're not updating your sprite's position. Your draw method should be:

@Override
public void draw(Batch batch, float parentAlpha) {
    actorSprite.setPosition(getX(), getY());
    actorSprite.draw(batch, parentAlpha);
}

This will update your sprite's position to your actor's position before you draw it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .