I have a method in a class that sets "Position X" and "Position Y".
public class Player {
int positionX, postinionY;
public void setPosition (int position_X, int position_Y)
{
positionX = position_X;
postinionY = position_Y;
}
}
I have another class "World" that has an array in it called grid. I have to use the values of position x and y from Player to get location from the grid.
public class World extends Player {
int [] [] grid = new int [16] [16];
public int getLocationID (int x, int y)
{
return grid [x][y];
}
}
I need help to take Position x,y(Player) and use them instead x,y (World)
You could add another method to World
get the own (the players) location id:
public int getPlayerLocationID() {
return grid[positionX][positionY];
}
But - honestly - in what kind of universum we have a relation "a world is-a player"? In all universums that I know (Ok, only one, actually), we usually have "a world has-many players".
This method should do what you want.
public class World extends Player {
int [] [] grid = new int [16] [16];
public int getPlayerID (int locationX, int locationY)
{
if((locationX>15)||(locationX>16))
throw new InvalidArgumentException("Player locations should be less than 16!");
return grid [locationX][locationY];
}
}
Note that it will throw an exception in case the arguments provided are not valid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.