Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
James Brown
2,095 PointsCan't 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)
}
}
James Brown
2,095 PointsTurns out I needed to have
self.physicsBody?.dynamic = true
and instead have
self.physicsBody?.affectedByGravity = false
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsCheck out this Blog post