Friday 22 July 2011

Getting the position of a touch on screen as cocos2d ccp

Ok, this one took a bit of working out. But if you want to get the cpp point of a player's touch on a layer, you need to use the following code, after having enabled touch on the screen etc (see earlier blog post). This converts a CGPoint into coordinates that cocos2d can understand.

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint beginTouchPoint= [touch locationInView: [touch view]];
    beginTouchPoint = [[CCDirector sharedDirector] convertToGL:beginTouchPoint];
    beginTouchPoint = [self convertToNodeSpace:beginTouchPoint];
    return true;

No comments:

Post a Comment