1
votes

I'm trying to move a sprite in libGDX by using a method, that is changing its x and y coordinates every time it is called (it's supposed to be an "animation" for a card game).

if (card.isAnimated()) {                               

                            card.getSprite().setX(card.getX());
                            card.getSprite().setY(card.getY());
                            System.out.println("X: " + card.getSprite().getX());
                            System.out.println("Y: " + card.getSprite().getY());
                            card.getSprite().draw(batch); }

The System.out updates perfectly with the new coordinates, yet the position of the card on the screen doesn't change. Another weird thing is: I'm using drag&drop that works basically the same way, and it's doing fine!

if(card.isDragged() == true){
                            //Card is dragged:
                            card.getSprite().setX(card.getX());
                            card.getSprite().setY(card.getY());
                            card.getSprite().draw(batch); }

Could the render-Function be blocked by the automatic function? It looks similar to this:

 public void animateCardsPlayed(ArrayList<Card> cards) {
    for(Card card : cards) {
                card.setAnimated(true);
                float deltaX = card.getX() - CardStack.ACTIVE_X;
                float deltaY = card.getY() - CardStack.ACTIVE_Y;
                System.out.println("DELTAX: " + deltaX);
                System.out.println("DELTAY: " + deltaY);

                if (deltaX <= 0) { //Karte links von Stapel
                    if (deltaY <= 0) { //Karte unter Stapel
                        while (card.getX() <= CardStack.ACTIVE_X && card.getY() <= CardStack.ACTIVE_Y) {
                            if (card.getX() <= CardStack.ACTIVE_X) {
                                float newX = card.getX() - deltaX/1000f;
                                card.setX(newX);
                                card.getSprite().setX(card.getX());
                            }
                            if (card.getY() <= CardStack.ACTIVE_Y) {
                                float newY = card.getY() - deltaY/1000f;
                                card.setY(newY);
                                card.getSprite().setY(card.getY());
                            }
                            System.out.println("1HIER");
                            screen.render(0); //Maybe that's the problem?
                        } //...

Does anybody have an idea? I would be utterly thankful.. Best Regards, Sebie

UPDATE: It seems like the whole screen is freezing until the function is done.. Is there any possibility to do something like that without multithreading?

1
Unrelated, but I's bug-prone (due to redundancy) to use Sprite instead of TextureRegion when your Card class is in charge of storing the object's coordinates.Tenfour04

1 Answers

1
votes

Your while loops are indeed blocking the render thread. The way to animate something is to update it once per frame. The game's render method is called repeatedly for you. Each card should only take one step of animation in your method. If you want to animate something and then continue doing something afterwards, you must use variables to track what you are doing to determine if a particular animation needs to finish, for example.

And you should never be calling your screen's render method directly.