Cocos2d 中的 UITextField 示例
任何人都可以建议一些在 cocos2d 中使用 UITextField 的链接吗? 我想按标签,然后应该选择 UITextField
并且我需要在该 UITextField
上进行编辑。
Can anyone please suggest some links for using UITextField in cocos2d.
I want to press on label, then the UITextField
should get selected and I need to edit on that UITextField
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在当前的项目中这样做是为了允许输入开始玩的关卡编号,这就是我的变量和方法如此命名的原因; 您可能应该调整这些以使其对您有意义。
在您的应用程序控制器中,将其定义为实例变量:
在 applicationDidFinishLaunching 内创建它:
定义一个方法来激活文本字段。 您还应该在应用程序控制器的头文件中声明它。
这将使按键盘上的“返回”结束编辑。
实际完成编辑时会触发此操作。
现在要真正启动事情,你要把它放在某个地方。 我从我的一个场景类中调用它,以响应用户操作:
I'm doing this in a current project to allow for entering the number of the level to start playing at, so that's why my variables and methods are named the way they are; you should probably adjust these to make sense for you.
In your app controller, define this as an instance variable:
Create it inside applicationDidFinishLaunching:
Define a method to activate the text field. You should also declare it in the header file for your app controller.
This will make pressing "return" on the keypad end editing
This is triggered when the editing is actually done.
Now to actually set things in motion, you put this somewhere. I call this from within one of my Scene classes, in response to a user action:
我采用了 Jack 提供的示例并实际创建了一个工作项目,这是使用 Cocos2D 0.7.1 XCode 模板完成的,然后只需编辑 *AppDelegate.m/.h 文件,这些文件在下面完整提供。 我还修改了 Jack 所说的一些内容,因为我觉得在 appDidFinishLoading 中创建 UITextField 会占用太多内存,特别是如果文本字段不是一直使用的话......这个解决方案仅在它时才创建文本字段需要时,该示例绘制一个空的 Cocos2D Layer 场景,并在屏幕触摸时显示文本字段,供您开始输入文本。 它将输出您在控制台中输入的内容的结果 - 您可以将其传递给您自己的代码中所需的任何内容。
.h
,然后是 .m
I took the example that Jack provided and actually created a working project, this was done using the Cocos2D 0.7.1 XCode Template, and then just editting the *AppDelegate.m/.h files, which are provided below in there entirety. I also modified some of what Jack said, because I feel that creating the UITextField in the appDidFinishLoading would utilize a bit too much memory, especially if the text field is not used all the time ... this solution creates the text field only when it is needed, the sample draws an empty Cocos2D Layer scene, and on screen touch, it displays the text field for you to start entering text into. It will spit out the result of what you entered to the Console - you can pass this to whatever is necessary in your own code.
the .h
and then the .m
要在 cocos2d 中添加文本字段,如下代码所示,
首先在场景中添加视图,然后在视图中添加文本字段,这非常简单。
您还可以在 cocos2d 中使用 CCTextfield 最好的例子是 https://github.com/iNinja/CCTextField
To add Text field in cocos2d as below code
first of all you add view in Scene and afetr add textfield add in view thats very easy.
you can also use CCTextfield in cocos2d best example is https://github.com/iNinja/CCTextField
尝试使用以下 CCNode 子类 CCMenuItemTextField 在 cocos2d 中使用文本字段。
该类直接从 CCMenuItemSprite 派生出来。 点击时,将调用“selected”方法,并将 UITextField 添加到主窗口。 编辑完成后,调用“unselect”方法并将 UITextField 从屏幕上删除。 用户的输入被保存到 CCLabelTTF 节点,该节点的位置与原始 UITextField 完全相同。
CCMenuItemTextField.h
CCMenuItemTextField.m
Try the following CCNode subclass, CCMenuItemTextField, to use text fields in cocos2d.
The class is directly subclassed from CCMenuItemSprite. When tapped, the "selected" method is called and a UITextField is added to the main window. After editing is done, "unselected" method is called and the UITextField is removed from screen. User's input is saved to a CCLabelTTF node, which position itself exactly as the original UITextField.
CCMenuItemTextField.h
CCMenuItemTextField.m