Hello, I have a problem with a sprite sheet. I started doing a tutorial on YouTube and basically my question is the same as this person here - http://stackoverflow.com/questions/14952863/importing-a-sprite-from-a-sprite-sheet/17820295#17820295 … I tried the advice given there, but there needs to be some more tweaking to the code and I am not sure how some parts of it work in order to fix it. The full code is here - https://github.com/vanZeben/2D-Game-Engine/tree/master/src/ca/vanzeben/game
Basically the code uses a sprite sheet with only 4 colors which are then replaced within the code to form the color displayed. I want it to be able to get the color directly from the sheet and render it.
Any help would be appreciated. Thanks!
So which one is it? ;V
For the second, simply copy the pixel directly from the sprite sheet to the screen.
For the first, map the 4 colors that appear in your sprite sheet to the colors you want them to change into somewhere. Then before rendering the pixel from the sprite sheet. Copy the color from the sprite sheet, check if it’s any of the 4 colors and change it to the appropriate one and then copy the modified color to the screen.
I didn’t express myself correctly I want to have sprites in the sheet with different colors and not only the 4 which are set in the code. So the first thing you quoted is how the code works now, but I want the thing from the second quote and that’s what I’m unsure how to do.
Hmm? If the color is in the sheet already, then you don’t have to do anything. Just draw it directly to the screen from the sheet? :v
I’m not sure how to do it The design uses the following render method:
public void render(int xPos, int yPos, int tile, int colour, int mirrorDir) {
xPos -= xOffset;
yPos -= yOffset;
boolean mirrorX = (mirrorDir & BIT_MIRROR_X) > 0;
boolean mirrorY = (mirrorDir & BIT_MIRROR_Y) > 0;
int xTile = tile % 32;
int yTile = tile / 32;
int tileOffset = (xTile << 4) + (yTile << 4) * sprites.width;
for (int y = 0; y < 16; y++) {
int ySheet = y;
if (mirrorY) {
ySheet = 15 - y;
}
if (y + yPos < 0 || y + yPos >= height)
continue;
for (int x = 0; x < 16; x++) {
int xSheet = x;
if (mirrorX) {
xSheet = 15 - x;
}
if (x + xPos < 0 || x + xPos >= width)
continue;
int col = (colour >> (sprites.pixels[xSheet + ySheet
* sprites.width + tileOffset] * 8) & 255);
if (col < 255)
pixels[(x + xPos) + (y + yPos) * width] = col;
}
}
}
The color is taken from the pixels array in the SpriteSheet class and some stuff which I don’t fully understand make the color, but I don’t know what to change.
public void render(int xPos, int yPos, int tile, int colour, int mirrorDir) {
xPos -= xOffset;
yPos -= yOffset;
boolean mirrorX = (mirrorDir & BIT_MIRROR_X) > 0;
boolean mirrorY = (mirrorDir & BIT_MIRROR_Y) > 0;
int xTile = tile % 32;
int yTile = tile / 32;
int tileOffset = (xTile << 4) + (yTile << 4) * sprites.width;
for (int y = 0; y < 16; y++) {
int ySheet = y;
if (mirrorY) {
ySheet = 15 - y;
}
if (y + yPos < 0 || y + yPos >= height)
continue;
for (int x = 0; x < 16; x++) {
int xSheet = x;
if (mirrorX) {
xSheet = 15 - x;
}
if (x + xPos < 0 || x + xPos >= width)
continue;
- int col = (colour >> (sprites.pixels[xSheet + ySheet
- * sprites.width + tileOffset] * 8) & 255);
- if (col < 255)
+ int col = sprites.pixels[xSheet + ySheet
+ * sprites.width + tileOffset]);
pixels[(x + xPos) + (y + yPos) * width] = col;
}
}
}
That should pick the exact color in the spriteSheet without modifying it at all.
I have the same problem and when I end up getting rid of a the lines with the - (red) and add the lines with the + (green) I get a problem with the height and width, a problem with the sprites.width part and with the screen.render part in other classes.