Здравствуйте подскажите как мне правильно решить проблему с Actor

В данной момент при моем коде квадрат и треугольник передвигаются одновременно,а мне нужно что бы они передвигались только после их выбора.Пожалуйста помогите

public class Figure extends Actor {
private TextureRegion textureRegion;
private Rectangle rectangle;

public Figure(){
    super();
    textureRegion= new TextureRegion();
    rectangle= new Rectangle();

}
public void setTextureRegion(Texture t){
    textureRegion.setRegion(t);
    setSize(t.getWidth(),t.getHeight());
    rectangle.setSize(t.getWidth(),t.getHeight());
}

public Actor hit(float x, float y, boolean touchable) {
    if (touchable && getTouchable() != Touchable.enabled) return null;
    return x >= 0 && x < 800 && y >= 0 && y < 600 ? this : null;
}
public void act(float dt){
    super.act(dt);

}


public void draw(Batch batch,float parentAlpha){
    super.draw(batch,parentAlpha);
    Color c=getColor();
    batch.setColor(c.r,c.g,c.b,c.a);
    if(isVisible())
        batch.draw(textureRegion,getX(),getY(),getOriginX(),getOriginY(),getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
}

}

public class TriAngle extends Figure {

private Vector2 position;
private float w = 800;
private float h = 600;


public TriAngle() {

}

@Override
public Actor hit(float x, float y, boolean touchable) {
    return super.hit(x, y, touchable);
}

public void act(float dt) {
    super.act(dt);

    if(Gdx.input.isKeyJustPressed(Input.Keys.LEFT))
        this.moveBy(-w/4,0);
    if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT))
        this.moveBy(w/4,0);
    if(Gdx.input.isKeyJustPressed(Input.Keys.UP))
        this.moveBy(0,h/4);
    if(Gdx.input.isKeyJustPressed(Input.Keys.DOWN ))
        this.moveBy(0,-h/4);


}

}

public class Quad extends Figure{
private float w=800;
private float h=600;
public Quad(){

    super();
}
public void act(float dt) {

    super.act(dt);
    if(Gdx.input.isKeyJustPressed(Input.Keys.LEFT))
        this.moveBy(-w/4,0);
    if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT))
        this.moveBy(w/4,0);
    if(Gdx.input.isKeyJustPressed(Input.Keys.UP))
        this.moveBy(0,h/4);
    if(Gdx.input.isKeyJustPressed(Input.Keys.DOWN ))
        this.moveBy(0,-h/4);


    }
}

public class MyGdxGame extends GameOsnova {

private TriAngle triAngle;
private Quad quad;
private Figure background;

private float w=800;
private float h=600;
@Override
public void initialize() {


}

@Override
public void update(float dt) {

}

@Override
public void create () {

    mainStage =new Stage();

    background=new Figure();
    background.setTextureRegion(new Texture(Gdx.files.internal("Pole.png")));
    background.setPosition(0,0);
    background.setSize(w,h);
    mainStage.addActor(background);


    triAngle= new TriAngle();
    triAngle.setTextureRegion(new Texture(Gdx.files.internal("TriAngle.png")));
    triAngle.setPosition(0,0);
    triAngle.setSize(w/4,h/4);
    //triAngle.setBounds(0,0,w/4,h/4);
    mainStage.addActor(triAngle);


    quad= new Quad();
    quad.setTextureRegion(new Texture(Gdx.files.internal("start_button.png")));
    quad.setPosition(3*w/4,0);
    quad.setSize(w/4,h/4);
   // quad.addAction(show);
    mainStage.addActor(quad);


}


@Override
public void dispose () {
    mainStage.dispose();

}

}

public abstract class GameOsnova extends Game {

protected Stage mainStage;

    public abstract void initialize();

       public void render(){

    float dt= Gdx.graphics.getDeltaTime();

      mainStage.act(dt);
     // mainStage.hit(200,400,false);
    // mainStage.hit(0,0,true);
     update(dt);
     Gdx.gl.glClearColor(0,0,0,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     mainStage.draw();
}

public abstract void update(float dt);

}


Ответы (1 шт):

Автор решения: bergik37

Ответ нашёл в книге Java Game Development with LibGDX From Beginner to Professional Second Edition в 9 главе (Drag-and-Drop Games). В этой главе рекомендуют создать замечательный класс DragAndDropActor.

public class DragAndDropActor extends BaseActor {
    private DragAndDropActor self;
    private float grabOffsetX;
    private float grabOffsetY;

    public DragAndDropActor(float x, float y, Stage s) {
        super(x, y, s);
        self = this;
        addListener(
            new InputListener() {
                public boolean touchDown(InputEvent event, float offsetX, float offsetY, int pointer, int button) {
                    self.grabOffsetX = offsetX;
                    self.grabOffsetY = offsetY;
                    self.toFront();
                    return true;
                }

                public void touchDragged(InputEvent event, float offsetX, float offsetY, int pointer) {
                    float deltaX = offsetX - self.grabOffsetX;
                    float deltaY = offsetY - self.grabOffsetY;
                    self.moveBy(deltaX, deltaY);
                }

                public void touchUp(InputEvent event, float offsetX, float offsetY, int pointer, int button) {
                    // will add code later
                }
            });
    }

    public void act(float dt) {
        super.act(dt);
    }
}
→ Ссылка