in cocos2d, the coordinate system is pretty straightforward.. bottom left is (0,0). whenever i set the position of anything it's fine.
lets say a layer is TouchEnabled.
when im setting the position of my sprite, it's fine..
[crab setPosition:ccp(20, 50)];
but if i get the x and y coordinate of a touch (and assume i clicked on the sprite..):
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches began!");
NSArray *touchArray = [touches allObjects];
UITouch *one = [touchArray objectAtIndex:0];
pointOne = [one locationInView:[one view]];
CCLOG(@"points are: %f and %f", pointOne.x, pointOne.y );
}
the y coordinate is the opposite of what cocos2d's coordinate system suggests as the y increases from top to bottom !
so im assuming the coordinate system of the view is different than mac's coord system.. which is quite counter intuitive.. is that correct?
thanks !
OpenGL uses a (0, 0) of bottom left while iOS and therefore UIKit which handles the touches use top left as (0, 0). It is easy to convert your point over, you just have to remember to do it.
The correct method on cocos2d 2.0 is
CGPoint uiKitPoint = [touch locationInView:touch.view];
CGPoint cocos2dPoint = [[CCDirector sharedDirector] convertToGL:uiKitPoint];
This cocos2d tutorial might help you
http://www.gamefromscratch.com/post/2012/06/08/Cocos2D-HTML-Tutorial-3All-about-sprites-and-positioning.aspx
But generally speaking in cocos2d point (0,0) is the left corner of the screen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With