2014-03-19 10 views
14

In welcher Methode füge ich einen UIGestureRecognizer zu meinem SKScene hinzu. Und wie finde ich heraus, welcher Knoten kopiert wurde? Dies gilt nicht zu funktionieren scheint:SpriteKit: UIGestureRecognizer hinzufügen und feststellen, welcher Knoten kopiert wurde

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

note Genießen Sie, dass die älteren Antworten hier während in der Regel korrekt einige Sachen enthalten, die – Fattie

Antwort

30

Sie fügen die UISwipeGestureRecognizer in dieser Methode:

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


Und das ist, wie Sie erkennen, welche SKNode geklaut wurde:

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

gebrochen geworden Es funktioniert gut! Und vergessen Sie nicht die Umkehr der Y-Achse in Sprite-Kit – djdance

+0

Das funktionierte wie ein Charme! Danke ein Bündel – Septronic

+0

Wie man Y-Mittellinie bitte umkehrt? (Swift) – fredericdnd

3

Swift 3 Version inspiriert von SE Antwort:

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 
} 
0

Sie können an einer Stelle der Knoten überprüfen umfassen einen Knoten Sie für die Verwendung dieser Suche:

nodes(at: touchLocation).contains(nodeYouAreLookingFor) 
2

Für 2017 ...

Sagen Sie eine Szene mit verschiedenen Sprites haben.

Tippen Sie auf eine davon. Sie müssen wissen, was man abgehört wurde ...

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

Verwandte Themen