简体   繁体   中英

Swift error: Cannot call value of non-function type “SKSpriteNode”

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

My translated Rope.swift finished looking like this:

//
//  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
//  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" 烦人的错误

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 SKNode s in Rope.swift to SKSpriteNode , but this just gave more errors.

You've declared a variable called Rope as well as having a class called 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM