34

I'm just going through the javadoc and various tutorials on libgdx and I'm at the stage of trying to figure out differences between various concepts that seem similar to me or provide similar capabilities in libgdx.

At first I thought scene2d was about creating interactive items such as menus, etc but various tutorials I'm reading use scene2d/actors for the main game items (i.e. the player, etc) and others just use sprites.

What exactly is the difference between using Sprite and Actor (i.e. scene2D) in a game and when should you choose?

Thanks.

Neil Walker
  • 6,400
  • 14
  • 57
  • 86
  • I am facing same confusion right now. Libgdx examples uses sprites, somewhere actor and image as well. Have you figured out how all this goes around? – Khawar Raza Feb 15 '13 at 08:25

2 Answers2

73

A Sprite is basically an image with a position, size, and rotation. You draw it using SpriteBatch, and once you have your your Sprites and your SpriteBatch, you have a simple, low-level way to get 2D images on the screen anywhere you want. The rest is up to you.

Actor, on the other hand, is part of a scene graph. It's higher-level, and there's a lot more that goes into a scene graph than just positioning images. The root of the scene graph is the Stage, which is not itself displayed. The Stage is a container for the Actors that you add to it, and is used for organizing the scene. In particular, input events are passed down through the Stage to the appropriate Actor, and the Stage knows when to tell the Actor to draw itself. A touch event, for example, only gets sent to the Actor that got touched.

But note that Actor does not contain a texture like Sprite does. Instead you probably want to use Image, a subclass of Actor that's probably closer to Sprite than just a plain Actor. Other subclasses of Actor contain text, and so on.

Another big advantage of Actors is that they can have Actions. These are a big topic, but they essentially allow you to plan a sequence of events for that Actor (like fading in, moving, etc) that will then happen on their own once you set them.

So basically Actor does a lot more than Sprite because it's part of a graphical framework.

Steve Blackwell
  • 5,904
  • 32
  • 49
0

It is more or less matter of taste. If you want to use actions and stage, use actors. Actors cannot be drawn directly, you need to override draw method. Inside draw you can use sprites.

  • 8
    It's not just a matter of taste. Sprites have limitations that Actors don't. If you want to have an enemy on the screen you create an Actor for that, implement the action "Attack" and can then in your game logic trigger the action with one line of code. With sprites you would need to code the action again and again, in every part of your program that you need it. As well as that the actor and screen are a LOT easier to handle. – AreusAstarte Apr 26 '13 at 21:50
  • 3
    @AreusAstarte That's not true at all. You could (and should) create a class that extends sprite with all the functionality you need. – benbot Jan 02 '15 at 20:16
  • 2
    That#s not the point. The "matter of taste" is indeed nonsense. As they stand they do different things for different reasons. ANY class can be extended to anything by your logic thus ANY question about functionality can be answerered with "just extend the class" - which would be pretty poor advice when there is something better suited already there in the libraries. – RichieHH Jan 16 '15 at 18:31
  • 1
    @RichieHH It isn't the best in all the cases. For instance, sometimes you might need to interrupt action which is already in progress and your game object might end up in some undefined state, between pixels, so to say. Actors functionality is pretty minimal from code amount point of view to not reimplement it by yourself if needed. So, the one should use his common sense and brain to choose. – son of the northern darkness Jan 19 '15 at 10:31
  • 1
    @sonofthenortherndarkness : no one said they were the best in all cases. I said "They do different things for different reasons" and then "*when* there is something better suited there in the libraries". So Im not sure what your reply is trying to achieve. As for "For instance, sometimes you might need to interrupt action which is already in progress and your game object might end up in some undefined state, between pixels, so to say" this is irrelevant to the subject in hand. – RichieHH Feb 11 '15 at 13:54
  • @RichieHH: No one? I think answer from AreusAstarte tells just that - use actors in all the cases. "Matter of taste" means that you can achieve the same results either using actors or sprites. – son of the northern darkness Feb 19 '15 at 14:23