Jonathan
Jonathan

Reputation: 2383

Touch is Currently Moving - Cocos2d

Is there a way to tell if the touch is currently moving? If so could someone lead me to the right place?

Upvotes: 0

Views: 189

Answers (2)

ShinuShajahan
ShinuShajahan

Reputation: 1296

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    //Here urSprite is your CCSprite which you want to move
    CGRect urSpriteRect = CGRectMake([urSprite position].x - [urSprite contentSize].width/2, 
                          [urSprite position].y - [urSprite contentSize].height/2, 
                          [urSprite contentSize].height, [urSprite contentSize].width);

    if (CGRectContainsPoint(urSpriteRect, location)) 
    {
           checkFlag = TRUE;
           NSLog(@"You Touched the sprite!");
    }

}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if(checkFlag){

          urSprite.position = location;
          NSLog(@"You are moving your sprite!");
    }
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    checkFlag = FALSE;
    NSLog(@"Sprite stopped!");

}

Upvotes: 1

Michael Fredrickson
Michael Fredrickson

Reputation: 37378

If your layer has touches enabled, you can implement the following method in your layer to handle move events for touches...

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

Upvotes: 1

Related Questions