Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMcgerald Lezeau
7,096 PointsHow do I create a menu?
i can't just add buttons because there is no view on a scene
4 Answers
Stone Preston
42,016 Pointsif you want to use sprite kit to make your menu you can add SKLabelNodes and use those as buttons. Since SKLabelNode inherits from SKNode it has touchesEnded/began/moved methods. you would need to subclass SKLabelNode and make a specific class for each button and override the touch methods if you wanted to do it that way. Depending on how many buttons you want to have this could get tedious as you would have a bunch of classes for all the different buttons so that they could each have a unique action on button presses.
another way would be to override the touchesEnded/began methods of your scene and then check to see if the touches were within the buttons frame. again depending on the number of buttons this could get tedious as you would have to check the frame of each button in your scene
AGSpriteButton works pretty well. it allows you to add different actions to your button to make it behave more like a UIButton. Ive used it pretty easily to make buttons but i prefer the method below since the hard part about making a menu is the layout and laying out SKLabels and sprites all in code can be annoying sometimes
you could add UIButtons directly to the view controller in storyboard and create actions for those buttons as well too. This way would be a lot easier than using sprite kit and designing a menu using SKLabels is pretty tedious since its all gotta be code. the storyboard would make it a lot easier.
If were making a menu for my game Id probably use storyboard and UIButtons and then setup my spritekit scene inside the button actions and what not. This would also make it easy to implement a navigation controller if you wanted to have multiple menu screens to navigate through etc etc.
Mcgerald Lezeau
7,096 Pointsyes thank you about the UIButtons way of doing things i wouldn't want my buttons to appear during game play , unless theres away to transition from SKScene on Game view controller to another view controller and vice versa.
Stone Preston
42,016 Pointsyes you could have a a MenuViewController which has buttons, and a GameViewController that displays the actual game scene. You would create a segue between the buttons on MenuViewController to the GameViewController to move from the menu to the game. this way you have buttons and stuff in the menu, but when you tap them you are shown the actual game with no buttons on the view, just the sprite kit scene.
I like this way because you get to use storyboards to layout your menu as opposed to doing it all in code. Either way works, but its nice to visually design it instead of having to write all the code to position it, change anchor points, align text etc.
Mcgerald Lezeau
7,096 Pointsalright i will give it a shot . Thanks-
Mcgerald Lezeau
7,096 Pointshow can i segue from a skscene to another view controller like you said
Stone Preston
42,016 Pointsyou segue between view controllers, not scenes. So if you had a menu view controller and a GameViewController to create a manual segue between them just control click and drag from your MenuViewController to your GameViewController to create a segue, then give the segue an identifier using the inspector. You can perform the segue by calling the performSegueWithIDentifier method in your MenuView controller.
Mcgerald Lezeau
7,096 PointsMcgerald Lezeau
7,096 Pointshow can i segue from a skscene to another view controller like you said