2015-06-24 60 views
0

我在android studio中有兩個類用於遊戲。板條箱類和玩家類。我想知道:我怎麼能在板條箱類中調用球員類?我試圖通過Player thePlayer = new Player(getContext())來完成它;但是這只是給我上下文部分的錯誤。如何從android中的另一個類調用方法?

public class Crate { 
public static int acrossCrate; 
public int upDownCrate; 
Player thePlayer = new Player(getContext()); //<--GIVES ERROR 

public Crate(Context context) { 

    int rect = 1000; 
    int height = context.getResources().getDisplayMetrics().heightPixels; 
    int width = context.getResources().getDisplayMetrics().widthPixels; 

    int startPosX = (width/2)-(rect/2); 
    int startPosY = (height/2)-(rect/2); 

    acrossCrate = startPosX +300; 
    upDownCrate = startPosY +300; 

} 


public class Player { 
public static int across; 
public int upDown; 
int boardWidth; 
int boardHeight; 
int startPosX; 
int startPosY; 
int stopPosX; 
int stopPosY; 

public Player(Context context) { 

    int rect = 1000; 
    boardHeight = context.getResources().getDisplayMetrics().heightPixels; 
    boardWidth = context.getResources().getDisplayMetrics().widthPixels; 

    startPosX = (boardWidth/2)-(rect/2); 
    startPosY = (boardHeight/2)-(rect/2); 
    stopPosX = (boardWidth/2)+(rect/2); 
    stopPosY = (boardHeight/2)+(rect/2); 

    across = startPosX+500; 
    upDown = startPosY+500; 
} 
+2

變化'球員thePlayer =新播放器(的getContext());'到'球員thePlayer;'和使用' thePlayer = new Player(context);'在'Crate'類的構造函數中 –

+0

你應該真的閱讀一本關於Java基礎知識的書。 – 2Dee

回答

2

變換你的代碼的第一部分,這一個:

public class Crate { 
public static int acrossCrate; 
public int upDownCrate; 
Player thePlayer; 

public Crate(Context context) { 

    int rect = 1000; 
    int height = context.getResources().getDisplayMetrics().heightPixels; 
    int width = context.getResources().getDisplayMetrics().widthPixels; 

    int startPosX = (width/2)-(rect/2); 
    int startPosY = (height/2)-(rect/2); 

    acrossCrate = startPosX +300; 
    upDownCrate = startPosY +300; 

    //define your player here 
    thePlayer = new Player(context); 

} 
0
public class Crate { 
public static int acrossCrate; 
public int upDownCrate; 
Player thePlayer ; 

public Crate(Context context) { 
thePlayer = new Player(context); 
int rect = 1000; 
int height = context.getResources().getDisplayMetrics().heightPixels; 
int width = context.getResources().getDisplayMetrics().widthPixels; 

int startPosX = (width/2)-(rect/2); 
int startPosY = (height/2)-(rect/2); 

acrossCrate = startPosX +300; 
upDownCrate = startPosY +300; 

} 


public class Player { 
public static int across; 
public int upDown; 
    int boardWidth; 
    int boardHeight; 
int startPosX; 
int startPosY; 
int stopPosX; 
    int stopPosY; 

    public Player(Context context) { 

    int rect = 1000; 
     boardHeight = context.getResources().getDisplayMetrics().heightPixels; 
boardWidth = context.getResources().getDisplayMetrics().widthPixels; 

startPosX = (boardWidth/2)-(rect/2); 
startPosY = (boardHeight/2)-(rect/2); 
stopPosX = (boardWidth/2)+(rect/2); 
stopPosY = (boardHeight/2)+(rect/2); 

across = startPosX+500; 
upDown = startPosY+500; 
} 
相關問題