iPhone Cocos2d Game Menu Text Entry or Text Field - iphone

Working on my third Cocos2d iPhone game and running into problems trying to make a create new account textfield. I have a menu image and some text that need to be displayed. On top of this layer I would like a Textfield so that the user can type the name of their new account. Seems like it would be pretty simple. I have looked at every example I could find online and nothing seems to work. Here is the method that I need to create the textfield in:
-(void) CreateNewAccount: (id) sender
{
[self removeChild:MainMenu cleanup:YES];
BG2 = [CCSprite spriteWithFile:#"WelcomeMenu.png"];
BG2.position = ccp(512, 384);
[self addChild:BG2 z:6];
[self removeChild:BG cleanup:YES];
NewNameText = [CCSprite spriteWithFile:#"NewName.png"];
NewNameText.position = ccp(512, 300);
[self addChild:NewNameText z:7];
}
Thanks for any help you can provide!!!

It looks like your text field is actually a sprite? I would suggest creating an actual UITextField for your purposes.
See the following: UITextField Example in Cocos2d

Related

How to make UITextView text all selectable and show copy option to the user iPhone app?

Am working in iPhone application like iMessage native app. I have designed a page look like iMessage with added a bubbles also. How i done is, just added a UITextView in UITableView Cell and also i have added UIImageView on UITextView with bubble images.
Load the text to UITextView from NSMutableArray. It is working fine. Now my doubt is:
In iMessage app they set that when the user hold the bubble they making the text as selectable and showing Copy option. In my app when the user hold a bubble the Copy option showing but some particular text only copying. How can i select all text and show copy option the user?
In iMessage app the selecting region (two lines in beginning of selection and end of selection) not showing. But in my app the selection regions are showing how can i manage that?
Could you please help me to solve this issues? Thanks in advance.
Finally i have solved my problem used below code. Now am able to select all content from UITextView and showing Copy option to the user to copy the message. Please find the code for your reference.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:))
{
[self selectAll:self];
return YES;
}
else if (action == #selector(cut:))
{
return NO;
}
return NO;
}
- (void)copy:(id)sender
{
UIPasteboard *pastBoard = [UIPasteboard generalPasteboard];
[pastBoard setString:self.text];
self.selectedTextRange = nil;
}
Happy Coding.
Here, How you can select text in UITextView
[textView setSelectedRange:NSMakeRange(row, length)];
where, row indicates from which row you want to start your selection. length is total length of text.
e.g. [textView setSelectedRange:NSMakeRange(1, 45)]; // where 1 is first row, and 45 is length of text of selection.

SetUserTrackingMode not working

I am programming a map app on iPhone and want the map to rotate as the user changes his direction. I have read most of the posts on stackoverflow. Most of them suggest the use of setUserTrackingMode with MKUserTrackingModeFollowWithHeading if we are working with iOS 5 or later. This does not seem to work with me for some reason. Following is my code:
-(IBAction)getLocation //This is a button
{
mapView.showsUserLocation=YES; //mapView is the instance of MKMapView
[mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
}
This only shows the user location but if I move the phone, it doesn't rotate.
One more thing is, I downloaded a project from internet, and I included this line. It worked there only for the first time. I have no idea why this is happening.
Any suggestions?
You need to wait for the 'MapView' finish loading...
follow:
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
mapView.userTrackingMode = MKUserTrackingModeFollow;
}
follow & heading:
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
The easier way to do this is to include an MKUserTrackingBarButtonItem instead of creating your own button. It acts exactly the same as the button in the iOS 5 Maps app and is easy to set up.
Here's how to use it:
// You should have an outlet to your map view called mapView
MKUserTrackingBarButtonItem *userTrackingButton;
userTrackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];
// You need an outlet to your toolbar too
[self.toolbar setItems:[NSArray arrayWithObject:userTrackingButton]];
Change "Animated" to "animated" and try again

getting of a menuitemfont in cocos2d

