1

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?

2
  • Your error pertains to Ball, but there is no mention of Ball in the code. Commented Aug 29, 2015 at 5:23
  • sorry about that, I got the code mixed with some commented stuff, I just made the correction. I'm still getting the same error Commented Aug 29, 2015 at 5:30

2 Answers 2

2

orbitaType parameter is an OrbitaType, not an Int. You'll need to pass something like Orbita(orbitaType: .firstPlanet, ....

Sign up to request clarification or add additional context in comments.

6 Comments

I guess I have to do some rawValue conversion or so to pass Int. How is this done?
You can create OrbitaType(rawValue: ...). Just curious, why do you need to pass an Int? Usually using enum constants by name is preferred since it's more readable.
how can I add newOrbita as a class variable of GameScene since I am using GameScene as an argument?
Why are you passing it as an argument? It seems there's no need. Makes sense for the scene to own the object.
In the init I add the sprite as a child. That's the main reason. As for the position, I can easily work around that.
|
0

To access anything from a separate class, you have to create an instance of that class: In gameScene:

let orbitaInstance = Orbita() //let instanceName = classYouWantToAccess()

Then, inside of any function:

let image = SKSpriteNode(image: orbitaInstance.simpleDescription) // instanceOfClassYouAreAccessing.nameOfFunctionOrVariableYouAreAccessing

Hope this helped and good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.