我使用 SWIFT 3 和 SpriteKit 制作了一个简单的屏幕保护程序。
我可以让场景显示,但是当我尝试应用纹理图集并为其设置动画时,我得到一个带有动画而不是实际图像的红色 X。
调试器出现以下错误:
2016-12-15 09:35:29.131724 ScreenSaverEngine[1819:52037] 找不到纹理图集“传单”。
我很难过——难道不能在屏幕保护程序中运行的 SpriteKit 中使用纹理图集吗?
有关该项目的更多详细信息:
- 使用 TexturePacker 创建了纹理图集——确认纹理图集在 iOS 测试 SpriteKit 应用程序中有效。
- 使用来自 TexturePacker 站点的示例 SWIFT3 代码。
更新 12/15/16: Rickster 提到屏幕保护程序/插件的 bundle.main 不是我的包,而是主机应用程序包。因此,我的问题是我试图从 screenaverengine.app 包而不是我自己的包中加载资产,这会给我一个“找不到”。
我相信 Flyer.swift(见下面的代码),我需要从我的包中引用并加载 Flyers.atlasc。任何人都知道该语法的外观吗?
根据我收集的信息,我需要在这里以某种方式引用我的应用程序包:
// load texture atlas
let textureAtlas = SKTextureAtlas(named: "Flyers") // << This is trying to find it in the screensaverengine.app bundle and not where it actually lives in my bundle.
代码:
import SpriteKit
import Cocoa
class GameScene: SKScene
{
let sheet = Flyers()
var sequence: SKAction?
override var acceptsFirstResponder: Bool { return false }
override func didMove(to view: SKView)
{
self.resignFirstResponder()
self.isUserInteractionEnabled=false
/* Setup your scene here */
backgroundColor = (NSColor.black)
// in the first animation CapGuy walks from left to right, in the second one he turns from right to left
let fly = SKAction.animate(with: sheet.flyer(), timePerFrame: 0.033)
// to walk over the complete iPad display, we have to repeat the animation
let flyAnim = SKAction.repeat(fly, count: 6)
// we define two actions to move the sprite from left to right, and back;
let moveRight = SKAction.moveTo(x: 900, duration: flyAnim.duration)
let moveLeft = SKAction.moveTo(x: 100, duration: flyAnim.duration)
// as we have only an animation with the CapGuy walking from left to right, we use a 'scale' action
// to get a mirrored animation.
let mirrorDirection = SKAction.scaleX(to: -1, y:1, duration:0.0)
let resetDirection = SKAction.scaleX(to: 1, y:1, duration:0.0);
// Action within a group are executed in parallel:
let flyAndMoveRight = SKAction.group([resetDirection, flyAnim, moveRight]);
let flyAndMoveLeft = SKAction.group([mirrorDirection, flyAnim, moveLeft]);
// now we combine the walk+turn actions into a sequence, and repeat it forever
sequence = SKAction.repeatForever(SKAction.sequence([flyAndMoveRight, flyAndMoveLeft]));
// each time the user touches the screen, we create a new sprite, set its position, ...
let sprite = SKSpriteNode(texture: sheet.flyer1())
sprite.position = CGPoint(x: 100.0, y: CGFloat(arc4random() % 100) + 200.0)
// ... attach the action with the walk animation, and add it to our scene
sprite.run(sequence!)
addChild(sprite)
}
override func update(_ currentTime: CFTimeInterval)
{
/* Called before each frame is rendered */
/*self.backgroundColor = NSColor(calibratedHue: 0.0, saturation: 0.0, brightness: CGFloat(0.5 + (sin(currentTime) * 0.5)), alpha: 1.0)*/
}
}
这是 Flyers.swift 代码:
// ---------------------------------------
// Sprite definitions for 'Flyers'
// Generated with TexturePacker 4.3.1
//
// http://www.codeandweb.com/texturepacker
// ---------------------------------------
import SpriteKit
class Flyers {
// sprite names
let FLYER1 = "flyer1"
let FLYER2 = "flyer2"
let FLYER3 = "flyer3"
let FLYER4 = "flyer4"
let TOAST = "toast"
// load texture atlas
let textureAtlas = SKTextureAtlas(named: "Flyers")
// individual texture objects
func flyer1() -> SKTexture { return textureAtlas.textureNamed(FLYER1) }
func flyer2() -> SKTexture { return textureAtlas.textureNamed(FLYER2) }
func flyer3() -> SKTexture { return textureAtlas.textureNamed(FLYER3) }
func flyer4() -> SKTexture { return textureAtlas.textureNamed(FLYER4) }
func toast() -> SKTexture { return textureAtlas.textureNamed(TOAST) }
// texture arrays for animations
func flyer() -> [SKTexture] {
return [
flyer1(),
flyer2(),
flyer3(),
flyer4(),
flyer3(),
flyer2()
]
}
}
我有一个由 TexturePacker 生成的 Flyers.atlasc 作为蓝色文件夹导入到项目中。