# Problem with for each cicles

• March 25th, 2013, 07:37 PM
Entertainer
Problem with for each cicles
Hi guys
i have a problem, first i have two cicles for each to print a line like a cross to an object, thats actualize the line when the object moves, my problem is when i got two objects pointing to the same object, the cross line of the first targeting him, dont desapears from the map and i dont know why.

Thanks
Entertainer
• March 25th, 2013, 08:06 PM
Norm
Re: Problem with for each cicles
Please post the code you are having problems with, post what it prints out and explain what is wrong with the printout.
• March 25th, 2013, 08:20 PM
Entertainer
Re: Problem with for each cicles
Attachment 1935

for (SimpleImage tower : towers) {
for (MovingImage x : invaders) {
if (Math.abs(tower.getPosition().distance(x.getPositi on())) < RANGE) {

SimpleShape s = new SimpleShape(
new Line2D.Double(tower.getPosition().getX()
+ tower.getDimension().width / 2, tower
.getPosition().getY()
+ tower.getDimension().height / 2, x
.getPosition().getX()
+ x.getDimension().width / 2, x
.getPosition().getY()
+ x.getDimension().height / 2), Color.blue, 50);

shot = s;

}
}
}
}
• March 25th, 2013, 08:25 PM
Norm
Re: Problem with for each cicles
How can the code by compiled and executed for testing? Can you make a small simple program that compiles, executes and shows the problem.

Be sure to wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• March 26th, 2013, 11:34 AM
Entertainer
Re: Problem with for each cicles
Code java:

```if (shot != null) window.getDisplayer().remove(shot); for (SimpleImage tower : towers) { for (MovingImage x : invaders) {   if (Math.abs(tower.getPosition().distance(x.getPosition())) < RANGE) {   // if (shooting == true) { // to target one target at once     System.out.println(shot); SimpleShape s = new SimpleShape( new Line2D.Double(tower.getPosition().getX() + tower.getDimension().width / 2, tower .getPosition().getY() + tower.getDimension().height / 2, x .getPosition().getX() + x.getDimension().width / 2, x .getPosition().getY() + x.getDimension().height / 2), Color.green, 100); window.getDisplayer().refresh(); shot = s;   window.getDisplayer().remove(shot);   shooting = false;   window.getDisplayer().add(s);   }   } }```
My objective is target two objects at the same time( two or more towers pointing at objects if they are in range), i think the problem is when we change the tower s(variable) turns null but i dont know why...

Image:Attachment 1936
• March 26th, 2013, 11:44 AM
Norm
Re: Problem with for each cicles
Can you make a small simple program that compiles, executes and shows the problem.

[code=java]
[/code]
to get highlighting and preserve formatting.

Quote:

when we change the tower s(variable) turns null
Where does the null value come from?
• March 26th, 2013, 12:08 PM
Entertainer
Re: Problem with for each cicles
i think when two objects tower change the value of variable s, my ideia is dont let second tower change s value but i dont know how can i do it ;/
• March 26th, 2013, 12:23 PM
Norm
Re: Problem with for each cicles
Quote:

dont let second tower change s value
How do you know whether it is the first tower or the second tower? Can you use that information to stop second tower from making any changes.
• March 26th, 2013, 01:21 PM
Entertainer
Re: Problem with for each cicles
thats is the problem i dont know which tower acess to variable s and i dont know how to restrict that ;/
• March 26th, 2013, 01:29 PM
Norm
Re: Problem with for each cicles
Without a SSCCE there isn't much more I can do.