i have a ccmenuitemfont and its contained inside a ccsprite. .
the touch event is assigned to menuitemfont.. i need to get the reference of the ccsprite which contains the menuitemfont on ccmenuitemfont click... that is i need to get the reference of the parent sprite.. how to implement? any ideas ? the code i used is as follows..
CCSprite *ballSprite=[CCSprite spriteWithFile:imageName];
[ballSprite setTag:randomNumber];
CCMenuItem *labelButton=[CCMenuItemFont itemFromString:[NSString stringWithFormat:#"%d",randomNumber]target:self selector:#selector(clickedBallLabel :)];
[labelButton setTag:randomNumber];
CCMenu *ball=[CCMenu menuWithItems:labelButton, nil];
[ball setPosition:ccp([ballSprite boundingBox].size.width/2, [ballSprite boundingBox].size.height/2)];
[ballSprite addChild:ball];
[self addChild:ballSprite];
[ballSprite setPosition:randomStartPoint];
id move = [CCMoveTo actionWithDuration:5 position:ccp(randomX, -30)];
[ballSprite runAction:move];
in my point of view , there is a logic problem in you program,you want your labelbutton have a recation right? when your touch it,so why you need a reference to parent with the button?but if there is a must for sure ,you need a spritesheet like this :
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"scene1atlas.plist"];
spriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:#"scene1atlas.png"];
then you can add your obj into the batchnode,after that you can use [obj parent] get the parent reference
i'm not sure i know what your meaning,when the button been touched,the ballsprite should be changed?if so,you can just change the ballsprite directly for another new sprite,of course,the better way is the sprites should be into one batchnode as much as possible, otherwise,if your ballsprite is a spritesheet ,then use the method i've told before

IPad Simple Form with Flexible Space

Im creating an app for an internal process where our trainers will go out to clients and fill in a survey of the customer after the fact.
I want to create a flexible space with in the view. Example if they click a checkbox that the Customer is not happy than text fields will be shown to enter more information which will need to dynamically push all fields under it down to accomodate the new fields. If the user is happy no fields need to appear and no dynamic shifting is necessary.
Just wondering if anyone has done this before and if there is a clean way to execute it.
I don't think there is a control that will do this.
But it should not be that hard. This code is not tested but something like this
//views needs to be ordered in the subview list by y position
//can be in in uibuilder or on awakefrom nib..
-(void) addTextFieldAfterView:(UIView*) selectedView
{
CGRect f = CGRectMake(selectedView.origin.x,
selectedView.origin.y+5+selectedView.size.height,
selectedView.size.width,
150);
UITextField *tf = [[UITextField alloc] initWithFrame:f];
[self insertSubview:tf belowSubview:selectedView];
int start = [self.subviews indexOfObject:tf];
//move everything down
for(int i =start; i < [self.subviews count];i++)
{
UIView* v = [self.subviews objectAtIndex:i];
v.frame = CGRectMake(v.origin.x,
v.origin.y+150,
v.size.width,
v.size.height);
}
}
I like using UITableView for forms. This is what a lot of the forms in the Settings app uses. It's fairly simple to turn sections on and off -- just respond to the messages with the right number of sections and then fill them in as requested.

Text Typing Effect in iPhone cocos2d Game

In many games, when a character is speaking (dialogue), the text has a typing effect, where it looks like you are watching the character type the text.
What would be a good way to achieve this look and (simple) "animation" for an iPhone game which uses cocos2d?
It's good if there's a way to do it with cocos2d, but I'm not completely opposed to layering a UIView subclass (UILabel?) on top of cocos2d's EAGL (OpenGL ES) view.
I ended up using built-in UIKit elements (UILabel) and adding them as subviews to the window.
the built-in UIKit could not works well with cocos2d-built-in UIKit,such as CCLabel,CCSprite.
I would suggest using a CCLabelTTF and CCAction as follows:
- (void) typeText
{
static NSUInteger currentCharacter = 0;
// Your instance variable stores the text you want to "type" in it:
_text;
// Sorry, I can't remember substring functions, this is pseudo-code:
NSString *substring = [_text getSubstringFrom:0 to:currentCharacter];
// Also can't remember how to get length of a string (yipes)
NSUInteger stringLength = [_text length];
if (currentCharacter < stringLength)
{
CCSequence *loop = [CCSequence actions:[CCDelayTime actionWithDuration:0.3f],[CCCallFunc actionWithTarget:self selector:#selector(typeText)],nil];
[self runAction:loop];
}
}
This is untested code. Also, it assumes that the typeText function is implemented in a subclass of CCNode because it calls [self runAction:]