• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with Action listener?

 
Melvin Guntanis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I am making a chess game but only with the knight.my problem is that when I move the piece the first time a new knights appear were I could have move it before. So I was thinking maybe if i remove the actionlistener inside the action perform method i could fix this. but it didn't. Do any 1 know how can I rewrite the code without having to addActionlisteners every time I run it? Please help im desperate Here is the methods related to this. Any kind of help is welcome.

here are the methods
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I can see, you add listeners for each button to any other button. It is wrong. And you removeActionListener action does nothing, because you create new listener instances, which are not equal to any one added (they use reference identity in your case).

You should:
1. Attach one and only one listener for each button. That listener will listen for a button click and process a move when knight can go to a specified point. So, point 2.
2. Store knight's position in a variables. You can check when move is correct or not. Don't use event source (it always will be a button where knight should move) and you know coordinates of that button (you can pass them in a listener creation function). Use knight position variables. And after a move update the variables first and only then update button states/icons.
 
Piet Souris
Rancher
Posts: 1295
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions about chess are quite frequent on this forum. To get some ideas how you can implement Maxims
advises, search this forum for the word "chess", and you will find many ways that are helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic