Swift错误:无法调用非函数类型“SKSpriteNode”的值

时间:2021-12-19 18:03:39

I tried to translate this github code that was in Swift 1 to Swift 2. That makes a rope like in this video.

我试图将Swift 1中的这个github代码翻译成Swift 2.这就像在这个视频中一样。

My translated Rope.swift finished looking like this:

我翻译的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()
        })
    }
}

And the GameScene.swift part became like this:

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

The problem is that var rope = Rope(parentScene: self, node: self.aplayer, node: self.anchor, texture: "Rope") is giving the error Cannot call value of non-function type "SKSpriteNode"Swift错误:无法调用非函数类型“SKSpriteNode”的值

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

And this is the only error that is giving, the rest of the code and files are just fine, and I have no ideia how to fix this. I already tried changing all the SKNodes in Rope.swift to SKSpriteNode, but this just gave more errors.

这是唯一给出的错误,其余的代码和文件都很好,我没有想法如何解决这个问题。我已经尝试将Rope.swift中的所有SKNode更改为SKSpriteNode,但这只会产生更多错误。

1 个解决方案

#1


1  

You've declared a variable called Rope as well as having a class called Rope:

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

var Rope = SKSpriteNode()

So, when you create the rope later:

所以,当你以后创建绳索时:

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

Swift thinks you are trying to use your variable Rope as a function pointer which you can't do with an SKSpriteNode.

Swift认为您正在尝试将变量Rope用作函数指针,而SKSpriteNode则无法使用它。

Note: You should use names that start with a lowercase letter for variables and names that start with an uppercase letter for classes and structs. If you had observed this convention, you wouldn't have had this problem.

注意:您应该使用以小写字母开头的变量名称和以类和结构的大写字母开头的名称。如果你观察过这个约定,你就不会遇到这个问题。

#1


1  

You've declared a variable called Rope as well as having a class called Rope:

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

var Rope = SKSpriteNode()

So, when you create the rope later:

所以,当你以后创建绳索时:

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

Swift thinks you are trying to use your variable Rope as a function pointer which you can't do with an SKSpriteNode.

Swift认为您正在尝试将变量Rope用作函数指针,而SKSpriteNode则无法使用它。

Note: You should use names that start with a lowercase letter for variables and names that start with an uppercase letter for classes and structs. If you had observed this convention, you wouldn't have had this problem.

注意:您应该使用以小写字母开头的变量名称和以类和结构的大写字母开头的名称。如果你观察过这个约定,你就不会遇到这个问题。