Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Mouse Clicked error

    What it's supposed to do:
    This code is supposed to see if the clicked card is selected. If it's not, select it and show the player where it can move / attack. If it is selected, then find out what card the player clicked on. If he clicked on the card that was already selected, deselect it. If he clicks on a friendly card (same playerID), then select that card. If he clicks on an enemy card (different playerID), attack that card. If he clicks on a tile, move it to that tile.

    The problem:
    However, line 24-58 give me a bit of a problem. Right now, it works fine BECAUSE lines 46-58 are commented (which also means the unit can't move). If I uncomment those lines, the unit becomes able to move, however, lines 34-46 cease working... Any help will be greatly appreciated.

    Java Code:
    @Override
    	public void mouseClicked(MouseEvent e) {
    		mainloop: for (int i = 0; i < Main.cards.size(); i++) {
    			Card c = (Card) Main.cards.get(i);
    
    			if (Card.cardSelected == null) {// Contains / Is not selected
    				if (c.contains(e.getX(), e.getY())) {
    					Tile.reset();
    					Card.reset();
    					Card.cardSelected = c;
    
    					Card.checkUnitMvtRange();
    					Card.checkUnitAtkRange();
    
    					break mainloop;
    				}
    			} else if (Card.cardSelected != null) {
    				if (c.contains(e.getX(), e.getY()) && c == Card.cardSelected) {
    					Tile.reset();
    					Card.reset();
    					Card.cardSelected = null;
    
    					break mainloop;
    				} else if (c.contains(e.getX(), e.getY()) && c != Card.cardSelected) {
    					if (c.contains(e.getX(), e.getY()) && c.playerID != Card.cardSelected.playerID){
    						System.out.println("runs here =----======================");
    						Card.attackUnit(e.getX(), e.getY());
    
    						Tile.reset();
    						Card.reset();
    						Card.cardSelected = null;
    
    						break mainloop;
    					} else {
    						System.out.println("H");
    						Tile.reset();
    						Card.reset();
    						Card.cardSelected = c;
    
    						Card.checkUnitMvtRange();
    						Card.checkUnitAtkRange();
    
    						break mainloop;
    					}
    				} else {
    					/*for (int a = 0; a < Main.GRID_X; a++) {
    						for (int b = 0; b < Main.GRID_Y; b++) {
    							if (Tile.tile[a][b].contains(e.getX(), e.getY())) {
    								System.out.println("tile clickedere =----======================");
    								Card.moveUnit(e.getX(), e.getY());
    							}
    						}
    					}
    					Tile.reset();
    					Card.reset();
    					Card.cardSelected = null;
    					
    					break mainloop;*/
    				}
    			}
    		}
    	}

  2. #2
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Mouse Clicked error

    What is with the sources of the classes Tile and Card?
    You should show them here, they are both called in the part that is not working correctly.

    The next point is that I would change the structure of your method.
    Remove function from your loop and move it to seperated methods.

    Java Code:
    public void mouseClicked(MouseEvent e) {
        mainloop: for (int i = 0; i < Main.cards.size(); i++) {
            Card c = (Card) Main.cards.get(i);
    
            if (c.contains(e.getX(), e.getY())) {
                if (Card.cardSelected == null || c != Card.cardSelected) {
                    selectCard(c);
                    break mainloop;
                } else if (c == Card.cardSelected) {
                    deselectCard();
                    break mainloop;
                } else if (c.playerID != Card.cardSelected.playerID) {
                    attackCard(e);
                    break mainloop;
                }
            } else if(Card.cardSelected != null) {
                moveCard(e)
                break mainloop;
            }
        }
    }
    
    public void selectCard(Card c) {
        Tile.reset();
        Card.reset();
        Card.cardSelected = c;
     
        Card.checkUnitMvtRange();
        Card.checkUnitAtkRange();
    }
    
    public void deselectCard() {
        Tile.reset();
        Card.reset();
        Card.cardSelected = null;
    }
    
    public void attackCard(MouseEvent e) {
        Card.attackUnit(e.getX(), e.getY());
        deselectCard();
    }
    
    public void moveCard(MouseEvent e) {
        for (int a = 0; a < Main.GRID_X; a++) {
            for (int b = 0; b < Main.GRID_Y; b++) {
                if (Tile.tile[a][b].contains(e.getX(), e.getY())) {
                    Card.moveUnit(e.getX(), e.getY());
                }
            }
        }
        deselectCard();
    }

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Mouse Clicked error

    AH thanks a lot. That's a great idea. Before, even the attack and move functions were all included in the loop and it was huge. :P
    Anyway, whether
    Java Code:
                            Tile.reset();
                            Card.reset();
                            Card.cardSelected = c;
     
                            Card.checkUnitMvtRange();
                            Card.checkUnitAtkRange();
    are included or not, it doesn't work if the next part is uncommented. Do you still think it could be a problem with card / tile?

    Even if I change it to:
    it won't work.
    Java Code:
    	public void mouseClicked(MouseEvent e) {
    		mainloop: for (int i = 0; i < Main.cards.size(); i++) {
    			Card c = (Card) Main.cards.get(i);
    
    			if (Card.cardSelected == null) {// Contains / Is not selected
    				if (c.contains(e.getX(), e.getY())) {
    					Tile.reset();
    					Card.reset();
    					Card.cardSelected = c;
    
    					Card.checkUnitMvtRange();
    					Card.checkUnitAtkRange();
    
    					break mainloop;
    				}
    			} else if (Card.cardSelected != null) {
    				if (c.contains(e.getX(), e.getY()) && c == Card.cardSelected) {
    					Tile.reset();
    					Card.reset();
    					Card.cardSelected = null;
    
    					break mainloop;
    				} else if (c.contains(e.getX(), e.getY()) && c != Card.cardSelected) {
    					if (c.contains(e.getX(), e.getY()) && c.playerID != Card.cardSelected.playerID){
    						System.out.println("runs here =----======================");
    						Card.attackUnit(e.getX(), e.getY());
    
    						Tile.reset();
    						Card.reset();
    						Card.cardSelected = null;
    
    						break mainloop;
    					} else {
    						System.out.println("H");
    						break mainloop;
    					}
    				} else {
    					for (int a = 0; a < Main.GRID_X; a++) {
    						for (int b = 0; b < Main.GRID_Y; b++) {
    							if (Tile.tile[a][b].contains(e.getX(), e.getY())) {
    								System.out.println("tile clickedere =----======================");
    								Card.moveUnit(e.getX(), e.getY());
    							}
    						}
    					}
    					Tile.reset();
    					Card.reset();
    					Card.cardSelected = null;
    					
    					break mainloop;
    				}
    			}
    		}
    	}

  4. #4
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Mouse Clicked error

    I think the problem could be within the following part:
    Java Code:
    for (int a = 0; a < Main.GRID_X; a++) {
        for (int b = 0; b < Main.GRID_Y; b++) {
            if (Tile.tile[a][b].contains(e.getX(), e.getY())) {
                System.out.println("tile clickedere =----======================");
                Card.moveUnit(e.getX(), e.getY());
            }
        }
    }
    This part also contains the Card.moveUnit so it is possible that the problem is there.

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Mouse Clicked error

    I'll look into it. It's strange. Basically, if you click on a card with the same playerID, it's just supposed to select the new card that was clicked on.

  6. #6
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Mouse Clicked error

    Quote Originally Posted by Tobias Kürbis View Post
    I think the problem could be within the following part:
    Java Code:
    for (int a = 0; a < Main.GRID_X; a++) {
        for (int b = 0; b < Main.GRID_Y; b++) {
            if (Tile.tile[a][b].contains(e.getX(), e.getY())) {
                System.out.println("tile clickedere =----======================");
                Card.moveUnit(e.getX(), e.getY());
            }
        }
    }
    This part also contains the Card.moveUnit so it is possible that the problem is there.
    I changed the code to this for now:

    Java Code:
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		mainloop: for (int i = 0; i < Main.cards.size(); i++) {
    			Card c = (Card) Main.cards.get(i);
    
    			if (c.contains(e.getX(), e.getY())){
    				System.out.println("CONTAINS PPPPPPPPPPPPPPPPPPPPPPPPP + " + c);
    			}
    			
    			if (c.contains(e.getX(), e.getY())) {
    				if (Card.cardSelected == null) { // Add 'if playerID == player's playerID'.
    					selectCard(c);
    					break mainloop;
    				} else if (c == Card.cardSelected) {
    					deselectCard();
    					break mainloop;
    				} else if (c.playerID != Card.cardSelected.playerID) {
    					attackCard(e);
    					break mainloop;
    				} else if (c.playerID == Card.cardSelected.playerID) {
    					System.out.println("!");
    					attackCard(e);
    					break mainloop;
    				}
    			} else if (Card.cardSelected != null) {
    				System.out.println("Runs");
    				moveCard(e);
    				break mainloop;
    			}
    		}
    	}
    And the 2nd tiem I click on a card, it just deselects the selected card and the message " if (c.contains(e.getX(), e.getY())){
    System.out.println("CONTAINS PPPPPPPPPPPPPPPPPPPPPPPPP + " + c);
    }" doesn't display.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Mouse Clicked error

    What are the bounds for c and what are the values returned by getX and getY? Print them all to see why.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Mouse Clicked error

    I found the problem (part of it). Tobias' answer has "break mainloop" at the bottom and this causes the loop to stop after the first iteration if the clicked card is not 1st in the array.
    Removing "break mainloop" fixes it. HOWEVER, it brings in another problem: after the unit is moved, it will automatically be reselected (for reasons yet unknown to me).

Similar Threads

  1. Replies: 9
    Last Post: 05-21-2014, 08:26 PM
  2. Replies: 5
    Last Post: 04-23-2014, 07:49 PM
  3. Replies: 2
    Last Post: 07-13-2012, 11:59 PM
  4. Replies: 4
    Last Post: 02-23-2011, 10:25 PM
  5. X & Y of the following event "Mouse clicked"
    By jack DANIEL's in forum Advanced Java
    Replies: 2
    Last Post: 10-12-2010, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •