在 Spritekit 中,有没有办法处理临时创建的 SKSpriteNode 变量上的触摸事件?
In Spritekit, is there a way to handle touch event on a temporarily created SKSpriteNode variable?
我想检测触摸并在触摸 sprite 时对其做出反应,但我不希望使用它自己的 class 覆盖它(因此,在 touchesBegan 方法中处理)。我只是想让它被视为场景中的另一个精灵。我目前是这样做的:
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:location];
if(touchedNode == self.AnswerButton) {
NSLog(@"ANSWER button pressed.");
return;
}
if(touchedNode == self.HangupButton) {
NSLog(@"HANGUP button pressed.");
[self hangUp];
return;
}
但是,我想知道是否有更直接的方法来访问 self.HangupButon 和 self.AnswerButton 子精灵的触摸?
添加到 sprite 的点击手势识别器可能就是您要找的。
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/
您几乎已经确定了它,而无需子类化。
逻辑是:
1)触摸场景
2) 确定节点
3) 基于接触节点的处理。
我唯一可能会做的不同的事情是将它作为一个单独的函数并将 else 添加到第二个 if(如果这是 Swift 我会说使用开关)
我想检测触摸并在触摸 sprite 时对其做出反应,但我不希望使用它自己的 class 覆盖它(因此,在 touchesBegan 方法中处理)。我只是想让它被视为场景中的另一个精灵。我目前是这样做的:
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:location];
if(touchedNode == self.AnswerButton) {
NSLog(@"ANSWER button pressed.");
return;
}
if(touchedNode == self.HangupButton) {
NSLog(@"HANGUP button pressed.");
[self hangUp];
return;
}
但是,我想知道是否有更直接的方法来访问 self.HangupButon 和 self.AnswerButton 子精灵的触摸?
添加到 sprite 的点击手势识别器可能就是您要找的。
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/
您几乎已经确定了它,而无需子类化。
逻辑是:
1)触摸场景
2) 确定节点
3) 基于接触节点的处理。
我唯一可能会做的不同的事情是将它作为一个单独的函数并将 else 添加到第二个 if(如果这是 Swift 我会说使用开关)