SpriteKit:使用skview in uiview而不是初始化游戏项目



SpriteKit全新。目前,我有一个Uiview,我想向其添加一个精灵节点(就像一个小型UIImageView,但我想要动画,因此请使用SpriteKit)。因此,我没有将项目初始化为游戏项目,正如SpriteKit的几乎所有教程中所发现的那样。我在这里找到了一个注释:链接,我现在拥有的是STH,例如:

func initializeImage() {
    let imageView = SKView()
    imageView.frame = CGRect(x: self.frame.width / 2 - Constants.imageWidth / 2, y: self.frame.height - Constants.imageHeight, width: Constants.imageWidth, height: Constants.imageHeight)
    // so place it somewhere in the bottom middle of the whole frame
    let sheet = SpriteSheet(texture: ...)
    let sprite = SKSpriteNode(texture: sheet.itemFor(column: 0, row: 0))
    sprite.position = imageView.center //basically the same position as the imageView.frame's x and y value
    let scene = SKScene(size: imageView.frame.size)
    scene.backgroundColor = SKColor.clear
    scene.addChild(sprite)
    imageView.presentScene(scene)
    self.frame.addSubview(imageView)
}

SpriteSheet与以下方式相似:Sprite片;它本质上是切割图像地图集,然后将其分为较小的图像。我跟踪了该过程,此步骤确实给出了较小的图像(var'Sprite')。但是,如果运行,我现在只有一个黑色正方形(应该是常数定义的大小)。如果我将场景设置为白色,那是白色。我可以知道如何从这里继续前进,因为我应该如何使精灵出现?

除此之外,所有代码看起来都不错:

sprite.position = imageView.center // basically the same position as the imageView.frame's x and y value

基本上是不是您认为是的位置。SpriteKit中的坐标系是a)相对于(SK)场景,而不是与SKView所包含的任何视图,b)相对于Uikit坐标系进行了垂直翻转。如果您想要一个以场景为中心的精灵,则可能要根据场景的size设置其position

sprite.position = CGPoint(x: scene.size.width / 2, y: scene.size.height / 2)

顺便说一句,如果您将Sprite纸切成薄片并将其放入Xcode Asset Catalog。

最新更新