0

So, like my SKSpriteNodes, I am trying to create a function to make me a default physics body.

Here's my DefaultBody.swift I created:

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

}

Here I can call the Sprite version easily:

enter image description here

However, although I can do that with an SKSpriteNode, my SKShapeNode function does not work, as I get this error:

enter image description here

This does not work, so if I get rid of the inout in the function and add it as a return, it now works (using the call commented out in the above image)

Is there any reason to this, and how can I use inout instead of returning the SKShapeNode?

Note: a PhysicsBody can be set to an SKShapeNode

Both are var and not let as shown here, and so should be mutable:

enter image description here

George
  • 25,988
  • 10
  • 79
  • 133

2 Answers2

1

First, to pass an inout parameter, you must add &:

createPhyscisBodyShape(for: &topBar, type: .rectangle)

You did this with the &ball. I wonder why you forgot the & the second time.

Anyway, you don't need inout in the first place. SKSpriteNode and SKShapeNode are classes. That means as long as you don't set body to something else with an assignment statement, the changes you made to body will be reflected in the parameter you passed.

Remove the inout and it should work fine.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • My code did actually have the ampersand (`&`), but for some reason I didn't copy and paste that correctly, oops. I will try not using `inout` now – George Apr 30 '18 at 18:10
  • @George_E_2 are you sure it's a copy and paste mistake? I didn't see a & in the image. – Sweeper Apr 30 '18 at 18:12
  • I'm quite surprised it works! I now understand how I don't need the inout. Thank you! – George Apr 30 '18 at 18:12
  • It is when I was testing when it didn't work, I had copied and pasted the commented version and got rid of the variable assignment which left no & – George Apr 30 '18 at 18:13
0

pass value to function of inout params , we should use &

createPhyscisBodyShape(for: &topBar , type: .rectangle)

check with this link https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID545

ShyamZ_R
  • 71
  • 2
  • 3