Showing posts with label add. Show all posts
Showing posts with label add. Show all posts

Tuesday, 5 November 2013

How to Install cocos2d in Xcode

Cocos2d is a powerful game engine, that can be added inside Xcode. It's installation steps are as simple as below:


  • Keep the downloaded file in your desktop.
  • Open terminal and type cd
  • Drag your latest version of cocos2d package into the terminal window.
  • Type sudo and open the cocos2d package, and drag the 'install-templates.sh' file into your terminal window. Then press 'Enter'.
  • Put your administrator password to install templates.
Now open your Xcode and see that the cocos2d has been successfully integrated. That's all...


Monday, 10 June 2013

How to draw simple lines in cocos2d

Here, I will show you 2 simple methods to draw lines in cocos2d applications.



Method 1 - Draw line - default method, updates in each frame:

Syntax:
 
    - (void)draw{
           [super draw];
           glColor4ub(0,0,0,255);
           glLineWidth(2);
           glColor4f(1.0, 1.0, 0.5, 1);
           ccDrawLine(ccp(0,100), ccp(320,150));    // Draw line connecting (0,100) & (320,150)
    }
       

Method 2 - Draw line using CCRibbon:
        This  is a very simple method to draw lines in cocos2d. The advantage of this method is that, you can even draw lines with your textured image.


Syntax:
 
        ccColor4B myColor = ccc4(255, 255, 255, 150);
        CCRibbon *ribbon = [CCRibbon ribbonWithWidth:0.5
                                                                              image:@"green.png"
                                                                              length:1.0
                                                                              color:myColor
                                                                              fade:0.7f];

        [self addChild:ribbon z:8];
       
        [ribbon addPointAt:ccp(0,240) width:2];          // This will initialize the starting point at (0,240)
        [ribbon addPointAt:ccp(320,200) width:2];      // This will connect the previous point[(0,240)]
                                                                                   with (320,200) via a straight line







Sunday, 2 June 2013

How to create UITextField programmatically: cocos2d/Xcode

In .h file:

        @interface SceneName : CCLayer <UITextFieldDelegate>
       
            UITextField *nameField;

In .m file:

   // To create UITextField and assigning properties//

        -(void)createTextField{
           // create a UIView here(say profileView). Then do the following code //

            nameField = [[UITextField alloc] initWithFrame:CGRectMake(5, 20, 150, 30)];
            nameField.center = CGPointMake(285, 120);
            nameField.backgroundColor = [UIColor whiteColor];
            nameField.textAlignment = UITextAlignmentCenter;
            nameField.delegate = self;
            [nameField.layer setBorderColor:[[UIColor redColor]CGColor]];
            [nameField.layer setBorderWidth:1];
            nameField.layer.cornerRadius = 8;
            nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [nameField addTarget:self action:@selector(valueChanged)
                                   forControlEvents:UIControlEventEditingChanged];
            [profileView addSubview:nameField];
        }

  
   // To resign first responder/keyboard //
   
        -(BOOL)textFieldShouldReturn:(UITextField *)textField
            {
                [textField resignFirstResponder];
                NSLog(@"%@ ",textField.text);                       // To get/print the data from the TextField
                return YES;
            }

        -(void)valueChanged{
            // To Limit text field text length //
            if ([nameField.text length] > 12) {
                nameField.text = [nameField.text substringToIndex:12];
            }
        }

    /*---------Some other important UITextField delegates---------*/
    -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        return YES;
    }

    - (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
        return YES;
    }

    - (void) textFieldDidBeginEditing:(UITextField *)textField
    {
   
    }