考虑:
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureRegion
import java.lang.Exception
import kotlin.math.floor
lateinit var AssetMap: MutableMap<String, Texture>
lateinit var SheetAssets: MutableMap<String, MutableMap<String, TextureRegion>?>
object Assets {
lateinit var fileHandle: FileHandle
fun loadAssets()
{
loadAssets(DefaultAssetDirectory)
}
fun loadAssets(directory: String)
{
AssetMap = mapOf<String, Texture>().toMutableMap() // Current implementation resets the map at load
fileHandle = Gdx.files.local(directory)
var fileList = fileHandle.list()
for(file in fileList)
{
if(file.extension() == "png")
try
{
AssetMap[file.nameWithoutExtension()] = Texture(directory+file.name())
}
catch(e: Exception) {
}
}
}
/**
* For very basic sprite sheets.
* Since you won't input a name List in this function, they will be named by their index.
* The function will probably start scanning from top left 0,0
* @param directoryToSheet local directory to sheet
* @param spaceBetweenX only the space between textures, doesnt work forspace between the texture and the border
* @param spaceBetweenY only the space between textures, doesnt work forspace between the texture and the border
*/
fun loadSheetAtlas(directoryToSheet: String, width: Int, height: Int, spaceBetweenX: Int, spaceBetweenY: Int, spritesX: Int, spritesY: Int)
{
loadSheetAtlas(directoryToSheet, null, width, height, spaceBetweenX, spaceBetweenY, spritesX, spritesY)
}
/**
* For very basic sprite sheets.
* Since you won't input a name List in this function, they will be named by their index.
* @param directoryToSheet local directory to sheet
* @param names if this is null, they will be named by their index
* @param spaceBetweenX only the space between textures, doesnt work forspace between the texture and the border
* @param spaceBetweenY only the space between textures, doesnt work forspace between the texture and the border
*/
fun loadSheetAtlas(directoryToSheet: String, names: List<String?>?, width: Int, height: Int, spaceBetweenX: Int, spaceBetweenY: Int, spritesX: Int, spritesY: Int)
{
SheetAssets = mutableMapOf<String, MutableMap<String,TextureRegion>?>()
var nameToConvert = Gdx.files.local(directoryToSheet).nameWithoutExtension()
nameToConvert = nameToConvert.replace(' ', '-').toLowerCase()
var sheet = Texture(directoryToSheet)
var nameIndex = 0
if(!SheetAssets.keys.contains(nameToConvert)) // To see if we have parsed it already
{
SheetAssets[nameToConvert] = mutableMapOf<String, TextureRegion>()
for(x in 0 until spritesX)
{
for(y in 0 until spritesY) // When I put a -1 to spritesX/Y, the keycount becomes 121 for 12,12?
{ // I check for if 'names' is null every single loop. It can be optimised to check for once, but that'd be premature optimisation
var keyToAdd = if((names != null) && (names[nameIndex] != null) ) names[nameIndex]!! else nameIndex.toString()
if(x == spritesX - 1)
{
var textureToAdd = TextureRegion(sheet,
((spritesX+spaceBetweenX))-spaceBetweenX,
((spritesY+spaceBetweenY))-spaceBetweenY,
width,height)
SheetAssets[nameToConvert]!![keyToAdd] = textureToAdd
}
else // These will work if textures use bottom left origin point
{
var textureToAdd = TextureRegion(sheet, // These don’t work. Continue from here
((spritesX+spaceBetweenX)*spritesX),
((spritesY+spaceBetweenY)*spritesY),
width,height)
SheetAssets[nameToConvert]!![keyToAdd] = textureToAdd
}
nameIndex++
}
}
}
}
}
这是我写的代码。由于某种原因,它只返回整个纹理中特定区域的特定纹理。但是,宽度和高度是正确的。
现在解释这个问题,基本上可以解决为xPos = texture.width / texture size % texture size 的东西。但这里的问题是存在偏移量。在纹理之间偏移,并且绝对不在纹理和边界之间。你可以在上面看到我的代码。我尝试添加偏移量,减去偏移量等,但它似乎不起作用......有什么帮助吗?
同样对于当前的实现,可变内部唯一的东西是在精灵表的 144,144,宽度:16,高度:16 originpoint:topleft 内找到的 textureRegion
我认为这称为 spritesheet 切割或 spritesheet 切割器。