Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

sprite kit - Folder of images: create and fill SKShapeNodes, 1 of each

Is it possible to create a folder and have SpriteKit go through that folder, find each image (regardless of name), and create a SKShapeNode for each in that folder (regardless of how many there are)?

Assume all images are the same size, and all .png files ready to be used as unique images as textures for filling SKShapeNodes, and that all SKShapeNodes will be the same size.

I want to know, once done, how many shapes have been created, and have them uniquely named based on the name of the image of the texture used.

But all I'm reading seems to be about knowing how many images are there, and what their names are, before hand.

Clueless as to where to start searching about how to do this.

Update: no idea how to reference the "folder" with images:

I've dumped the images, with their very imaginative names [one, two, three, four, five etc. ], into a folder as "atlas", like this:

enter image description here

but as per Allessandro's wonderful answer, I have no idea how to address this "folder" and get at what's in it.

I think it's this line I'm getting completely wrong:

let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)

I don't know what to replace "Images" with.

Update 2:

Found a much easier way to do this:

let myTextureAtlas = SKTextureAtlas(named: "demoArt")

        print(myTextureAtlas)
        print(myTextureAtlas.textureNames)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Too many requests , so I try to help you with some code:

Create a folder:

func createDir(folderName:String) {
        let fileManager = FileManager.default
        if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") {
            let newDirectory = directory.appendingPathComponent(folderName)
            try! fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
        }
}

Now you have your folder so you can decide to save a file or to copy one file from documents to this new directory. I make an example for the last one.

Copy a file:

let fileManager = FileManager.default
do {
   try fileManager.copyItem(atPath: "hero.png", toPath: "subfolder/hero.swift")
}
catch let error as NSError {
   print("Something went wrong: (error)")
}

Now you want to know how to extract all files from a directory.

Extract all files from a directory:

func extractAllFile(atPath path: String, withExtension fileExtension:String) -> [String] {
        let pathURL = NSURL(fileURLWithPath: path, isDirectory: true)
        var allFiles: [String] = []
        let fileManager = FileManager.default
        if let enumerator = fileManager.enumerator(atPath: path) {
            for file in enumerator {
                if #available(iOS 9.0, *) {
                    if let path = NSURL(fileURLWithPath: file as! String, relativeTo: pathURL as URL).path
                        , path.hasSuffix(".(fileExtension)"){
                        allFiles.append(path)
                    }
                } else {
                    // Fallback on earlier versions
                }
            }
        }
        return allFiles
    }

Usage:

let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)
let allPngFiles = extractAllFile(atPath: folderPath!, withExtension: "png") // returns file path of all the png files inside the folder

After that, you can create an array of SKShapeNode based from allPngFiles using fillTexture with a SKTexture created from each image and give to each node the name of the file used (so you know at the end also how many nodes you have created from array.count).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...