问题
I am creating a game using Sprite Kit, and I want to add a textfield for players to enter their name.
How can I create a textfield using Sprite Kit? I'm going to create a player object (which I already have set up) and use the name they enter and a default score of 0. Is it bad practice, or even possible, to mix UITextfields and Sprite Kit nodes?
回答1:
Something like this
UILabel *nameField = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 300, 100)];
[self.view addSubview:nameField];
But I suggest, setting up your layout as you would for any other iOS app.
Mixing the 2 in my option is more trouble than what its worth. So you can have a simple scene, the displays a modal view, which you can set up in IB and store the persons name.
回答2:
It is certainly possible to mix the two but be aware that the UIViewController presents the SKScene so when you transition between SKScenes your UIKit elements may not transition as expected
When you add child SKNodes to an SKScene you use:
[self addChild:node]
In order to access the UIViewController to add UIKit elements like UITextView you do something like:
[self.view addSubview:textView]
Hope that helps.
回答3:
Thanks Smick, your tip of not mixing traditional controls and SpriteKit is helpful. I did two little videos on how to do what you suggested. You can see them at http://youtu.be/-B-iQ30dBbk and http://youtu.be/4xh5Z8os2I0.
来源:https://stackoverflow.com/questions/19897061/adding-textfield-to-skscene