2013-09-30 2 views
0

저는 파이썬에서 자바로 만든 게임을 포팅하려고합니다. 파이썬 버전에서는, 나는 하나의 "클래스"의 모든 메소드와 변수를했고, 플레이어는이 같은 사전과 같다 :다른 클래스의 기본 클래스에서 공용 변수에 액세스

game.py 플레이어의 데이터 값이 다음 별도로 추가됩니다

... 
new_player={"name":"","hp":0,...} 
players=[] 
//to add new player 
players.append(new_player.copy()) 

:

... 
players[0]["name"]="bob" 
players[0]["hp"]=50 
... 

자바 버전에서는 Player 개체와 게임의 기본 메서드를 정의하는 데 사용되는 별도의 클래스가 있습니다. 예를 들어

(이 작은 버전)

game.java (리턴 생략)

import java.utils.*; 
public class game 
{ 
    public static ArrayList<player> players = new ArrayList<player>(); 
    public static ArrayList<String> pdead = new ArrayList<String>(); 
    public static int turn = 0; 
    public static void main(String[] args) 
    { 
     //do stuff 
     players.add(new player(name)); 
     //do other stuff 
    } 
    public static void do_move(move) 
    { 
     //some move is selected 
      players.get(turn).set_hp(10); 
      //at this point compiler throws error: cannot find symbol 
      //compiler does not recognize that a player should have 
      //been added to the players variable 
     //other stuff 
    }; 
}; 

player.java (리턴 생략)

public class player 
{ 
    //arbitrary list of private variables like hp and name 
    public player(new_name) 
    { 
     name = new_name; 
     //other variables defined 
    }; 
    public void set_hp(int amount) //Adding hp 
    { 
     hp += amount; 
    }; 
    public void set_hp(int amount,String type) //taking damage 
    { 
     mana += amount; 
     //go through types, armor, etc. 
      hp -= amount; 
    }; 
    public void display stats() //displays all player's stats before choosing move 
    { 
     //display stats until... 
     //later in some for loop 
      System.out.println(players.get(index).get_hp()); 
      //here compiler throws error again: cannot find symbol 
      //players arraylist is in main class's public variables 
     //other stuff 
    }; 
    //other stuff 
}; 

기발한 때 2 개의 클래스가 함께 컴파일되면 주요 변수가 public이고 프로그램 변수가 플레이어의 변수가 정의되어 있기 때문에 프로그램을 실행할 수 있습니다. 그러나 컴파일러는 이것을 인식하지 못하고 클래스를 (동일한 디렉토리에있는) 클래스가 서로 읽지 않기 때문에 객체를 검사하는 동안 배열/arraylist에서 "정의"되지 않으므로 오류가 발생합니다.

컴파일러가 정의한 변수를 어떻게 보냅니 까? 필요한 경우 두 클래스와 최종 파이썬 버전의 현재 작업 버전을 업로드 할 수 있지만 게임을 닫힌 상태로 유지하려고합니다.

편집 : ArrayList를 초기화 고정 sjkm의 답변에 따라

+0

Java에서는'{}'대괄호를';'로 닫을 필요가 없습니다. –

답변

0

하여 목록의 제네릭 형식 정의 : 그렇지 않으면 당신은 항상에있는

public static ArrayList players = new ArrayList<player>(); 
public static ArrayList pdead = new ArrayList<String>(); 

public static ArrayList<player> players = new ArrayList<player>(); 
public static ArrayList<String> pdead = new ArrayList<String>(); 

변화를 목록에서 가져온 물건을 던지십시오.

+0

감사합니다. 메인 클래스의 오류가 수정되었지만, player.java에 이러한 변수를 표시하려면 어떻게해야합니까? 또한 빠른 답장을 보내 주셔서 감사합니다. – greatmastermario

+0

플레이어 클래스에서 정확히 액세스하려는 변수는 무엇입니까? – sjkm

+0

플레이어 ArrayList는 통계를 표시하고 players.size()를 기반으로 한 상태 효과를 기반으로 hp를 추가하기 위해 지금 액세스해야하는 모든 것입니다. 다른 사람들이 있다면 나는 같은 방법을 사용할 수 있습니다. 추신 왜 우리는 그냥 채팅하지 않습니까? – greatmastermario

관련 문제