Handling events at LIBGDX. The event is registered in two place at the same time











up vote
1
down vote

favorite












Im creating a turn base-fight game and I have the problem that I want to create a button where you can click it and then it need to click on the target enemy.



So far this is the part of the code I have:



public class FightScreen extends BaseScreen {
LevelScreen previousMap;
Champion fighterOne;
boolean turn = true; //variable to set the turn of the player, if true is players turn, otherwise enemy turn
EnemyFighters enemyOne;
EnemyFighters enemyTwo;
EnemyFighters enemyThree;
boolean firstAttack = false;


public FightScreen(LevelScreen prev){
super();
previousMap = prev;
}


public void initialize() {
//initialize the mapbackground
BaseActor fightBackground = new BaseActor(0,0, mainStage);
fightBackground.loadTexture( "assets/img/dungeon.png" );
fightBackground.setSize(800,600);
//initialize the actors at the screen
fighterOne = new WarriorOne(mainStage);
enemyOne = new BatFighter(mainStage);
enemyTwo = new SkeletonFighter(mainStage);
enemyThree = new ZombieFighter(mainStage);


//create the buttons
uiStage.addActor(fighterOne.getFirstButton());


fighterOne.getFirstButton().addListener(
(Event e) ->
{

if ( !(e instanceof InputEvent) )
return false;

if ( !((InputEvent)e).getType().equals(InputEvent.Type.touchDown) )
return false;
if (!(turn))
return false;
firstAttack = true;
System.out.println("we need a target please!");
return true;
}
);
enemyTwo.addListener(
(Event o) ->
{
if ( !(o instanceof InputEvent) )
return false;

if ( !((InputEvent)o).getType().equals(InputEvent.Type.touchDown) ){
return false;
}
if (firstAttack){
fighterOne.attackOne(enemyTwo);
firstAttack = false;
System.out.println("we delivered the attack! viva");
turn = !turn;
}else
return false;

return true;
}
);

}

public void update(float dt) {
if (!(turn)){
enemyOne.attackOne(fighterOne);
turn = !(turn);
System.out.println("we got attacked by enemy");
System.out.println("Our hero health is: "+ fighterOne.getHP());

}
if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
System.out.println("setting attack to false");
firstAttack = false;
}
}


}



My issue is that when I press LEFT click in the mouse, both the eventListener for fighterOne and the listener in update are beeing call.



Is there a way to only listen a Leftclick when no object are beeing clicked in the update method of the class?



Thanks in advance.










