如何在Spritekit中向节点添加滑动手势 [英] How to add a swipe gesture to a node in spritekit

查看:144
本文介绍了如何在Spritekit中向节点添加滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向节点添加滑动手势,以便当用户滑动该手势时,它会离开屏幕,但是我一直收到SIGABRT错误:

I'm trying to add a swipe gesture to a node so that when a user swipes it, it goes off screen but I keep getting a SIGABRT error:

`Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[fidget2.PlankScene swipedRight:]: unrecognized selector sent to instance 0x7ff4c3603e00'`

我不确定为什么会弹出此错误.我确保在.sks文件中正确标记了该节点.这是我的代码:

I'm not sure why this error is popping up. I made sure the node is labeled correctly in the .sks file. Here is my code:

import SpriteKit


let plankName = "woodPlank"

class PlankScene: SKScene {

  var plankWood : SKSpriteNode?


  override func didMove(to view: SKView) {

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


    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))

    swipeRight.direction = .right

    view.addGestureRecognizer(swipeRight)

  }


  func swipedRight(sender: UISwipeGestureRecognizer) {

    print("Object has been swiped")

  }


 func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
  {
    let touch = touches.first as! UITouch
    let location = touch.location(in: self)

    if (plankWood?.frame.contains(location))!
    {
     print("Swipe has started")
    }
  }
}

推荐答案

有相同的问题,只是我们对此有一个可以接受的答案,我想指出,0x141E留下的评论是对以下问题的正确解决方案这个:

Had the same problem, just so we have an accepted answer on this I would like to point out that the comment left by 0x141E is the correct solution to this:

Selector("swipedRight:")替换为#selector(PlankScene.swipedRight)

这篇关于如何在Spritekit中向节点添加滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