Ios SpriteKit:添加UIgestureRecognitor并检测哪个节点被刷过

Ios SpriteKit:添加UIgestureRecognitor并检测哪个节点被刷过,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我用哪种方法向我的SKScene添加uigesturecognizer。我如何检测哪个节点被刷过?这似乎不起作用: -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { ... UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self acti

我用哪种方法向我的
SKScene
添加
uigesturecognizer
。我如何检测哪个节点被刷过?这似乎不起作用:

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {

    ...

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [[self view] addGestureRecognizer:recognizer];

    }
    return self;
}

您可以在此方法中添加
UISweepGestureRecognitor

- (void)didMoveToView:(SKView *)view
{
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [[self view] addGestureRecognizer:recognizer];
}

这就是您检测哪个
SKNode
被刷卡的方式:

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint touchLocation = [sender locationInView:sender.view];
        touchLocation = [self convertPointFromView:touchLocation];
        SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];

        NSLog(@"%@", touchedNode);
    }
}

Swift 3版本灵感来源于S.E.的答案:

func didSceneViewPan(_ sender: UIPanGestureRecognizer) {
    if sender.state == .began {

        let touchPoint = sender.location(in: sender.view)
        let touchLocation = convertPoint(fromView: touchPoint)

        if isPointInNode(point: touchLocation, node: testNode) { 
            print("yes, node touched!")
        } else {
            print("no bueno :(")
        }
     }
 }


fileprivate func isPointInNode(point: CGPoint, node: SKNode) -> Bool {
    // Get all nodes intersecting <point>
    let nodes = self.nodes(at: point)

    // Return true on first one that matches <node>
    for n in nodes {
        if n == node {
            return true
        }
    }

    // If here, <point> not inside <node> so return false
    return false
}
func-didSceneViewPan(uu-sender:uipangestureerecognizer){
如果sender.state==。开始{
让touchPoint=sender.location(在:sender.view中)
让touchLocation=convertPoint(fromView:touchPoint)
如果isPointInNode(点:touchLocation,节点:testNode){
打印(“是,节点已触摸!”)
}否则{
打印(“无bueno:(”)
}
}
}
fileprivate func isPointInNode(点:CGPoint,节点:SKNode)->Bool{
//使所有节点相交
让节点=self.nodes(在:点)
//在第一个匹配的值上返回true
对于n个节点{
如果n==节点{
返回真值
}
}
//如果在这里,不在里面,那么返回false
返回错误
}

您可以使用以下方法检查某个位置的节点,包括要查找的节点:

nodes(at: touchLocation).contains(nodeYouAreLookingFor)
2017年。。。 假设你有一个有各种精灵的场景

点击其中一个。你需要知道哪一个被点击了

class YourScene: SKScene {
    override func didMove(to view: SKView) {
        
        super.didMove(to: view)
        
        // your setup for the scene - example, add objects etc
        
        setupTapDetection()
    }
    
    func setupTapDetection() {
        
        let t = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
        view?.addGestureRecognizer(t)
    }
    
    @objc func tapped(_ tap: UITapGestureRecognizer) {
        
        // critical...
        if tap.state != .ended { return }
        
        // Note: you actually do NOT "invert the Y axis",
        // these two calls in fact take care of that properly.
        let viewPoint = tap.location(in: tap.view)
        let scenePoint = convertPoint(fromView: viewPoint)
        
        // technically there could be more than one node...
        let nn = nodes(at: scenePoint)
        
        if nn.count == 0 {
            
            print("you very like tapped 'nowhere'")
            return
        }
        
        if nn.count != 1 {
            
            // in almost all games or physics scenes,
            // it is not possible this would happen
            print("something odd happened - overlapping nodes?")
            return
        }
        
        // you must have a custom class for your sprites
        // Say it is class YourSprites: SKSpriteNode
      
        if let dotSpriteTapped = nn.first! as? YourSprites {
            
            // we are done. dotSpriteTapped is the YourSprite,
            // which was tapped. now do whatever you want.
            
            let nm = String(describing: dotSpriteTapped.name)
            print("   \(nm)")
            
            // you can now access your own properties:
            
            let whichTank = dotSpriteTapped.someID
            print("   \(whichTank)")
            
            let tankPower = dotSpriteTapped.sheildStrength
            print("   \(tankPower)")
        }
    }

尽情享受吧

它工作得很好!别忘了在sprite Kit中反转Y轴。在这个设置中,您实际上不需要反转Y轴。转换器似乎可以做到这一点。请注意,这里的旧答案虽然通常正确,但包含一些已损坏的内容