Sprite kit 如何检测是否触摸了SKShapeNode?
我创造了一个sknodeshape。如何检测我制作的形状是否被触摸(单击) 下面是代码:(我已经解决了)Sprite kit 如何检测是否触摸了SKShapeNode?,sprite-kit,sknode,skshapenode,Sprite Kit,Sknode,Skshapenode,我创造了一个sknodeshape。如何检测我制作的形状是否被触摸(单击) 下面是代码:(我已经解决了) 我的错误是,当我创建形状时,我在初始化它之前输入了SKShapeNode名称。实现touch-delegate方法。然后,在-touchesbeated:方法中,提取接触点并使用[self nodeAtPoint:]方法检索节点 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Called wh
我的错误是,当我创建形状时,我在初始化它之前输入了SKShapeNode名称。实现touch-delegate方法。然后,在-touchesbeated:方法中,提取接触点并使用[self nodeAtPoint:]方法检索节点
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *yourNode = ....;
CGRect yourNodeFrame = bowlNode.frame;
if (CGRectContainsPoint(yourNodeFrame, location)) {
//your node may be touched, check if it's your node
SKNode *theNode = [self nodeAtPoint:location];
if ([theNode.name isEqualTo:yourNode.name]) {
//it's your node touched
}
}
}
方法
nodeAtPoint
返回值很复杂。您可以检查文档以找到不同的情况实际形状(路径)或其框架?您的解决方案肯定是错误的。不管怎样,只要使用我的代码,它就简单多了。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *yourNode = ....;
CGRect yourNodeFrame = bowlNode.frame;
if (CGRectContainsPoint(yourNodeFrame, location)) {
//your node may be touched, check if it's your node
SKNode *theNode = [self nodeAtPoint:location];
if ([theNode.name isEqualTo:yourNode.name]) {
//it's your node touched
}
}
}