How to calculate the touch location with convertToWorldSpace?

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2012-06-25T03:11:16Z Indexed on 2012/06/25 3:15 UTC
Read the original article Hit count: 212

i would like to convert the touch location as a world coordinate in my tile game. With this code, i clicked on the right of the screen (so that my character walks in the tiled game, and the background goes slowly to the left) :

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for( UITouch *touch in touches ) {
        CGPoint location = [touch locationInView: [touch view]];

        location = [[CCDirector sharedDirector] convertToGL: location];
        CGPoint test = [self convertToWorldSpace:location];

        CCLOG(@"test : %.2g", test.x);

The test log gives me :

50, 72, 1e+02, 2.6e+02, 4.2e+02, (and then goes down) 3.2e+02, 9.5, -1.9e+02, etc.

Does anyone know why? I would like to calculate the "real" coordinate of the touch, so that i know when the character has to keep going (click on the right of its actual position) or if he has to turn and go backwards. (click on the left of its actual position)

Thanks for your help

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocos2d-iphone