简体   繁体   English

如何从Android中的另一个类调用方法?

[英]How to call a method from another class in android?

I have two classes in android studio for a game. 我在android studio中有两个课程可以玩。 Crate class and player class. 板条箱类和玩家类。 I was wondering: How can I call the player class within the crate class? 我想知道:如何在板条箱类中调用播放器类? I am trying to do it by Player thePlayer = new Player(getContext()); 我正在尝试通过Player thePlayer = new Player(getContext()); but this just keeps giving me errors for the get context part. 但这总是让我在获取上下文部分出错。

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;
}

Transform the first part of your code to this one: 将代码的第一部分转换为此:

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);

}
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM