我有一个类似于 UIButton 的节点和一个在触摸屏幕时执行操作的节点。
问题是狮子跳跃 Action 没有被触发。
另外:两个节点的名字都是正确的。
我的 UIEvent 代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode: self];
SKNode *node = [self nodeAtPoint:location];
UITouch *lionTouch = [touches anyObject];
CGPoint locationLion = [lionTouch locationInView:[self view]];
SKNode *lionNode = [self nodeAtPoint:locationLion];
if ([lionNode.name isEqualToString:@"veggieLion"]) {
if (!lionIsJumping) {
[self lionJump];
lionIsJumping = YES;
}
}
if ([node.name isEqualToString:@"repalyButton"]) {
// Button action
}
}
最佳答案
Apple documentation for SKNode说方法nodeAtPoint
“返回与点相交的最深后代。”
我过去也遇到过这个问题。该方法将返回我用于背景的 SKSpriteNode,即使我肯定是在点击目标 Sprite /节点!
由于您正在检查两个节点,我将通过以下方式处理它:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode: self];
SKNode * lionNode = [self childNodeWithName:@"veggieLion"];
SKNode * replayButton = [self childNodeWithName:@"repalyButton"];
if ([self.lionNode containsPoint:location]) {
if (!lionIsJumping) {
[self lionJump];
lionIsJumping = YES;
}
return;
}
if ([self.replayButton containsPoint:location]) {
// Button action
return;
}
/* test for other targets */
}
关于ios - 检测 UIView 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957429/