0

I'm trying to develop a game which has a background consists of 4*4 box created as texture. And I have an actor which is moving in the boxes. It is like 2048 game. But I have just one character that will move around.

enter image description here

I'm using actor as moving character because I can implement moving animation easily using MoveToAction. (As far as I know)

I'm not sure if this is the best implemetation. If there is a better way please let me know.

Ok what I've done so far is;

I'have a class extended from ApplicationAdapter.

This is my Actor class;

public class MyActor extends Actor {
    Texture texture = new Texture(Gdx.files.internal("joffrey.png"));
    public boolean started = false;

    public MyActor() {
        setBounds(imageWidth / 2, imageHeight / 2, texture.getWidth() / 2, texture.getHeight() / 2);
    }

    @Override
    public void draw(Batch batch, float alpha) {
        batch.draw(texture, this.getX(), getY());
    }
}

So I want to move this (joffrey) from one box to another using MoveToAction when user Swipes right, left, up, down.

I created a class SimpleDirectionGestureDetector as told in here; LibGDX - Get Swipe Up or swipe right etc.?

And here is how I used the class;

Gdx.input.setInputProcessor(new SimpleDirectionGestureDetector(new SimpleDirectionGestureDetector.DirectionListener() {

        @Override
        public void onUp() {
            moveAction = new MoveToAction();
            moveAction.setPosition(myActor.getX(), myActor.getY() + imageHeight);
            moveAction.setDuration(0.1f);
            myActor.addAction(moveAction);
        }

        @Override
        public void onRight() {
            moveAction = new MoveToAction();
            moveAction.setPosition(myActor.getX() + imageWidth, myActor.getY());
            moveAction.setDuration(0.1f);
            myActor.addAction(moveAction);
        }

        @Override
        public void onLeft() {
            moveAction = new MoveToAction();
            moveAction.setPosition(myActor.getX() - imageWidth, myActor.getY());
            moveAction.setDuration(0.1f);
            myActor.addAction(moveAction);
        }

        @Override
        public void onDown() {
            moveAction = new MoveToAction();
            moveAction.setPosition(myActor.getX(), myActor.getY() - imageHeight);
            moveAction.setDuration(0.1f);
            myActor.addAction(moveAction);
        }
    }));

Everything looks fine until here. When I launch the app it is working fine at the beginning but after some moves the moving action look so laggy and then suddenly application is closed without any error message or anything and nothing in the logcat!

Do you have any idea what am I mising here?

Moreover, is this the right way to implement a moving action ? Do I have to use Actors to be able to give moving animation to a texture ?

Thanks fo any help.

Community
  • 1
  • 1
Mtok
  • 1,600
  • 11
  • 36
  • 62
  • Can you share your code for SimpleDirectionGestureDetector? Sounds like it may be firing `onUp`, `onRight`, etc. many times during a swipe. Or it could be something else. Why do you assign the new actions to a member variable (`moveAction`)? Are you using it somewhere else? And no, you don't have to use Actors. You can roll your own solution if you want. – Tenfour04 Jun 29 '16 at 13:59

1 Answers1

0

You can implement the action movement differently. For instance:

  • On your SimpleDirectionGestureDetector, on each action (up, down, etc) you set a "direction". This is just a property in the main class (MyActor) that holds a value that represents where the movement should head to.
  • Override the act() method and there check the direction and apply the movement. I wouldn't create an action every time because it generates too much garbage. Instead you can use a counter that you add the delta to and calculate the progress. Check: https://github.com/libgdx/libgdx/wiki/Interpolation
  • Once the counter reaches the max time (0.1f as in the action) you set the direction to something neutral, like no movement.

Sorry, no code now as I'm writing this from my phone, but I hope this helps understand the logic.

Fechy
  • 46
  • 3