I am using Cocos2D to develop a mini iPhone game.. I wanted to detect the touch of a sprite. To do so I decided not to subclass the CCSprite class but instead using the touch events in the layer class:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CCLOG(@"touch began...");
CCSprite *particularSprite = [self getChildByTag:artSprite];
CCNode *nodeClass = (CCNode*) particularSprite;
CGRect DesiredSprite = CGRectMake(nodeClass.positionInPixels.x, nodeClass.positionInPixels.y,particularSprite.contentSize.width , particularSprite.contentSize.height);
for (UITouch *myTouch in touches) {
CGPoint touchPosition = [myTouch locationInView: [myTouch view]];
if(CGRectContainsPoint(DesiredSprite ,touchPosition ))
{
CCLOG(@"Sprite touched");
}
}
}
Unfortunately the coordinates are wrong. The locationInView translates it differently. I am using the landscapeleft view (kCCDeviceOrientationLandscapeLeft).
Adding a breakpoint on the function and looking at the myTouch variable, then I see that it has a member variable called locationInWindow which reflects the actual touch position (which is what I want).
I tried to access to the locationInWindow but there is no getter method for it. How can I do so?
Many thanks and Best Regards
Window is a UIWindow
which is a UIView
subclass. Additionally UITouch
has a window
property. So you could try:
CGPoint touchPosition = [myTouch locationInView:myTouch.window];
The view's transform figures into the calculation; So you might also want to try self.superview
for the parameter.