share|improve this question




























    up vote
    1
    down vote

    favorite












    Im creating a turn base-fight game and I have the problem that I want to create a button where you can click it and then it need to click on the target enemy.



    So far this is the part of the code I have:



    public class FightScreen extends BaseScreen {
    LevelScreen previousMap;
    Champion fighterOne;
    boolean turn = true; //variable to set the turn of the player, if true is players turn, otherwise enemy turn
    EnemyFighters enemyOne;
    EnemyFighters enemyTwo;
    EnemyFighters enemyThree;
    boolean firstAttack = false;


    public FightScreen(LevelScreen prev){
    super();
    previousMap = prev;
    }


    public void initialize() {
    //initialize the mapbackground
    BaseActor fightBackground = new BaseActor(0,0, mainStage);
    fightBackground.loadTexture( "assets/img/dungeon.png" );
    fightBackground.setSize(800,600);
    //initialize the actors at the screen
    fighterOne = new WarriorOne(mainStage);
    enemyOne = new BatFighter(mainStage);
    enemyTwo = new SkeletonFighter(mainStage);
    enemyThree = new ZombieFighter(mainStage);


    //create the buttons
    uiStage.addActor(fighterOne.getFirstButton());


    fighterOne.getFirstButton().addListener(
    (Event e) ->
    {

    if ( !(e instanceof InputEvent) )
    return false;

    if ( !((InputEvent)e).getType().equals(InputEvent.Type.touchDown) )
    return false;
    if (!(turn))
    return false;
    firstAttack = true;
    System.out.println("we need a target please!");
    return true;
    }
    );
    enemyTwo.addListener(
    (Event o) ->
    {
    if ( !(o instanceof InputEvent) )
    return false;

    if ( !((InputEvent)o).getType().equals(InputEvent.Type.touchDown) ){
    return false;
    }
    if (firstAttack){
    fighterOne.attackOne(enemyTwo);
    firstAttack = false;
    System.out.println("we delivered the attack! viva");
    turn = !turn;
    }else
    return false;

    return true;
    }
    );

    }

    public void update(float dt) {
    if (!(turn)){
    enemyOne.attackOne(fighterOne);
    turn = !(turn);
    System.out.println("we got attacked by enemy");
    System.out.println("Our hero health is: "+ fighterOne.getHP());

    }
    if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
    System.out.println("setting attack to false");
    firstAttack = false;
    }
    }


    }



    My issue is that when I press LEFT click in the mouse, both the eventListener for fighterOne and the listener in update are beeing call.



    Is there a way to only listen a Leftclick when no object are beeing clicked in the update method of the class?



    Thanks in advance.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Im creating a turn base-fight game and I have the problem that I want to create a button where you can click it and then it need to click on the target enemy.



      So far this is the part of the code I have:



      public class FightScreen extends BaseScreen {
      LevelScreen previousMap;
      Champion fighterOne;
      boolean turn = true; //variable to set the turn of the player, if true is players turn, otherwise enemy turn
      EnemyFighters enemyOne;
      EnemyFighters enemyTwo;
      EnemyFighters enemyThree;
      boolean firstAttack = false;


      public FightScreen(LevelScreen prev){
      super();
      previousMap = prev;
      }


      public void initialize() {
      //initialize the mapbackground
      BaseActor fightBackground = new BaseActor(0,0, mainStage);
      fightBackground.loadTexture( "assets/img/dungeon.png" );
      fightBackground.setSize(800,600);
      //initialize the actors at the screen
      fighterOne = new WarriorOne(mainStage);
      enemyOne = new BatFighter(mainStage);
      enemyTwo = new SkeletonFighter(mainStage);
      enemyThree = new ZombieFighter(mainStage);


      //create the buttons
      uiStage.addActor(fighterOne.getFirstButton());


      fighterOne.getFirstButton().addListener(
      (Event e) ->
      {

      if ( !(e instanceof InputEvent) )
      return false;

      if ( !((InputEvent)e).getType().equals(InputEvent.Type.touchDown) )
      return false;
      if (!(turn))
      return false;
      firstAttack = true;
      System.out.println("we need a target please!");
      return true;
      }
      );
      enemyTwo.addListener(
      (Event o) ->
      {
      if ( !(o instanceof InputEvent) )
      return false;

      if ( !((InputEvent)o).getType().equals(InputEvent.Type.touchDown) ){
      return false;
      }
      if (firstAttack){
      fighterOne.attackOne(enemyTwo);
      firstAttack = false;
      System.out.println("we delivered the attack! viva");
      turn = !turn;
      }else
      return false;

      return true;
      }
      );

      }

      public void update(float dt) {
      if (!(turn)){
      enemyOne.attackOne(fighterOne);
      turn = !(turn);
      System.out.println("we got attacked by enemy");
      System.out.println("Our hero health is: "+ fighterOne.getHP());

      }
      if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
      System.out.println("setting attack to false");
      firstAttack = false;
      }
      }


      }



      My issue is that when I press LEFT click in the mouse, both the eventListener for fighterOne and the listener in update are beeing call.



      Is there a way to only listen a Leftclick when no object are beeing clicked in the update method of the class?



      Thanks in advance.










      share|improve this question















      Im creating a turn base-fight game and I have the problem that I want to create a button where you can click it and then it need to click on the target enemy.



      So far this is the part of the code I have:



      public class FightScreen extends BaseScreen {
      LevelScreen previousMap;
      Champion fighterOne;
      boolean turn = true; //variable to set the turn of the player, if true is players turn, otherwise enemy turn
      EnemyFighters enemyOne;
      EnemyFighters enemyTwo;
      EnemyFighters enemyThree;
      boolean firstAttack = false;


      public FightScreen(LevelScreen prev){
      super();
      previousMap = prev;
      }


      public void initialize() {
      //initialize the mapbackground
      BaseActor fightBackground = new BaseActor(0,0, mainStage);
      fightBackground.loadTexture( "assets/img/dungeon.png" );
      fightBackground.setSize(800,600);
      //initialize the actors at the screen
      fighterOne = new WarriorOne(mainStage);
      enemyOne = new BatFighter(mainStage);
      enemyTwo = new SkeletonFighter(mainStage);
      enemyThree = new ZombieFighter(mainStage);


      //create the buttons
      uiStage.addActor(fighterOne.getFirstButton());


      fighterOne.getFirstButton().addListener(
      (Event e) ->
      {

      if ( !(e instanceof InputEvent) )
      return false;

      if ( !((InputEvent)e).getType().equals(InputEvent.Type.touchDown) )
      return false;
      if (!(turn))
      return false;
      firstAttack = true;
      System.out.println("we need a target please!");
      return true;
      }
      );
      enemyTwo.addListener(
      (Event o) ->
      {
      if ( !(o instanceof InputEvent) )
      return false;

      if ( !((InputEvent)o).getType().equals(InputEvent.Type.touchDown) ){
      return false;
      }
      if (firstAttack){
      fighterOne.attackOne(enemyTwo);
      firstAttack = false;
      System.out.println("we delivered the attack! viva");
      turn = !turn;
      }else
      return false;

      return true;
      }
      );

      }

      public void update(float dt) {
      if (!(turn)){
      enemyOne.attackOne(fighterOne);
      turn = !(turn);
      System.out.println("we got attacked by enemy");
      System.out.println("Our hero health is: "+ fighterOne.getHP());

      }
      if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
      System.out.println("setting attack to false");
      firstAttack = false;
      }
      }


      }



      My issue is that when I press LEFT click in the mouse, both the eventListener for fighterOne and the listener in update are beeing call.



      Is there a way to only listen a Leftclick when no object are beeing clicked in the update method of the class?



      Thanks in advance.







      java events libgdx mouseevent






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 0:30

























      asked Nov 19 at 0:13









      Patrick Vibild

      3618




      3618





























          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',
          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%2f53366778%2fhandling-events-at-libgdx-the-event-is-registered-in-two-place-at-the-same-time%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366778%2fhandling-events-at-libgdx-the-event-is-registered-in-two-place-at-the-same-time%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

          Ottavio Pratesi

          Tricia Helfer

          15 giugno