Ios 如何子类化一个SKSpriteNode并实现touchesbreated?

Ios 如何子类化一个SKSpriteNode并实现touchesbreated?,ios,swift,sprite-kit,skspritenode,Ios,Swift,Sprite Kit,Skspritenode,我正在尝试创建一个能够刷离屏幕的节点。我尝试在视图中添加UIGestureRecognitor,但滑动仅适用于第一个节点。我听说有一种方法可以将spriteNode子类化,并在Touches中开始添加滑出屏幕的功能。我可以设置一个自定义精灵节点的基础,但不确定如何使其能够被刷卡。以下是我的自定义精灵节点代码: class CustomNode: SKSpriteNode { init() { let texture = SKTexture(imageNamed: customNod

我正在尝试创建一个能够刷离屏幕的节点。我尝试在视图中添加UIGestureRecognitor,但滑动仅适用于第一个节点。我听说有一种方法可以将spriteNode子类化,并在Touches中开始添加滑出屏幕的功能。我可以设置一个自定义精灵节点的基础,但不确定如何使其能够被刷卡。以下是我的自定义精灵节点代码:

class CustomNode: SKSpriteNode {
  init() {

    let texture = SKTexture(imageNamed: customNodeImage)
    super.init(texture: texture, color: SKColor.clear, size: texture.size())
  }

  required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}
在我的场景中确实有一个touchestarted,但是当我触摸一个节点时,当我尝试打印节点的名称时,我会得到一个“nil”响应。这是我的场景代码:

import SpriteKit


let plankName = "woodPlank"

class PlankScene: SKScene {

  var plankWood : Plank?


  var plankArray : [SKSpriteNode] = []



  override func didMove(to view: SKView) {

    plankWood = childNode(withName: "woodPlank") as? Plank

    plankWood?.isUserInteractionEnabled = false

    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))

    swipeRight.direction = .right

    view.addGestureRecognizer(swipeRight)

  }


  func swipedRight(sender: UISwipeGestureRecognizer) {



    if sender.direction == .right {

      let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5)

      let nodeFinishedMoving = SKAction.removeFromParent()

      let waitForNode =   SKAction.wait(forDuration: 0.5)


      plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]),
                     completion:{

      } )
    }
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    let touchLocation = touch!.location(in: self)

    print("\(plankWood?.name)")
      if plankWood?.name == plankName {
        print("Plank touched")
    }
  }

  override func enumerateChildNodes(withName name: String, using block: @escaping (SKNode, UnsafeMutablePointer<ObjCBool>) -> Void) {

    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))

    swipeRight.direction = .right

    view?.addGestureRecognizer(swipeRight)

  }
}
导入SpriteKit
让plankName=“woodPlank”
课堂场景:SKScene{
木板:木板?
变量数组:[SKSpriteNode]=[]
覆盖func didMove(到视图:SKView){
plankWood=childNode(名称为“woodPlank”)作为?Plank
plankWood?.isUserInteractionEnabled=false
让swipeRight:uisweepGestureRecognizer=uisweepGestureRecognizer(目标:self,动作:#选择器(PlankScene.swipedRight))
swipeRight.direction=.right
view.AddGestureRecognitor(swipeRight)
}
func swipedRight(发送方:UISweepGestureRecognitor){
如果sender.direction==.right{
让moveOffScreenRight=SKAction.moveTo(x:400,持续时间:0.5)
让nodeFinishedMoving=SKAction.removeFromParent()
让waitForNode=SKAction.wait(持续时间:0.5)
plankWood?运行(SKAction.sequence([moveOffScreenRight,nodeFinishedMoving]),
完成:{
} )
}
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
让我们先接触
让touchLocation=touch!.location(in:self)
打印(“\(plankWood?.name)”)
如果plankWood?.name==plankName{
印刷(“木板接触”)
}
}
重写func enumerateChildNodes(使用名称:String,使用块:@escaping(SKNode,UnsafeMutablePointer)->Void){
让swipeRight:uisweepGestureRecognizer=uisweepGestureRecognizer(目标:self,动作:#选择器(PlankScene.swipedRight))
swipeRight.direction=.right
查看?.AddGestureRecognitor(swipeRight)
}
}

要在节点上实现touchesbeated,只需添加
覆盖func touchesbeated
。。。到你的SKSpriteNode自定义类你有没有查看过我在上一个问题中发布的链接?;)要在节点上实现touchesbented,只需添加
覆盖func touchesbented
。。。到你的SKSpriteNode自定义类你有没有查看过我在上一个问题中发布的链接?;)