Domanda

In this, I'm testing if an int value is a whole number or has a decimal value, if it has a decimal, it slowly adds or subtracts to the value to make it a whole number. The first and the third parts work, but the second and fourth don't.

if(ax % tileSize != 0) {
    ax -= (ax % tileSize) / 6; // works fine
}
if(ax % tileSize != 0) {
    ax += (ax % tileSize) / 6; // doesn't work
}
if(ay % tileSize != 0) {
    ay -= (ay % tileSize) / 6; // works fine
}
if(ay % tileSize != 0) {
    ay += (ay % tileSize) / 6; // doesn't work
}

The ones that work are decreased by 48 / 6 each time, and the others should be increased by 48 / 6, but it seems that the amount they are increased by changes each time.

È stato utile?

Soluzione

Given this author's comment:

This is just a Java Game, and all I'm testing for is if the Player's x coordinate, (ax), and the y coordinate, (ay), are in line with the tiles, as this is a tile-based game. If they're not in line with the tiles, then the coordinates are increased or decreased so you are put in line with the tiles.

The way to do that would be something along these lines:

double tileSize = 10;
double ax = 25;
double vectorX = Math.floor(ax/tileSize + 0.5) - ax/tileSize;

This will give you a vector in range of -1..1 which you can multiply by speed or do whatever you want to decide the movement. For example:

ax = ax + Math.ceil(vectorX*speed);

Same goes with ay axis. Also, notice that there are doubles in my formula so apply appropriate casts if needed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top