Java 경로 지정 foreach 루프 (Java pathing foreach loop)


문제 설명

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 MadsenJesper Tuborg Madsen)

참조 문서

  1. Java pathing foreach loop (CC BY‑SA 3.0/4.0)

#path-finding #foreach #java






관련 질문

그리드 셀을 다시 방문하지 않고 그리드에서 가장 긴 경로 (Longest path on a grid, without revisiting grid cells)

Java 경로 지정 foreach 루프 (Java pathing foreach loop)

A* Admissible Heuristic for die rolling on grid (A* Admissible Heuristic for die rolling on grid)

2차원 배열의 타일 연결 알고리즘 (Algorithm for tile connection in a two-dimensional array)

무방향 그래프에서 노드에 대한 최단 경로 찾기 (Finding the shortest path to a node in an undirected graph)

논리적으로 최단 경로 결정 (Logically determine shortest path)

NavMesh로 더 가까운 플레이어 찾기 (Finding the closer player with NavMesh)

최단 경로 알고리즘: 다중 소스, 가장 가까운 목적지 (Shortest-path algorithm: multiple source, closest destination)

절차적으로 생성된 던전에서 AI의 길 찾기를 수정하는 방법은 무엇입니까? (How to fix pathfinding for AI in a procedural generated dungeon?)

NavMeshAgent는 Unity의 대상 지점과 같은 방향을 향합니다. (NavMeshAgent face same direction as destination point in Unity)

여러 차량에 대한 Unity 경로 찾기 (Unity Path Finding for Multiple Vehicles)

걸을 수 있는 타일 위에 있는 장애물 사이의 길 찾기 (Pathfinding between obstacles that sit on top of walkable tiles)







코멘트