[英]How to detect touches in physicsBody area?
我確實使用此代碼創建了SKSpriteNode,而physicsBody是圓形的。 我如何檢查用戶是否在PhysicsBody中觸摸耳朵?
self = [super initWithColor:[SKColor clearColor] size:CGSizeMake(200, 200)];
//...
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2.0f];
self.userInteractionEnabled = YES;
我確實使用以下代碼找到了解決問題的方法:
CGMutablePathRef pathRef;
// Fill pathRef with points and create phisycs body using pathRef.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if (CGPathContainsPoint(pathRef, nil, touchLocation, NO)) {
// If we are here it means user did touche physic body.
}
}
最簡單的解決方案是創建第二個不可見的精靈,它具有所需的大小,並將其直接放在要記錄觸摸的區域上。
另一個選擇是存儲所有觸發有效觸摸的坐標,並將它們與您的觸摸方法中的實際觸摸坐標進行比較,如下所示:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
NSLog(@"%@", NSStringFromCGPoint(touchLocation));
// Compare the touchLocation coordinate against your list of acceptable coordinates...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.