Swift 错误:无法调用非函数类型“SKSpriteNode”的值
Posted
技术标签:
【中文标题】Swift 错误:无法调用非函数类型“SKSpriteNode”的值【英文标题】:Swift error: Cannot call value of non-function type "SKSpriteNode" 【发布时间】:2016-02-06 14:51:23 【问题描述】:我试图将 Swift 1 中的 this github 代码翻译成 Swift 2。这使得像 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")
给出错误Cannot call value of non-function type "SKSpriteNode"
这是唯一的错误,其余的代码和文件都很好,我不知道如何解决这个问题。我已经尝试将 Rope.swift 中的所有 SKNode
s 更改为 SKSpriteNode
,但这只会产生更多错误。
【问题讨论】:
您的问题是您有一个名为Rope
的var
。见var Rope = SKSpriteNode()
。
哦,谢谢!我删除了那行,但现在当我运行它时,屏幕变为白色,var anchor = SKSpriteNode()
以绿色突出显示“线程 1:断点 2.5”...我该怎么办?
是不是不小心设置了断点?如果该行旁边有一个蓝色箭头,请控制单击它并将其删除。
哦,是的!我不知道它们是做什么用的,我昨天刚开始使用 Xcode,但是您帮了很多忙,谢谢!如果你愿意,你可以用你所说的来回答这个问题
【参考方案1】:
您已经声明了一个名为 Rope
的变量以及一个名为 Rope
的类:
var Rope = SKSpriteNode()
所以,当您稍后创建绳索时:
var rope = Rope(parentScene: self, node: self.aplayer, node: self.anchor, texture: "Rope")
Swift 认为您正在尝试将变量 Rope
用作函数指针,而 SKSpriteNode
则无法做到这一点。
注意:您应该为变量使用以小写字母开头的名称,为类和结构使用以大写字母开头的名称。如果你遵守了这个约定,你就不会遇到这个问题。
【讨论】:
以上是关于Swift 错误:无法调用非函数类型“SKSpriteNode”的值的主要内容,如果未能解决你的问题,请参考以下文章
Swift 3:无法调用非函数类型“(()-> Void)的值?”
无法调用非函数类型“任何?!”的值:- Firebase,Swift3