Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用SKSpritenodes和纹理_Ios_Swift_Xcode_Sprite Kit - Fatal编程技术网

Ios 使用SKSpritenodes和纹理

Ios 使用SKSpritenodes和纹理,ios,swift,xcode,sprite-kit,Ios,Swift,Xcode,Sprite Kit,我正在尝试编程的功能,允许用户改变“鬼”到一个不同的图像 这是我的密码: Ghost = SKSpriteNode(imageNamed: "Ghost17") Ghost.size = CGSize(width: 50, height: 50) Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2) Ghost.physicsB

我正在尝试编程的功能,允许用户改变“鬼”到一个不同的图像

这是我的密码:

    Ghost = SKSpriteNode(imageNamed: "Ghost17")
    Ghost.size = CGSize(width: 50, height: 50)
    Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
    Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
    Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
    Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
    Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
    Ghost.physicsBody?.affectedByGravity = false
    Ghost.physicsBody?.isDynamic = true
    Ghost.zPosition = 2

    if defaults1.integer(forKey: "Sphere") == 2 {
        Ghost = SKSpriteNode(imageNamed: "Ghost13")
    }
    if defaults1.integer(forKey: "Sphere") == nil! {
        Ghost = SKSpriteNode(imageNamed: "Ghost2")
    }

    self.addChild(Ghost)
我使用UserDefaults在viewcontoller之间发送信息


当我点击将defaults1设置为2的按钮时,会弹出新的图像,但它的行为与应有的不同。我不知道如何修复此问题,如果您需要更多信息,请询问。

更改纹理时,请尝试使用以下方法:

Ghost.texture = SKTexture(imageNamed: "Ghost13")
而不是:

Ghost = SKSpriteNode(imageNamed: "Ghost13")
这只会更改节点的纹理特性,而不会重新初始化它。您可能会遇到这些问题,因为在重新初始化节点时正在重置节点

希望这有帮助