Most of the SpriteKit code I've written so far just has a few objects so I put all the game logic in the GameScene. Now I am working on something more complex and I am trying to separate some of the objects into their own classes. How can I initialize the Orbita class below so that the simpleDescription string is used for the ImageName of the SKSpriteNode in GameScene?
class Orbita {
var orbita : SKSpriteNode
init(orbita: SKSpriteNode) {
self.orbita = orbita
physics ()
}
func physics () {
orbita.physicsBody = SKPhysicsBody(circleOfRadius: orbita.frame.size.width/2)
orbita.physicsBody?.friction = 0
orbita.physicsBody?.restitution = 1
orbita.physicsBody?.linearDamping = 0
orbita.physicsBody?.angularDamping = 0
orbita.physicsBody?.allowsRotation = false
orbita.physicsBody?.applyImpulse(CGVectorMake(4, -4))
}
enum OrbitaType: Int {
case firstPlanet = 0, secondPlanet, thirdPlanet
func simpleDescription()->String {
switch self {
case .firstPlanet:
return "green"
case .secondPlanet:
return "red"
case .thirdPlanet:
return "yellow"
}
}
}}
UPDATE
This is what I've been able to do so far. I decided to make a number of changes
class Orbita {
var sprite : SKSpriteNode
var orbitaType : OrbitaType
init(orbita: SKSpriteNode) {
self.orbita = orbita
physics ()
}
init(orbitaType: OrbitaType, gameScene: GameScene) {
self.orbitaType = orbitaType
sprite = SKSpriteNode(imageNamed: orbitaType.simpleDescription())
sprite.name = orbitaCategoryName
sprite.position = CGPointMake(3*gameScene.frame.size.width/4, 3*gameScene.frame.size.height/4)
sprite.zPosition = 10
gameScene.addChild(sprite)
physics()
}
func physics () {
sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.frame.size.width/2)
sprite.physicsBody?.friction = 0
sprite.physicsBody?.restitution = 1
sprite.physicsBody?.linearDamping = 0
sprite.physicsBody?.angularDamping = 0
sprite.physicsBody?.allowsRotation = false
sprite.physicsBody?.applyImpulse(CGVectorMake(4, -4))
}
enum OrbitaType: Int {
case firstPlanet = 0, secondPlanet, thirdPlanet
func simpleDescription()->String {
switch self {
case .firstPlanet:
return "green"
case .secondPlanet:
return "red"
case .thirdPlanet:
return "yellow"
}
}
}}
In the GameScene class I added the following:
var newOrbita = Orbita(orbitaType: 0, gameScene: self)
But I keep getting the following error:
Cannot invoke initializer for type 'Orbita' with an argument list of type '(orbitaType: Int, gameScene: GameScene)'
Also, how can I add newOrbita as a class variable of GameScene since I am using GameScene as an argument?
Ball, but there is no mention of Ball in the code.