3

What is the best way to add a UIButton to an SKScene?

I realize that I probably can't add it as a subview of the scene, so what is the alternative so that I can have a tappable button on top of a scene?

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

2 Answers2

1

You can opt for one of the following ways:

1 - Subclass SKSpriteNode to act as a button

2 - Implement touch delegates within the scene and respond if your 'button' node is touched

This link will tell u how to implement the two above.

3 - Use a component like the SpriteKit Button which can be found here.

Community
  • 1
  • 1
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
1
 /// create a uibutton in skscene (spritkit)

      UIButton * startButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 60, 20)];
                startButton.backgroundColor = [UIColor redColor];

                [self.view addSubview:startButton];

/// if u want to connect it to a method or function /// 

[startButton addTarget:self action:@selector(StartBtn) forControlEvents:UIControlEventTouchUpInside];
Simran Singh
  • 445
  • 6
  • 8