所以,就像我SKSpriteNode
的 s 一样,我正在尝试创建一个函数来让我成为一个默认的物理体。
这是我DefaultBody.swift
创建的:
import Foundation
import SpriteKit
enum Type {
case rectangle
case circle
}
// Create new default physics body
func createPhysicsBodySprite(for body: inout SKSpriteNode, type: Type) {
// Create new physics body
switch type {
case .rectangle:
body.physicsBody = SKPhysicsBody(edgeLoopFrom: body.frame)
case .circle:
body.physicsBody = SKPhysicsBody(circleOfRadius: body.frame.width / 2)
}
body.physicsBody?.isDynamic = false
body.physicsBody?.allowsRotation = false
body.physicsBody?.pinned = false
body.physicsBody?.affectedByGravity = false
body.physicsBody?.friction = 0
body.physicsBody?.restitution = 0
body.physicsBody?.linearDamping = 0
body.physicsBody?.angularDamping = 0
}
func createPhysicsBodyShape(for body: inout SKShapeNode, type: Type) {
// Create new physics body
switch type {
case .rectangle:
body.physicsBody = SKPhysicsBody(edgeLoopFrom: body.frame)
case .circle:
body.physicsBody = SKPhysicsBody(circleOfRadius: body.frame.width / 2)
}
body.physicsBody?.isDynamic = false
body.physicsBody?.allowsRotation = false
body.physicsBody?.pinned = false
body.physicsBody?.affectedByGravity = false
body.physicsBody?.friction = 0
body.physicsBody?.restitution = 0
body.physicsBody?.linearDamping = 0
body.physicsBody?.angularDamping = 0
}
在这里我可以轻松调用 Sprite 版本:
但是,尽管我可以使用 来执行此操作,但SKSpriteNode
我的SKShapeNode
函数不起作用,因为我收到此错误:
这不起作用,所以如果我去掉inout
函数中的 并将其添加为返回,它现在可以工作(使用上图中注释掉的调用)
有什么理由吗,我该如何使用inout
而不是返回SKShapeNode
?
注意: aPhysicsBody
可以设置为SKShapeNode
两者都在这里显示,var
也没有let
显示,因此应该是可变的: