Ciaran
Ciaran

Reputation: 695

How to call a method from another class in android?

I have two classes in android studio for a game. 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()); 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;
}

Upvotes: 0

Views: 309

Answers (2)

magirtopcu
magirtopcu

Reputation: 1194

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

Upvotes: 0

Gabriella Angelova
Gabriella Angelova

Reputation: 2985

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

}

Upvotes: 2

Related Questions