문제 설명
Java 경로 지정 foreach 루프 (Java pathing foreach loop)
I'm trying to get my "Monsters" to move through an arraylist of "PathingCoordinates", by setting the individual Monsters "chasePoint". But everytime I add more then 1 monster, they run around the JPanel in all sorts of directions. (My guess; taking the fastest route to the next "chasePoint")
Since I'm new to java, and first time user of the foreach loop, I'd appriciate if someone would point me in the direction where my logic fails.
for (Monster i : Monstre)
{
this.add(i); //Adds all monsters from the "Monstre" arrayList
i.setBounds(i.monsterx, i.monstery, Monster.img0.getIconWidth(), Monster.img0.getIconHeight());
if(i.monstery == i.chasePoint.getY() && i.monsterx == i.chasePoint.getX() ){
p++;
i.chasePoint = PathingCoordinates.get(p);
}
if(i.monsterx < i.chasePoint.getX()){
i.monsterx++;
}else if(i.monstery < i.chasePoint.getY()){
i.monstery++;
}else if(i.monsterx > i.chasePoint.getX()){
i.monsterx‑‑;
}else if(i.monstery > i.chasePoint.getY()){
i.monstery‑‑;
}
}
Apologies if more info is needed, please request.
Edit: There's a delay between each monster spawn. First monster chases the correct "chasePoint" next mobs, seems to move randomly.
참조 솔루션
방법 1:
Created variable "p" in the Monster‑Class definition Credits goes to Lolsvemir & Heuster.
(by Jesper Tuborg Madsen、Jesper Tuborg Madsen)