| View previous topic :: View next topic |
| Author |
Message |
Degen
Joined: 23 Sep 2007 Posts: 16
Digg It |
Posted: Sat Mar 01, 2008 9:31 am Post subject: IR |
|
|
Hi this question doesn't really concern WiiremoteJ. In the test application at the IRInputReceived im not sure what the : in the loop does.
for (IRLight light : evt.getIRLights())
Is it a logic operator?
I did it like this but your code is mush nicer so I want to understand it
Thx for answers!
light = new IRLight[4];
light = evt.getIRLights();
System.out.println("X:");
for(int i=0; i<4 && light[i] != null; i++){
System.out.println("X: "+light[i].getX());
System.out.println("Y: "+light[i].getY());
} |
|
| Back to top |
|
 |
guiguito
Joined: 16 Dec 2007 Posts: 14
Digg It |
Posted: Sat Mar 01, 2008 11:48 am Post subject: |
|
|
if i'm correct, his code means his iterating over the lights.
it is exactly what you do, instead light in his loop his equal to light[i] in your loop.
His way is much nicer  |
|
| Back to top |
|
 |
Cha0s Site Admin
Joined: 17 Jan 2007 Posts: 421
Digg It |
Posted: Sat Mar 01, 2008 1:09 pm Post subject: |
|
|
Hehe. Yep, as of Java 5, there's a nice new for-each loop. Just be careful using it with threading. It works by creating an Iterator and Iterators aren't thread-safe.  _________________ Cha0s |
|
| Back to top |
|
 |
Degen
Joined: 23 Sep 2007 Posts: 16
Digg It |
Posted: Sat Mar 01, 2008 1:26 pm Post subject: |
|
|
Ok thx!
So you dont have to define the length before starting the loop? Will it loop for every IRLight objekt found in the event and store it in light?
Have I understod this right? |
|
| Back to top |
|
 |
Cha0s Site Admin
Joined: 17 Jan 2007 Posts: 421
Digg It |
Posted: Sat Mar 01, 2008 9:51 pm Post subject: |
|
|
Yep. It goes through each element in Iterable (that's an interface that means the thing can be iterated through. Arrays, ArrayLists, etc are all Iterable) and puts the value in light. Then you do something with it and it puts the next value in light, etc. until it goes through all the values in the Iterable. _________________ Cha0s |
|
| Back to top |
|
 |
Shuzz
Joined: 14 Jan 2008 Posts: 12
Digg It |
Posted: Tue Mar 04, 2008 8:19 am Post subject: Re: IR |
|
|
| Degen wrote: | light = new IRLight[4];
light = evt.getIRLights();
System.out.println("X:");
for(int i=0; i<4 && light[i] != null; i++){
System.out.println("X: "+light[i].getX());
System.out.println("Y: "+light[i].getY());
} |
Careful with that code: It is possible that e.g. when the Wiimote "sees" three lights that the second element in the array is null and the other three aren't.
Your code would stop going over the array when hitting the first null element and therefore might miss out on some IRLights. |
|
| Back to top |
|
 |
Degen
Joined: 23 Sep 2007 Posts: 16
Digg It |
Posted: Wed Mar 05, 2008 11:29 am Post subject: |
|
|
I know,I dont use that any more it was my first try
Thx for the tip! |
|
| Back to top |
|
 |
|