0

I would like my buttons to fade in when changing scene rather than just be their. I am using sprite kit and UIView.animateWithDuration doesn't work.

How else could you do it using sprite kit in swift?

LukeTerzich
  • 555
  • 1
  • 4
  • 17
  • If by button you mean UIButton, it wouldn't and you shouldn't be using one in your game. If you want a button, you need to implement your own using a subclass of SKNode, SKSpriteNode, etc. – Mick MacCallum Feb 01 '15 at 13:13
  • Thank you for clarifying that for me, Could you offer a quick example of how to implement that or point me in the right direction? I need something which the user clicks to take them to a particular scene (which i thought a button would be the best bet) – LukeTerzich Feb 01 '15 at 13:16
  • 1
    Sure, there are some good examples of how you could implement touch handling on buttons made out of nodes [here](http://stackoverflow.com/questions/19082202/setting-up-buttons-in-skscene). The accepted answer shows the basic idea, but the next answer down is a lot more interesting. It shows you how you could go about subclassing SKSpriteNode to make a button class. These answers are in Objective-C, but they should get the point across just fine. – Mick MacCallum Feb 01 '15 at 13:20
  • Perfect Answer - Thank you very much! now to translate Obj_c to Swift.... i feel a problem arrising! haha Great help though! – LukeTerzich Feb 01 '15 at 13:22
  • You don't necessarily have to translate it. That second answer appears to be complete, so you could just add it into your project in Objective-C header/implementation files and then use a [bridging header](http://stackoverflow.com/a/24005242/716216) to use it in Swift. Once you complete this, you can use SKAction as stated in @Christian Woerz's answer below (with its fade related methods). – Mick MacCallum Feb 01 '15 at 13:29
  • Just given it ago - Their seems to be one that has already been translated to swift and created as a new class. I am not too sure what to do with it though, I have created a new swift file and pasted it in - Just need a little push to get started as I'm new to swift @0x7fffffff – LukeTerzich Feb 01 '15 at 15:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70001/discussion-between-luketerzich-and-0x7fffffff). – LukeTerzich Feb 01 '15 at 16:51

1 Answers1

1

I'm assuming you are using UIButtons, and you really shouldn't, use only SKNodes (Sprites, Shapes, Labels, etc) and the methods of touch handle like: touchBegan: touchMoved: TouchEnded:

If you want to build a button or a switch for generic use, you should try this control I made, the use its pretty straight forward:

You just initialize the type of Button/Switch you want (ColoredSprite, Textured or TextOnly)

let control = TWButton(normalColor: SKColor.blueColor(), highlightedColor: SKColor.redColor(), size: CGSize(width: 160, height: 80))`

And after initialize you add a closure to it (like addTargetForSelector on UIButton)

control.addClosureFor(.TouchUpInside, target: self, closure: { (scene, sender) -> () in
    scene.testProperty = "Changed Property"
    })
}

That’s it! There’s more info on the readme section on the GitHub page: https://github.com/txaidw/TWControls

Then you can use actions to fade your button

SKAction.fadeAlphaTo(alpha:, duration:)
txaidw
  • 485
  • 4
  • 13