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

时间:2014-03-19 16:52:54

标签: ios objective-c sprite-kit

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

-(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;
}

4 个答案:

答案 0 :(得分:32)

您在此方法中添加UISwipeGestureRecognizer

- (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);
    }
}

答案 1 :(得分:4)

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
}

答案 2 :(得分:2)

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)")
        }
    }

享受

答案 3 :(得分:1)

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

nodes(at: touchLocation).contains(nodeYouAreLookingFor)