How to drag sprites in libgdx












1















I'm making a card game and take pictures from the atlas. How to do to take any card from your hand with your finger and drag (drag and drop). I made it through isTouched for two cards, but this is not what is needed



@Override
public void create () {

touchPos1 = new Vector3();
touchPos2 = new Vector3();
batch = new SpriteBatch();


one = new Rectangle();
one.x = 640;
one.y = 20;
one.width = 64;
one.height = 92;

two = new Rectangle();
two.x = 688;
two.y = 20;
two.width = 64;
two.height = 92;


camera = new OrthographicCamera();
camera.setToOrtho(false, 1280, 720); //** w/h ratio = 1.66 **//
batch = new SpriteBatch();
cardAtlas = new TextureAtlas("carddd.pack"); //** Load circles.pack and circles.png **//
redZero = new TextureRegion(cardAtlas.findRegion("0R")); //** Load redCircle from circleAtlas **//
greenFive = new TextureRegion(cardAtlas.findRegion("5G"));
}

@Override
public void render () {
Gdx.gl.glClearColor(0, 1, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();

batch.draw(redZero, one.x, one.y,100,141); //** draw Red circle **//
batch.draw(greenFive, two.x, two.y,100,141);

batch.end();


Here I am in the process of dragging



    if(Gdx.input.isTouched()){
touchPos1.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos1);
if(one.x+64 > touchPos1.x && touchPos1.x>one.x-64){
one.x = (int) (touchPos1.x);
one.y = (int) (touchPos1.y);
}

else if(Gdx.input.isTouched()){
touchPos2.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos2);
if(two.x+64 > touchPos2.x && touchPos2.x>two.x-64) {
two.x = (int) (touchPos2.x);
two.y = (int) (touchPos2.y);
}}









share|improve this question





























    1















    I'm making a card game and take pictures from the atlas. How to do to take any card from your hand with your finger and drag (drag and drop). I made it through isTouched for two cards, but this is not what is needed



    @Override
    public void create () {

    touchPos1 = new Vector3();
    touchPos2 = new Vector3();
    batch = new SpriteBatch();


    one = new Rectangle();
    one.x = 640;
    one.y = 20;
    one.width = 64;
    one.height = 92;

    two = new Rectangle();
    two.x = 688;
    two.y = 20;
    two.width = 64;
    two.height = 92;


    camera = new OrthographicCamera();
    camera.setToOrtho(false, 1280, 720); //** w/h ratio = 1.66 **//
    batch = new SpriteBatch();
    cardAtlas = new TextureAtlas("carddd.pack"); //** Load circles.pack and circles.png **//
    redZero = new TextureRegion(cardAtlas.findRegion("0R")); //** Load redCircle from circleAtlas **//
    greenFive = new TextureRegion(cardAtlas.findRegion("5G"));
    }

    @Override
    public void render () {
    Gdx.gl.glClearColor(0, 1, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    batch.draw(redZero, one.x, one.y,100,141); //** draw Red circle **//
    batch.draw(greenFive, two.x, two.y,100,141);

    batch.end();


    Here I am in the process of dragging



        if(Gdx.input.isTouched()){
    touchPos1.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(touchPos1);
    if(one.x+64 > touchPos1.x && touchPos1.x>one.x-64){
    one.x = (int) (touchPos1.x);
    one.y = (int) (touchPos1.y);
    }

    else if(Gdx.input.isTouched()){
    touchPos2.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(touchPos2);
    if(two.x+64 > touchPos2.x && touchPos2.x>two.x-64) {
    two.x = (int) (touchPos2.x);
    two.y = (int) (touchPos2.y);
    }}









    share|improve this question



























      1












      1








      1








      I'm making a card game and take pictures from the atlas. How to do to take any card from your hand with your finger and drag (drag and drop). I made it through isTouched for two cards, but this is not what is needed



      @Override
      public void create () {

      touchPos1 = new Vector3();
      touchPos2 = new Vector3();
      batch = new SpriteBatch();


      one = new Rectangle();
      one.x = 640;
      one.y = 20;
      one.width = 64;
      one.height = 92;

      two = new Rectangle();
      two.x = 688;
      two.y = 20;
      two.width = 64;
      two.height = 92;


      camera = new OrthographicCamera();
      camera.setToOrtho(false, 1280, 720); //** w/h ratio = 1.66 **//
      batch = new SpriteBatch();
      cardAtlas = new TextureAtlas("carddd.pack"); //** Load circles.pack and circles.png **//
      redZero = new TextureRegion(cardAtlas.findRegion("0R")); //** Load redCircle from circleAtlas **//
      greenFive = new TextureRegion(cardAtlas.findRegion("5G"));
      }

      @Override
      public void render () {
      Gdx.gl.glClearColor(0, 1, 0, 0);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


      camera.update();
      batch.setProjectionMatrix(camera.combined);
      batch.begin();

      batch.draw(redZero, one.x, one.y,100,141); //** draw Red circle **//
      batch.draw(greenFive, two.x, two.y,100,141);

      batch.end();


      Here I am in the process of dragging



          if(Gdx.input.isTouched()){
      touchPos1.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos1);
      if(one.x+64 > touchPos1.x && touchPos1.x>one.x-64){
      one.x = (int) (touchPos1.x);
      one.y = (int) (touchPos1.y);
      }

      else if(Gdx.input.isTouched()){
      touchPos2.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos2);
      if(two.x+64 > touchPos2.x && touchPos2.x>two.x-64) {
      two.x = (int) (touchPos2.x);
      two.y = (int) (touchPos2.y);
      }}









      share|improve this question
















      I'm making a card game and take pictures from the atlas. How to do to take any card from your hand with your finger and drag (drag and drop). I made it through isTouched for two cards, but this is not what is needed



      @Override
      public void create () {

      touchPos1 = new Vector3();
      touchPos2 = new Vector3();
      batch = new SpriteBatch();


      one = new Rectangle();
      one.x = 640;
      one.y = 20;
      one.width = 64;
      one.height = 92;

      two = new Rectangle();
      two.x = 688;
      two.y = 20;
      two.width = 64;
      two.height = 92;


      camera = new OrthographicCamera();
      camera.setToOrtho(false, 1280, 720); //** w/h ratio = 1.66 **//
      batch = new SpriteBatch();
      cardAtlas = new TextureAtlas("carddd.pack"); //** Load circles.pack and circles.png **//
      redZero = new TextureRegion(cardAtlas.findRegion("0R")); //** Load redCircle from circleAtlas **//
      greenFive = new TextureRegion(cardAtlas.findRegion("5G"));
      }

      @Override
      public void render () {
      Gdx.gl.glClearColor(0, 1, 0, 0);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


      camera.update();
      batch.setProjectionMatrix(camera.combined);
      batch.begin();

      batch.draw(redZero, one.x, one.y,100,141); //** draw Red circle **//
      batch.draw(greenFive, two.x, two.y,100,141);

      batch.end();


      Here I am in the process of dragging



          if(Gdx.input.isTouched()){
      touchPos1.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos1);
      if(one.x+64 > touchPos1.x && touchPos1.x>one.x-64){
      one.x = (int) (touchPos1.x);
      one.y = (int) (touchPos1.y);
      }

      else if(Gdx.input.isTouched()){
      touchPos2.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos2);
      if(two.x+64 > touchPos2.x && touchPos2.x>two.x-64) {
      two.x = (int) (touchPos2.x);
      two.y = (int) (touchPos2.y);
      }}






      java android libgdx drag-and-drop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 19:27









      Squiddie

      1,0221821




      1,0221821










      asked Nov 25 '18 at 3:54









      Егор КозловЕгор Козлов

      61




      61
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464515%2fhow-to-drag-sprites-in-libgdx%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464515%2fhow-to-drag-sprites-in-libgdx%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga