私はまだSpriteKitを初めて使用しているので、XcodeのSKSエディターを使用してレベルを作成できるかどうかを確認したいと思いました。「Spaceship.png」テクスチャを使用してスプライトをいくつか追加し、テンプレートアプリを作成すると、テクスチャが読み込まれません。
これは、言語としてSwiftを使用し、提供されている「Spaceship.png」スプライトを追加したバニラOSXゲームテンプレートのスクリーンショットです。テクスチャはきれいに表示されます。テンプレートを変更しただけでアプリをビルドして実行した結果は次のとおりです。
The debug console displays this warning message: I tried to add an .atlas folder, and got the same result. the scene just displays the red X icon in place of the sprite. If the added sprites where just color sprites, they display fine. I had an app some time ago that I scrapped, where I used to load the SKScene and would manually add the sprite assets within to my SKScene sub-class, and it worked fine.
If, however, i moved the textures - "Spaceship.png" as an example - to the root of the project, i.e. not inside an asset catalogue or .atlas folder, the scene loads with the textures displaying fine.
Here's the texture added to the root of the project:
And this is the desired result: I tried to manually add the loaded assets from the SKS file to the scene via enumerateChildNodesWithName(_,usingBlock)
and I get the same result if the textures were not in the root of the project folder.
This is me trying to add the assets manually:
func applicationDidFinishLaunching(aNotification: NSNotification) {
/* Pick a size for the scene */
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let sceneToBePresented = GameScene()
/* Set the scale mode to scale to fit the window */
sceneToBePresented.scaleMode = .AspectFill
sceneToBePresented.size = scene.size
/* Sprite Kit applies additional optimizations to improve rendering performance */
self.skView!.ignoresSiblingOrder = true
self.skView!.showsFPS = true
self.skView!.showsNodeCount = true
scene.enumerateChildNodesWithName("*") { node, stop in
sceneToBePresented.addChild(node.copy() as SKSpriteNode)
}
self.skView!.presentScene(sceneToBePresented)
}
}
I looked around SpriteKit classes - SKNode
, SKScene
, SKTexture
, SKSPriteNode
- for any clue about paths, caching, preloading, or anything but was unable to find a thing that could make this work.
I am running Xcode 6.1.1 and my target is 10.9, and the language I'm using is Swift, although the same behavior holds true using ObjC. Is this a feature or a bug? Anybody else running/ran into a similar situation ?
Thanks in advance for any help
[UPDATE]
Looks like this is not implemented yet - loading textures from asset catalogs - as this post in the dev forums discusses the same issue, and his solution is to rename the asset catalogues to be the same name as the texture. In essence, what I found out about having the image files in the root folder: How do you load sprite textures from Images.xcassets when using SKS scene file, although my earlier app which i managed to roll-back to a working state does load textures from .atlas'es, but i can't seem to do it with a clean template!!!
[Answering my own question here]
As far as I can tell, at this moment, loading texture from asset catalogues - Images.xcassets
- while unarchiving or deserializing an SKScene
file does not work on OSX based on my attempts and the devforumns post referenced above.
Loading of the textures from image atlases, however, works by forcing SKTexture
to load the image. Forcing or 'touch'ing SKTexture
can be done via the preloadWithCompletionHandler(_:)
or preloadTextures(_:, withCompletionHandler:)
methods, or simply by printing the description of the sprite's SKTexture
as i have discovered.
For the benefit of anybody who might need further assistance, here is a code snippet that preloads the textures after unarchiving an SKS file:
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
for child in scene.children as [SKNode] {
if child is SKSpriteNode {
let sprite = child as SKSpriteNode
sprite.texture?.preloadWithCompletionHandler({ })
/* or even a simple debug log of the texture */
/* println(sprite.texture) */
}
}
/* Do your other stuff ... */
...
}
私が間違っている場合は私を訂正してください。誰かがそうするか、AppleがiOSとOSXの間のSpriteKitの動作の不一致を修正するまで、私は別の解決策を探すことはなく、マーフィーの法則に従います。
それが機能する場合は、修正しないでください
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加