I will share some code. Please go through this code.
Your Ques : if the user slides the finger in any point of the screen above the sprite _player, I want to move the sprite _player up.
Ans :
This is for, if you touch the particular sprite and move that sprite......
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint point=[myTouch locationInView:[myTouch view]];
point=[[CCDirector sharedDirector] convertToGL:point];
NSLog(@"point is %@",NSStringFromCGPoint(point));
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint point=[myTouch locationInView:[myTouch view]];
point=[[CCDirector sharedDirector] convertToGL:point];
CCNode *Sprite=[self getChildByTag:spriteTag]; // This is your sprite, which one you want to move.
[Sprite setPosition:point];
// This is the boundary for sprite, for what sprite is nor cross the screen.
if(Sprite.position.y>245)
[Sprite setPosition:ccp(Sprite.position.x,245)];
if(Sprite.position.y<25)
[Sprite setPosition:ccp(Sprite.position.x,25)];
if(Sprite.position.x<15)
[Sprite setPosition:ccp(15,Sprite.position.y)];
if(Sprite.position.x>465)
[Sprite setPosition:ccp(465,Sprite.position.y)];
}
This is for, if you touch anywhere on screen and move that sprite ......
// CGPoint initialPos; // in .h file
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPt = [touch locationInView:touch.view];
initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
I think this will help you :)