我的SKSpriteNode不会';即使我在swift中使用了addChild,也无法显示
我试图将图像添加到游戏场景中,但它不会出现。下面是我的代码。我希望你们能帮助我我的SKSpriteNode不会';即使我在swift中使用了addChild,也无法显示,swift,sprite-kit,skspritenode,addchild,Swift,Sprite Kit,Skspritenode,Addchild,我试图将图像添加到游戏场景中,但它不会出现。下面是我的代码。我希望你们能帮助我 import SpriteKit import GameplayKit class GameScene: SKScene { var Ground = SKSpriteNode() var iceCream = SKSpriteNode() override func didMove(to view: SKView) { Ground = SKSpriteNode(image
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) {
}
}
导入SpriteKit
导入游戏工具包
类游戏场景:SKScene{
var接地=SKSpriteNode()
var冰淇淋=SKSpriteNode()
覆盖func didMove(到视图:SKView){
地面=SKSpriteNode(图像名称:“背景”)
接地。设置刻度(0.5)
Ground.position=CGPoint(x:self.frame.width/2,y:self.Ground.frame.height/2)
self.addChild(地面)
冰淇淋=SKSpriteNode(图片名为:“VanillaIceCream”)
冰淇淋.size=CGSize(宽:60,高:70)
iceCream.position=CGPoint(x:self.iceCream.frame.width,y:self.frame.height/2)
自助儿童(冰淇淋)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
}
覆盖函数更新(currentTime:CFTimeInterval){
}
}
当您将位置设置为x:self.iceCream.frame.width时
您可以使用冰淇淋来定位它。
我相信将其更改为x:self.frame.width/2将解决此问题。
如果没有错误,并且您想在屏幕左侧设置冰淇淋
,请在将雪碧添加到self后定位它。
为每个元素设置zPosition
也很好,以确保它不会隐藏在background
图像后面
编辑:
以下是您的方法的完整代码(可以复制/粘贴):
您的代码正在编译,工作正常,您看到任何带有“X”的图像吗?没有。它不会出现在我的模拟器上,或者这些图像不会显示。我不知道怎么了,你能给我举一个例子吗
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