Results 1 to 7 of 7
Thread: Listening for variables?
- 02-09-2010, 01:52 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
Listening for variables?
Hi,
At the moment if I want to detect a change in a variable, I use a thread to loop, and then check if the variable is different from itself one loop before. I'm sure this isn't the most efficient way to do this though.
Is it possible to listen for variable changes like you can listen for mouse clicks?
Thanks.
- 02-09-2010, 02:15 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
I'm not quite sure what you mean by this, but you could start a separate thread that run a while loop checking if the variable is equal to it's instance of the variable. so if one changes, the loop will replace its variable with the real one and run a method you want to run.Is it possible to listen for variable changes like you can listen for mouse clicks?
Thanks.
- 02-09-2010, 02:19 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
Hmmm that's similar to what I'm already doing, but I guess that's the way I'll do it then.
Thank you!
- 02-09-2010, 02:44 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
Say if I had 20 buttons, would it be alright to use a thread for each of them? Do they take up much of the systems resources, (are there a limited number of threads I can use?)
- 02-09-2010, 05:17 AM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
why not just have 1 thread that uses a while(true) loop and an if statement for each button?
-
Rather than talk about a specific tactic of "listening to a variable", why not tell us your overall strategy here. Just what are you trying to achieve by this? Tell us this, and I'll bet we'll find a better way to solve it.
- 02-09-2010, 09:14 AM #7
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
It's alright now, I've fixed it. I thought that testing lots of variables from my main JPanel in a loop was drastically slowing down my game. I went around in a circle and ended up back where I started - testing for variables in my main JPanel. It wasn't this that was slowing down my game, it was the screen fader.
I had a large black rectangle who's alpha value decreased and was constantly being repainted. I fixed this up, so that it was only repainted if the alpha value changed. Now this went from chewing up 100% of my CPU to just 7%.
I guess repainting takes up a heap more of the processor power than I thought. I would have thought though, that because this is a simple 2D game that it wouldn't take up so much.
Am I doing something wrong, or is this just a limitation of Java?
Similar Threads
-
MDB listening
By nipunalk in forum Advanced JavaReplies: 5Last Post: 01-20-2010, 05:10 AM -
NIO Server: listening on client's side socket
By carek in forum NetworkingReplies: 1Last Post: 12-24-2009, 07:30 AM -
Listening to SMS, which is send without any port number
By uskris in forum CLDC and MIDPReplies: 7Last Post: 11-25-2009, 11:34 PM -
Java server listening on given interface?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-26-2009, 05:14 PM -
Listening to a Printer
By Mosd in forum New To JavaReplies: 0Last Post: 08-12-2008, 12:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks