使用 SKSpritenodes 和纹理 [英] Working with SKSpritenodes and textures

查看:26
本文介绍了使用 SKSpritenodes 和纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写允许用户将Ghost"更改为不同图像的功能.

I am trying to program in feature that allows the user to change the "Ghost" to a different image.

这是我的代码:

    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 在 vi​​ewContollers 之间发送信息.

I am using UserDefaults to send the information between the viewContollers.

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

When i click on the button that sets the defaults1 to 2 the new image pops up but it doesn't behave like it should. Im not sure how to fix this, if you need anymore info please ask.

推荐答案

当改变纹理时,尝试使用这个:

When changing the texture, try using this:

Ghost.texture = SKTexture(imageNamed: "Ghost13")

而不是:

Ghost = SKSpriteNode(imageNamed: "Ghost13")

这只会改变节点的纹理属性,而不是重新初始化它.您可能会遇到这些问题,因为您在重新初始化节点时正在重置它.

This only changes the texture property of the node rather than reinitializing it. You may be encountering the issues because you're resetting the node when you reinitialize it.

希望这有帮助!

这篇关于使用 SKSpritenodes 和纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