我试图将Swift 1中的this github代码转换为Swift2。这就像this video一样。

我翻译的Rope.swift看起来像这样:

//
//  Rope.swift
//  SKSwiftRopeDemo
//
//  Created by Jeremy Higgins on 6/10/14.
//  Copyright (c) 2014 Digital Buckeye. All rights reserved.
//

import SpriteKit

class Rope : SKNode {

    var ropeTexture : String
    var node1 : SKNode
    var node2 : SKNode
    var parentScene : SKScene

    init(parentScene scene : SKScene, node node1 : SKNode, node node2 : SKNode, texture tex : String) {

        self.ropeTexture = tex
        self.node1 = node1
        self.node2 = node2
        self.parentScene = scene

        super.init()
        self.name = "rope"

        self.createRope()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func createRope() {
        // Calculate distance & angle

        let deltaX = node2.position.x - node1.position.x
        let deltaY = node2.position.y - (node1.position.y  + (node1.frame.size.height / 2))
        let total = deltaX * deltaX + deltaY * deltaY
        let distance = Float(sqrtf(Float(total)))
        let height = Float(SKSpriteNode(imageNamed: "Rope").size.height - 1.0)
        let p = (distance / height)

        var points = Int(p)
        points -= 1;

        let vX = CGFloat(deltaX) / CGFloat(points)
        let vY = CGFloat(deltaY) / CGFloat(points)

        let vector = CGPoint(x: vX, y: vY)
        var previousNode : SKSpriteNode?
        let angle = atan2f(Float(deltaY), Float(deltaX))

        for i in 0...points {
            var x = self.node1.position.x
            var y = self.node1.position.y + (self.node1.frame.size.height / 2)

            y += vector.y * CGFloat(i)
            x += vector.x * CGFloat(i)

            let ropePiece = SKSpriteNode(imageNamed: self.ropeTexture)
            ropePiece.name = "rope"
            ropePiece.position = CGPoint(x: x, y: y)
            ropePiece.zRotation = CGFloat(angle + 1.57)
            ropePiece.zPosition = -1

            ropePiece.physicsBody = SKPhysicsBody(rectangleOfSize: ropePiece.size)

            ropePiece.physicsBody?.collisionBitMask = 2
            ropePiece.physicsBody?.categoryBitMask = 2
            ropePiece.physicsBody?.contactTestBitMask = 2

            self.parentScene.addChild(ropePiece)

            if let pNode = previousNode {
                let pin = SKPhysicsJointPin.jointWithBodyA(pNode.physicsBody!, bodyB: ropePiece.physicsBody!, anchor: CGPoint(x: CGRectGetMidX(ropePiece.frame), y: CGRectGetMidY(ropePiece.frame)))
                self.parentScene.physicsWorld.addJoint(pin)
            } else {
                if i == 0 {
                    let pin = SKPhysicsJointPin.jointWithBodyA(self.node1.physicsBody!, bodyB: ropePiece.physicsBody!, anchor: CGPoint(x: CGRectGetMidX(self.node1.frame), y: CGRectGetMaxY(self.node1.frame)))
                    self.parentScene.physicsWorld.addJoint(pin)
                }
            }

            previousNode = ropePiece
        }

        if let pNode = previousNode {
            let pin = SKPhysicsJointPin.jointWithBodyA(self.node2.physicsBody!, bodyB: pNode.physicsBody!, anchor: CGPoint(x: CGRectGetMidX(pNode.frame), y: CGRectGetMidY(pNode.frame)))
            self.parentScene.physicsWorld.addJoint(pin)
        }
    }

    func destroyRope() {
        self.parentScene.enumerateChildNodesWithName("rope", usingBlock: { node, stop in
            node.removeFromParent()
        })
    }
}

而GameScene.swift部分变成这样:
//
//  GameScene.swift
//  Hop Hope
//
//  Created by Sergio on 5/2/16.
//  Copyright (c) 2016. All rights reserved.
//

import SpriteKit

struct PhysicsCategory {
    static let Player : UInt32 = 0x1 << 1
    static let Ground : UInt32 = 0x1 << 2
    static let Rope : UInt32 = 0x1 << 3
}

class GameScene: SKScene {

    var Ground = SKSpriteNode()

    var Rope = SKSpriteNode()

    var Player = SKSpriteNode()
    let szPlayer = 60

    var anchor = SKSpriteNode()
    var aplayer = SKSpriteNode()


    override func didMoveToView(view: SKView) {
        // Physics border around screen
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

        // Static Body
        self.anchor = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
        self.anchor.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height - anchor.size.height)
        self.anchor.physicsBody = SKPhysicsBody(rectangleOfSize: anchor.frame.size)
        self.anchor.physicsBody?.dynamic = false
        self.anchor.physicsBody?.collisionBitMask = 1
        self.anchor.physicsBody?.categoryBitMask = 1
        self.anchor.physicsBody?.contactTestBitMask = 1
        self.addChild(self.anchor)

        // Dynamic Body
        self.aplayer = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 50, height: 50))
        self.aplayer.position = CGPoint(x: aplayer.size.width * 2, y: self.frame.size.height / 2)
        self.aplayer.physicsBody = SKPhysicsBody(rectangleOfSize: aplayer.frame.size)
        self.aplayer.physicsBody?.collisionBitMask = 1
        self.aplayer.physicsBody?.categoryBitMask = 1
        self.aplayer.physicsBody?.contactTestBitMask = 1
        self.addChild(self.aplayer)

        // Create Rope
        var rope = Rope(parentScene: self, node: self.aplayer, node: self.anchor, texture: "Rope")


        var label = SKLabelNode(text: "Tap on screen to move block")
        label.fontColor = UIColor.blackColor()
        label.position = CGPoint(x: self.frame.size.width / 2, y: 200)
        self.addChild(label)

        ////////////////////////////////// rest of the code

问题是var rope = Rope(parentScene: self, node: self.aplayer, node: self.anchor, texture: "Rope")给出错误无法调用非功能类型“SKSpriteNode”的值ios - 快速错误:无法调用非函数类型“SKSpriteNode”的值-LMLPHP

这是唯一的错误,其余代码和文件都很好,而且我不知道如何解决此问题。我已经尝试过将Rope.swift中的所有SKNode更改为SKSpriteNode,但这只会带来更多错误。

最佳答案

您已经声明了一个名为Rope的变量,以及一个名为Rope的类:

var Rope = SKSpriteNode()

因此,稍后创建绳索时:
var rope = Rope(parentScene: self, node: self.aplayer, node: self.anchor, texture: "Rope")

Swift认为您正在尝试将变量Rope用作函数指针,而SKSpriteNode无法做到这一点。

注意:对于变量和变量,应使用以小写字母开头的名称,对于类和结构,应使用以大写字母开头的名称。如果您遵守了该约定,那么您就不会遇到这个问题。

10-05 18:32