How to set speed in Swift
Below is the enemy node in a sprite set in Swift. I have set the speed for the node, but when it appears, the movement is not moving. Xcode is not giving me any errors, is there something I am 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)
}
}
source to share
From Apple's SpriteKit Documentation :
A Boolean value that indicates whether the physical body is moved using physics simulation.
Declaration
Swift
var dynamic: Bool
Objective-C
@property(nonatomic, getter=isDynamic) BOOL dynamic
Discussion
If the value is NO, the physical body ignores all forces and impulses applied to it. This property is ignored on edge bodies; they are automatically static.
As 0x141E says you have to make your dynamic SKPhysicsBody
, or it won't move.
source to share