我正在尝试编写Android游戏,但目前我遇到一个问题,即四个条件中的两个条件正常工作,但不知道为什么后两个条件不起作用。我的意思不是条件,而是在那里精确分配的两个负值。
public abstract class GameMovingObject {
(...)
public void update() {
this.colUsing++;
if(colUsing >= this.colCount) {
this.colUsing =0;
}
// Current time in nanoseconds
long now = System.nanoTime();
// Never once did draw.
if(lastDrawNanoTime==-1) {
lastDrawNanoTime= now;
}
// Change nanoseconds to milliseconds (1 nanosecond = 1000000 milliseconds).
int deltaTime = (int) ((now - lastDrawNanoTime)/ 777777 );
// Distance moves
float distance = velocity * deltaTime;
double movingVectorLength = Math.sqrt(movingVectorX* movingVectorX + movingVectorY*movingVectorY);
// Calculate the new position of the game character.
this.x = x + (int)(distance* movingVectorX / movingVectorLength);
this.y = y + (int)(distance* movingVectorY / movingVectorLength);
// When the game's character touches the edge of the screen, then change direction
if(this.x < 0 ) {
this.x = 0;
this.movingVectorX = - this.movingVectorX;
} else if(this.x > this.gameSurface.getWidth() -width) {
this.x= this.gameSurface.getWidth()-width;
this.movingVectorX = - this.movingVectorX;
}
if(this.y < 0 ) {
this.y = 0;
this.movingVectorY = - this.movingVectorY;
}
// rowUsing (obraca ludzika)
if( movingVectorX > 0 )
{
if(movingVectorY > 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
this.rowUsing = ROW_TOP_TO_BOTTOM;
}
else if(movingVectorY < 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
this.rowUsing = ROW_BOTTOM_TO_TOP;
}
else {
this.rowUsing = ROW_LEFT_TO_RIGHT;
}
}
else
{
if(movingVectorY > 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
this.rowUsing = ROW_TOP_TO_BOTTOM;
}
else if(movingVectorY < 0 && Math.abs(movingVectorX) < Math.abs(movingVectorY)) {
this.rowUsing = ROW_BOTTOM_TO_TOP;
}
else if(movingVectorX!=0 || movingVectorY!=0) {
this.rowUsing = ROW_RIGHT_TO_LEFT;
}
}
}
和另一个班
public class Enemy extends GameMovingObject {
(...)
public void moveFreely(ChibiCharacter chibi, int xWhenInvoked, int YwhenInvoked, int enemyNode, int chibiNode, Canvas ca, Graph g, Vertex[] v, Edge[] e) {
setMovingVector(0, 0);
if (chibi.getX() < this.getX()) {
//ok
setX((cellWidth * ((getX() / cellWidth))) + (cellWidth / 2) - (getWidth() / 2));
}
if (chibi.getX() > this.getX() && raz == false) {
setX((cellWidth * ((getX() / cellWidth))) + (cellWidth) + (cellWidth / 2) - (getWidth() / 2));
raz = true;
}
if (chibi.getY() < this.getY()) {
//ok
setY((cellHeight * ((getY() / cellHeight))) + (cellHeight / 2) - (getHeight() / 2));
}
if (chibi.getY() > this.getY() && raz2 == false) {
setY((cellHeight * ((getY() / cellHeight))) + (cellHeight) + (cellHeight / 2) - (getHeight() / 2));
raz2 = true;
}
stop = true;
followChibi(enemyNode, chibiNode, ca, pt.pBlack, g, v, e);
}
public void showAttention(Canvas canvas, Paint p, ChibiCharacter chibi) {
if (Integer.parseInt(list.get(1)) == gs.enemyNode + 6 && list.size() > 2)
movingVectorY = 1;
if (Integer.parseInt(list.get(1)) == gs.enemyNode + 1 && list.size() > 2)
movingVectorX = 1;
if (Integer.parseInt(list.get(1)) == gs.enemyNode - 6 && list.size() > 2)
movingVectorY = -1;
if (Integer.parseInt(list.get(1)) == gs.enemyNode - 1 && list.size() > 2)
movingVectorX = -1;
}
}
问题在于后两个条件似乎不起作用,即敌人没有进入玩家的角色,而是前两个条件按预期工作。为什么会这样?