3
\$\begingroup\$

I am intending to build a circular menu, with menu items equally distributed around the circle. When clicking on a menu item the circle should rotate so that the selected item is facing the top. I am using libgdx and I am not very familiar with the Actors concept, so I intuitively tried to implement an Actor, who is drawing a texture and then transforming it by using Actions, with no success:

    class CircleActor extends Actor {

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        batch.draw(texture1, 100, 100);
    }

    @Override
    public Actor hit(float x, float y) {
        return this;
    }

}

and the rotate action:

    CircleActor circleActor = new CircleActor();
    circleActor.action(Forever.$(RotateBy.$(0.1f, 0.1f)));
    // stage.addActor();
    stage.addActor(circleActor);

The texture is rectangular, but it doe not work. 1. What is wrong? 2. Is it a good approach to solve the task?

\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

In your draw method, you are currently drawing without taking into account the position or rotation of your actor. You are just telling it to draw the texture at 100, 100. So moving or rotating the actor have obviously no effect.

Update your batch.draw call to pass in the rotation and position of your actor. You can choose between these overloaded methods to do the work.

batch.draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY) 
batch.draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) 
batch.draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise) 

See the SpriteBatch documentation or read about SpriteBatch on the wiki.

With that said your method of doing it looks good. You may want to investigate if you can use some existing actors instead. There are actors like Button, ImageButton, Image, Label etc. that you probably can reuse. Just be warned that some of them don't support rotating. (You can rotate them, but they will do as you; ignore the angle when drawing :p ).

\$\endgroup\$
2
  • \$\begingroup\$ Reusing other Actors is what I did in the End, so that I used a WidgetGroup as container to hold the menu items and Image as an rotation enabled Menu Item. I was just confused that some Actors do not support rotation, but that is probably because Skins are not going well together with such transformation. Thx. \$\endgroup\$
    – joecks
    Jun 27, 2012 at 11:21
  • \$\begingroup\$ Nice you found a solution. \$\endgroup\$
    – Matsemann
    Jun 27, 2012 at 12:00
1
\$\begingroup\$

To rotate them in a circular manner, you have to set your actor's origin first:

image.setOrigin(image.getWidth() / 2,image.getHeight() / 2);
image.addAction(Actions.rotateBy(360, .2f));
\$\endgroup\$

You must log in to answer this question.

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