0

I am trying to create a texture atlas using existing assets from a previous image asset that was created outside of Xcode/my machine.

Importing the image into Xcode does not "split" it, and I am unsure how to tell Xcode / SpriteKit where the "atlas" locations are.

Other than using an image editor and manually eating each image as an asset, how can I convince Xcode that the image is really an atlas already?

Finally, is it even worth it to use an atlas, as I have to cycle through the image myself to do animation; am I better off hardcoding (!) the x/y positions of the atlas strip and doing some sort of rotation / loop code in an SKAction?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Antonio Ciolino
  • 546
  • 5
  • 16
  • First question is unclear. Is it an Xcode-generated atlas of the same version of Xcode? Then you might be able to get it working in the bundle - somehow. Or maybe not. Having the source images will be a lot easier going forward, and because of that I think you're trying to solve the wrong problem here. The second question is subjective, depends entirely on code and context and can't be answered in general. Though if in doubt, favor the use of an atlas, its pros typically outweigh any cons. – CodeSmile Jan 09 '15 at 16:48
  • The source image is an older jpg file that has the frames strung together - it's never seen Xcode before today. Basically, it'a already an image atlas, so making one in in SKTextureAtlas probably will be helpful but might be more pain that I want to experience :) – Antonio Ciolino Jan 09 '15 at 17:15
  • 1
    If it hasn't been created with Xcode, it doesn't have the necessary plist and name/folder structure. You'd have to painstakingly reconstruct that. I bet it'll be easier to extract the images, or just use different images (ie get some for free on OpenGameArt). – CodeSmile Jan 09 '15 at 17:20
  • yeah, I just went ahead and stripped them down to PNG files. Would have preferred to read the Old Atlas, split it into SKTextures in memory and merge them into a new SKTextureAtlas. Too much code. – Antonio Ciolino Jan 09 '15 at 18:02
  • OK...NOW...when SKTextureAtlas loads the images what order does it decide to do that in? When I look altho the TextureNames, they are not sorted, even though my texture "files" are ordered numerically. – Antonio Ciolino Jan 09 '15 at 18:38
  • It loads the atlas when one of the images in the atlas is needed. You can also preload the atlas and keep a strong reference to it if you want to ensure it stays in memory even when no sprite is using any of its images (textures). – CodeSmile Jan 09 '15 at 18:46
  • While valid, this does not address the haphazard order of file names being loaded from the Atlas. Decided to load all file names and performa .sort on an array, then go through that array and load the file names in the "correct" order. – Antonio Ciolino Jan 09 '15 at 18:49
  • What are you doing? Are you using the SKTextureAtlas textureNames property? You really only need to load the atlas, and then use SKTexture textureWithImageNamed: providing your list of image names to "load" the textures. – CodeSmile Jan 09 '15 at 18:52
  • as an aside, I don't like hardcoding the "prefix" as I want a generic chunk of reusable code. So, I depend on the file name to be "properly" ordered. – Antonio Ciolino Jan 09 '15 at 18:53

1 Answers1

0

Decided to split the old atlas into separate images, then load them with a .atlas folder. Also decided to load all file names and perform a .sort on an array, then go through that array and load the file names in the "correct" order.

function snippet below:

func AssignTextureAtlas(atlas: SKTextureAtlas)
{
    var arrTextureNames: Array<String> = []

    //Create texture atlas array
    for (var i = 0; i < atlas.textureNames.count; i++)
    {
        var myText = atlas.textureNames[i] as String
        arrTextureNames.append(myText)
    }

    arrTextureNames.sort( {$0 < $1})
    for (var i = 0; i < arrTextureNames.count; i++)
    {
        var myTexture = atlas.textureNamed(arrTextureNames[i])
        arr.insert(myTexture, atIndex: 0)
    }
    //...more code here, until:

    self.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(arr, timePerFrame: 0.1, resize: false, restore: true)), withKey: "TEST")

}
Antonio Ciolino
  • 546
  • 5
  • 16