gpt4 book ai didi

ios - swift 错误 : Cannot call value of non-function type "SKSpriteNode"

转载 作者:行者123 更新时间:2023-11-28 06:49:43 28 4
gpt4 key购买 nike

我尝试翻译 this github Swift 1 到 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非函数类型“SKSpriteNode”的值annoying error

这是唯一给出的错误,其余代码和文件都很好,我不知道如何解决这个问题。我已经尝试将 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 来做。

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

关于ios - swift 错误 : Cannot call value of non-function type "SKSpriteNode",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35242362/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com