单击-SWIFT更改节点的图像

时间:2015-03-18 13:26:30

标签: ios swift sprite-kit

我想知道如何通过单击来更改节点的图像。我一直试图改变像这样的图像节点,但这是完全错误的,希望你能帮忙。

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")
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

var nodeTexture = SKTexture(imageNamed: "player1")

var Node:SKSpriteNode = SKSpriteNode(texture: nodeTexture)

然后你可以改变节点的纹理:

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

答案 1 :(得分:0)

如果要更改SKSpriteNode的图像,则必须使用其纹理属性。