views:

72

answers:

2

Hi, I've created Cocos Apps, then inserted sprite in that. I moved sprite to mouse moved location.sprite's coordinates are out of the screen borders.

any one explain how can I convert sprite coordinates to screen coordinates? That would be in (320 * 480) format!!!

+2  A: 

Because the cocos coordinate system is "upside down" compared to the iPhone screen coordinate system, you need to do:

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

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];

    CGPoint touchCorrected;
    touchCorrected.x = point.x;
    touchCorrected.y = 480 - point.y;

}

If however your sprite is part of a hierarchy or stack of sprites, you need to convert the (corrected) touch coordinates into the sprites local coordinates using CCNodes convertToNodeSpace method.

Toastor
Sry it is not working!!!
A: 
-(BOOL)ccTouchBegan:(UITouch *)touches withEvent:(UIEvent *)event  
{  
    CGPoint location = [touches locationInView: [touches view]];  
    location = [[CCDirector sharedDirector] convertToGL:location];  
        NSLog(@"X=%f Y=%f",location.x,location.y);  
        return YES;  
}

It will returns the screen Coordinates (320 * 480)