Reputation: 1680
It seems that linear gravity field with vector method has been deprecated in Swift. There's a bunch of new SKFieldNode in this new language, but I can't seem to find one that resembles linear gravity. The property "direction" isn't even available in Swift. Maybe it can be done using this generic method customFieldWithEvaluationBlock but I'm not sure how. I'm new to SpriteKit.
So, how can we make the same physics effect in Swift?
Upvotes: 2
Views: 1636
Reputation: 126137
In Swift 2 (part of Xcode 7), SIMD vectors are available to Swift, so API that uses them (such as linearGravityFieldWithVector
, the new GameplayKit and Model I/O frameworks, etc) is are now imported into Swift, too... no workarounds necessary anymore.
let field = SKFieldNode.linearGravityFieldWithVector(float3(1,0,0))
Previously...
The SKFieldNode
class method linearGravityFieldWithVector
does this, but it's not available in Swift. In ObjC vector parameter uses the vector_float3
type from the SIMD library, which uses special C compiler magic to make vectors that use SSE (on OS X / x86) or NEON (on iOS / ARM) hardware acceleration and pack nicely for sending to GPUs. That kind of voodoo doesn't currently exist in Swift, so any ObjC APIs that rely on it aren't imported into Swift.
For now, you can access those APIs from ObjC -- in a Swift project, you can make yourself a little ObjC wrapper function and expose it to Swift in your bridging header.
Upvotes: 4
Reputation: 11746
If you want to apply linear gravity to all nodes you can try setting physicsWorld.gravity
, it's a CGVector
Upvotes: 1