【问题标题】:My SKSpriteNode wouldn't show even though I used addChild in swift即使我在 swift 中使用了 addChild,我的 SKSpriteNode 也不会显示
【发布时间】:2018-07-28 10:49:39
【问题描述】:

我尝试将图像添加到我的游戏场景中,但它不会出现。下面是我的代码。希望大家帮帮我

import SpriteKit
import GameplayKit

class GameScene: SKScene {
    var Ground = SKSpriteNode()
    var iceCream = SKSpriteNode()
    override func didMove(to view: SKView) {
        Ground = SKSpriteNode(imageNamed: "background")
        Ground.setScale(0.5)
        Ground.position = CGPoint(x: self.frame.width / 2, y: self.Ground.frame.height / 2)
        self.addChild(Ground)
        iceCream = SKSpriteNode(imageNamed: "VanillaIceCream")
        iceCream.size = CGSize(width: 60, height: 70)
        iceCream.position = CGPoint(x: self.iceCream.frame.width, y: self.frame.height / 2)
        self.addChild(iceCream)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    }
    override func update(_ currentTime: CFTimeInterval) {

    }
}

【问题讨论】:

  • 您的代码正在编译并且工作正常,您是否看到任何带有“X”的图像?
  • 没有。它没有出现在我的模拟器上
  • 图片不显示。我不知道出了什么问题

标签: swift sprite-kit skspritenode addchild


【解决方案1】:

当您将position 设置为x: self.iceCream.frame.width 您使用iceCream 定位它。 我相信将其更改为x: self.frame.width / 2 将解决它。 如果没记错,你想在屏幕左侧设置iceCream,把sprite添加到self后定位。 最好为每个元素设置zPosition,以确保它不会隐藏在background 图像后面。

编辑: 这是您的方法的完整代码(准备复制/粘贴):

Ground = SKSpriteNode(imageNamed: "background")
self.addChild(Ground)
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
Ground.zPosition = 1.0

iceCream = SKSpriteNode(imageNamed: "VanillaIceCream")
self.addChild(iceCream)
iceCream.size = CGSize(width: 60, height: 70)
iceCream.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
iceCream.zPosition = 2.0

【讨论】:

  • 你能给我举个例子吗,包括zPosition
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2016-01-24
  • 2017-05-30
相关资源
最近更新 更多