James Stephen Brown
James Stephen Brown

Reputation: 299

How to set velocity in Swift

Below is an Enemy node in Sprite Kit, in Swift. I have set the velocity for the node, but when it spawns there is no movement. Xcode is giving me no errors, is there something I'm doing wrong here?

class Enemy: SKSpriteNode {

    init(imageNamed: String) {

        let imageTexture = SKTexture(imageNamed: imageNamed)
        super.init(texture: imageTexture, color: nil, size: imageTexture.size())

        self.name = "Enemy"

        self.physicsBody = SKPhysicsBody(rectangleOfSize: imageTexture.size())
        self.physicsBody?.dynamic = false
        self.physicsBody?.velocity = CGVectorMake(10.0,10.0)

    }
}

Upvotes: 3

Views: 4490

Answers (1)

Moshe
Moshe

Reputation: 58097

From Apple's SpriteKit documentation:

A Boolean value that indicates whether the physics body is moved by the physics simulation.

Declaration

Swift

var dynamic: Bool

Objective-C

@property(nonatomic, getter=isDynamic) BOOL dynamic

Discussion

If the value is NO, the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.

As 0x141E says, you have to make your SKPhysicsBody dynamic, or it won't move.

Upvotes: 7

Related Questions