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
    \$\begingroup\$ You commented out stage.act(). No acting will be done on actors on the stage. \$\endgroup\$ – Madmenyo Jun 1 '17 at 11:01
1
\$\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\$
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\$
  • 1
    \$\begingroup\$ Perhaps you could elaborate more? This answer has been flagged as low quality due to its size. \$\endgroup\$ – Gnemlock Dec 21 '16 at 12:57
  • \$\begingroup\$ Does not move. Question update. \$\endgroup\$ – ravend Dec 21 '16 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\$ – JDSweetBeat Dec 31 '16 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.