1
\$\begingroup\$

Let me explain: I´m reading a book I got given that is called "Beginning Java Game Development with LibGDX", and get to the section where the book talks about animating actors, or animating "sprites". The example code they presented is as follows:

    public class AnimatedActor extends BaseActor {
    private float elapsedTime;
    private Animation anim;

    public AnimatedActor(){
        super();
        elapsedTime=0;
    }

 public void setAnimation(Animation a)
 {
 Texture t = a.getKeyFrame(0).getTexture();
 setTexture( t );
 anim = a;
 }

 public void act(float dt)
 {
  super.act( dt );
  elapsedTime += dt;
 if (velocityX != 0 || velocityY != 0)
  setRotation( MathUtils.atan2( velocityY, velocityX ) * MathUtils.radiansToDegrees );
 }

 public void draw(Batch batch, float parentAlpha)
 {
  region.setRegion( anim.getKeyFrame(elapsedTime) );
  super.draw(batch, parentAlpha);
  }
}

I'm running the examples, using android studio, and surprisingly I find the following situation: the code that proposes the book marks an error ... and it is exactly in this line:

 public void setAnimation(Animation a)
 {
 Texture t = a.getKeyFrame(0).getTexture(); <------- getTexture() is the error
 setTexture( t );
 anim = a;
 }

The error I'm getting is

cannot resolve method getTexture()

I'm a bit surprised, since I'm not supposed to have any errors, since I'm reading from a book ... trying to implement something similar for my project, but ... I found this. Surely, it should be that the function should be removed in recent versions of LibGDX.

The only thing I try to do is that the moment you click on an actor on the stage, the texture set, change to the animated sprite. How can you create an animation in LibGDX?

\$\endgroup\$
  • \$\begingroup\$ You need to show us the error you get for us to be able to help. \$\endgroup\$ – Charanor May 10 '17 at 5:54
  • \$\begingroup\$ @Charanor the error is "Cannot resolve method getTexture()". \$\endgroup\$ – TwoDent May 10 '17 at 15:23
2
\$\begingroup\$

The reason you're getting your error is because the Animation class doesn't know what key frame class it should use. The Animation class is generic, meaning you have to supply the type of keyframe it should use within <>. I suspect that your example is supposed to use TextureRegion since a texture region has a method called getTexture().

To fix it just change all of your:

Animation

To this:

Animation<TextureRegion>
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thaaank you my friend \$\endgroup\$ – TwoDent May 10 '17 at 16:59

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.