通过单击更改节点的图像-SWIFT

Changing the image of a node by clicking-SWIFT

我想知道如何通过单击更改节点的图像。我一直在尝试像这样更改图像节点,但这是完全错误的,希望您能提供帮助。

import SpriteKit

class Scene: SKScene , SKPhysicsContactDelegate{
...
func addNode(){

    var Node:SKSpriteNode = SKSpriteNode(imageNamed: "Node")
    Node.name = "Node"
    Node.physicsBody = SKPhysicsBody(circleOfRadius: Node.size.width/2)
    Node.physicsBody?.dynamic = true
    var actionArray:NSMutableArray = NSMutableArray()
    Node.removeFromParent()

    Node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    addChild(Node)

}

...

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    let move = CGFloat(colorAzul.size.width)
    for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
    let node = self.nodeAtPoint(location)
    let node2 = self.nodeAtPoint(location)
        if (node.name == "Node") {
            node = SKSpriteNode(imageNamed: "Node2")
        }
    }
}

如果你想改变 SKSpriteNode 的图像,你将不得不使用它的纹理 属性。

您应该尽可能使用 SKTexture 而不是图像,因为您可以轻松创建漂亮的 SKPhysicsBody。但是对于您的问题,您可以轻松更改节点的 SKTexture

var nodeTexture = SKTexture(imageNamed: "player1")

var Node:SKSpriteNode = SKSpriteNode(texture: nodeTexture)

然后您可以像这样更改节点的纹理:

node.texture = SKTexture(imageNamed: "otherImage")