Showing posts with label began. Show all posts
Showing posts with label began. Show all posts

Sunday, 28 July 2013

Touch listener in Corona SDK

We can assign touch listener to an object in corona as follows:


-- Create the object --
local rect = display.newRect(100,100,50,50)
rect:setFillColor(255,0,0)

-- Create the touch function --
local function myTouchFunction(event)
    if(e.phase=="began")then
         print("Touch begins...")
    elseif(e.phase=="moved")then
         print("Touch moved...")
    elseif(e.phase=="ended")then
         print("Touch ended...")
    end
end

-- Create the listener to the object --
rect:addEventListener("touch",myTouchFunction)








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
    {
   
    }