Android game logic problem
Posted
by
semajhan
on Stack Overflow
See other posts from Stack Overflow
or by semajhan
Published on 2010-12-31T00:16:35Z
Indexed on
2010/12/31
4:54 UTC
Read the original article
Hit count: 225
I'm currently creating a game and have a problem which I think I know why it is occurring but not entirely sure and even if I knew, don't know how to solve. I have a 2D array 10 x 10 and have a "player" class that takes up a tile. Now, I have created 2 instances of the player and move them around via swiping. Around the edges I have put "walls" that the player cannot walk through and everything works fine, until I remove a wall. Once I remove a wall and move the character/player to the edge of the screen, the player cannot go any further. The problem occurs here, where the second instance of the player is not at the edge of the screen but say 2 tiles from the first instance of "player" who is at the edge. If I try moving them further into the direction of the edge, I understand that the first instance of player wouldn't move or do anything but the second instance of player should still move, but it won't.
This is the code that executed when the user swipes:
if (player.getArrayX() - 1 != player2.getArrayX()) {
player.moveLeft();
} else if (player.getArrayX() - 1 == player2.getArrayX() && player.getArrayY() != player2.getArrayY()) {
player.moveLeft();
}
if (player2.getArrayX() - 1 != player.getArrayX()) {
player2.moveLeft();
} else if (player2.getArrayX() - 1 == player.getArrayX() && player2.getArrayY() != player.getArrayY()) {
player2.moveLeft();
}
In the player class I have:
public void moveLeft() {
if (alive) {
switch (levelMaster.getLevel1(getArrayX() - 1, getArrayY())) {
case 0:
break;
case 1:
subX(); // basically moves player left
setArrayX(getArrayX() - 1); // shifts x coord of player 1 within tilemap
Log.d("semajhan", "x: " + getArrayX());
break;
case 9:
subX();
setArrayX(getArrayX() - 1);
setAlive(false);
break;
}
}
}
Any help on the matter or further insight would be greatly appreciated, thanks.
© Stack Overflow or respective owner