0
\$\begingroup\$

well i am trying to achieve the same effect of windows desktop , you can select multiple icons and drag all of them to differents positions.

here you can see how i select the images here you can see how i select the images

in the second image you can see the three sprites selected

in the second image you can see the three sprites selected.

my problem is the only one of the images are moving! not the three :/

here is my code:

public class Nivel extends GameState implements InputProcessor {

    private Texture tex;
    private Sprite sprite;
    private Array<Sprite> sprites;

    private boolean isdragged = false;

    private float x1 = 0;
    private float y1 = 0;
    private float x2 = 0;
    private float y2 = 0;
    private Rectangle rec;
    private Array<Sprite> seleccionados;
    private Vector3 vector = new Vector3();
    private boolean selected = false;
    private TextureRegion region;

    public Nivel(GameStateManager gsm) {
        super(gsm);
        tex = new Texture(Gdx.files.internal("badlogic.jpg"));
        tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        sprites = new Array<Sprite>();
        seleccionados = new Array<Sprite>();//sprites inside the rectangles
        for (int i = 0; i < 6; i++) {
            sprite = new Sprite(tex);
            sprites.add(sprite);
        }
        rec = new Rectangle(-100, -100, 0, 0);
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void handleInput() {}

    @Override
    public void update(float dt) {
        for (int i = 0; i < sprites.size; i++) {
            sprites.get(i).setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
            if (rec.contains(sprites.get(i)
                    .getBoundingRectangle()) && !isdragged)//check if rectangle collide with sprites and select them
            {
                sprites.get(i).setColor(Color.GREEN);
                seleccionados.add(sprites.get(i));
                selected = true;
            }
        }

        if (!isdragged) {
            rec.set(-100, -100, 0, 0);
        }
    }

    @Override
    public void render() {
        sb.setProjectionMatrix(camera.combined);

        sb.begin();
        for (int i = 0; i < sprites.size; i++) {
            if (seleccionados.contains(sprites.get(i), true))
                continue;
            sprites.get(i).setBounds(i * 110, 100, 100, 100);
            sprites.get(i).draw(sb);
        }

        for (int i = 0; i < seleccionados.size; i++) {//array of sprite that has been selected

            if (!isdragged)
                seleccionados.get(i).setBounds(seleccionados.get(i).getX(), seleccionados.get(i).getY(), 100, 100);
            else
                seleccionados.get(i).setBounds(x2 - seleccionados.get(i).getWidth() / 2, y2 - seleccionados.get(i)
                        .getHeight() / 2, 100, 100);//move the selected sprites , only work for one :c

            seleccionados.get(i).draw(sb);
        }
        sb.end();


        renderer.setProjectionMatrix(camera.combined);
        renderer.begin(ShapeType.Line);
        renderer.setColor(Color.GREEN);
        if (isdragged && !selected) {
            float px = Math.min(x1, x2);
            float py = Math.min(y1, y2);
            float pw = Math.abs(x1 - x2);
            float ph = Math.abs(y1 - y2);

            rec.set(px, py, pw, ph);
            renderer.rect(px, py, pw, ph);
        }
        renderer.end();
    }

    @Override
    public void dispose() {
        tex.dispose();
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 vector = new Vector3(screenX, screenY, 0);
        vector = camera.unproject(vector);
        x1 = vector.x;
        y1 = vector.y;
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        isdragged = false;

        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        Vector3 vector = new Vector3(screenX, screenY, 0);
        vector = camera.unproject(vector);
        x2 = vector.x;
        y2 = vector.y;
        isdragged = true;

        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}
\$\endgroup\$
0
\$\begingroup\$

It is because you assign each selected Sprite the same coordinates and they end up being drawn on top of each other:

seleccionados.get(i).setBounds(x2 - seleccionados.get(i).getWidth() / 2, y2 - seleccionados.get(i)
                    .getHeight() / 2, 100, 100);

Each sprite in seleccionados has the same width and height, so x2 - seleccionados.get(i).getWidth() always evaluates to the same number.

Also, here are multiple suspicious things / tips I noticed in this code:

  • While you add to seleccionados on every frame, you never clear it, so each frame will be a little slower until the game crashes as it runs out of memory
  • You don't have to do seleccionados.get(i).setBounds(seleccionados.get(i).getX(), seleccionados.get(i).getY(), 100, 100);, Sprite has setSize(float, float) method which does the same. But do you really need to set the size on each frame?
  • Using for (Sprite selected : seleccionados) (for-each loop) instead of for (int i = 0; i < seleccionados.size; i++) will make the code more readable
|improve this answer|||||
\$\endgroup\$

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.