无法检测UItouch
问题描述:
我有一个大图像(1024,1496)作为我的图层的背景图像。于是我开始显示图像的按钮。 在应用程序的时候我显示图像的顶部,以及一些CCSprites。无法检测UItouch
id move = [CCMoveBy actionWithDuration:2 position:ccp(0,-746)];
[layer runAction:move];
我的问题是,我无法检测CCSrites上的触摸,因为他们的位置保持不变。 (20,1200)和(1024,746)之间的UITouch。
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for(UITouch * touch in [event allTouches]){
for (CCSprite *book in books) {
CGPoint location = [touch locationInView:touch.view];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint([book boundingBox], location))
NSLog(@"Touch");
else{
NSLog(@"NO Touch"); }
}
}
}
不知道如何解决这个问题?
答
只需将精灵触摸转换为世界坐标。您可以通过计算基于缩放和精灵大小的屏幕位置,或者使用Cocos中的方法转换为世界坐标来使用自己的方法执行此操作。我倾向于做前者。