1
votes

I'm making complex buttons with various states and animations between states, and looping "attract" animations. Due to the nature of the shadows, highlights, glows and internal visual changes the buttons are quite complex.

Each button has several SKSpriteNodes for drawing and animation, multiple SKLabelNodes and multiple SKEffectNodes, plus actions transitioning between states based on state of self and others, and touch events on self.

Currently these buttons are built and instanced from an SKSpriteNode subclass.

But it occurs to me I might be doing this "wrong", and that I should be subclassing an SKNode instead.

What are the advantages and disadvantages of stacking elements into a subclass of SKNode vs SKSpriteNode in situations like the above?

1

1 Answers

3
votes

According with SKNode definition:

The SKNode class does not perform any drawing of its own.

I quite agree to think it's better to use SKSpriteNode as a subclass of a button because there may also be big advantages in the init methods of the custom button and this can become very comfortable during the creation of a button. I write few code to make an example:

class CoolButton: SKSpriteNode {

    var isEnabled: Bool = true {
        didSet {
            if (disabledTexture != nil) {
                texture = isEnabled ? defaultTexture : disabledTexture
            }
        }
    }
    var isSelected: Bool = false {
        didSet {
            texture = isSelected ? selectedTexture : defaultTexture
        }
    }

   init(normalTexture defaultTexture: SKTexture!, selectedTexture:SKTexture!, disabledTexture: SKTexture?, size:CGSize) {
      ...
      super.init(texture: defaultTexture, color: UIColor.white, size: size)
      ...
   }
   ...
}

I see no disadvantages because SKSpriteNode inherits directly from SKNode.

Also there is no difficulty to add new elements as children, as SKLabelNode or other SKSpriteNode