【问题标题】:Detect touches on two separate sprite nodes检测两个独立精灵节点上的触摸
【发布时间】:2018-02-01 18:42:11
【问题描述】:

所以我在这里尝试做的是能够移动四个精灵节点中的两个,彼此独立。我使用枚举来检测触摸了哪些幻灯片,然后使用该信息移动正确的节点,我知道我可以使用节点的名称,但似乎更适合我正在尝试做的事情。到目前为止一切正常,除了我一次只能移动一个节点。我希望能够移动...比如说,leftSlide 向上,同时向右移动向下。

感谢您的任何建议或cmets,欢迎任何反馈..

var selectedSlides: [SKSpriteNode] = []
var theSlideTouched: SlideTouched = .nothing

enum SlideTouched
{
    case left
    case right
    case bottom
    case top
    case nothing
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{   
    for touch in touches
    {

        if leftSlide.contains((touch.location(in: self)))
        {
            selectedSlides.append(leftSlide)
            theSlideTouched = .left

        }

        if rightSlide.contains((touch.location(in: self)))
        {
            selectedSlides.append(rightSlide)
            theSlideTouched = .right

        }

        if bottomSlide.contains((touch.location(in: self)))
        {
            selectedSlides.append(bottomSlide)
            theSlideTouched = .bottom
        }

        if topSlide.contains((touch.location(in: self)))
        {
            selectedSlides.append(topSlide)
            theSlideTouched = .top
        }
     }
 }

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

        switch theSlideTouched
        {
            case .bottom:
                if selectedSlides.contains(bottomSlide)
                {
                    bottomSlide.color = UIColor.white
                    bottomSlide.position.x = location.x
                }
            case .top:
                if selectedSlides.contains(topSlide)
                {
                    topSlide.color = UIColor.white
                    topSlide.position.x = location.x
                }
            case .left:
                if selectedSlides.contains(leftSlide)
                {
                    leftSlide.color = UIColor.white
                    leftSlide.position.y = location.y
                }
            case .right:
                if selectedSlides.contains(rightSlide)
                {
                    rightSlide.color = UIColor.white
                    rightSlide.position.y = location.y
                }
            case .nothing: break
        }
    }

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
        switch theSlideTouched
        {
            case .bottom: bottomSlide.color = UIColor.clear
            case .top: topSlide.color = UIColor.clear
            case .left: leftSlide.color = UIColor.clear
            case .right: rightSlide.color = UIColor.clear
            case .nothing: break
        }
            theSlideTouched = .nothing
            selectedSlides.removeAll()
}

【问题讨论】:

    标签: swift sprite-kit multi-touch uitouch touchesbegan


    【解决方案1】:

    我会将滑块对象移动到它们自己的类中。这样您就可以独立处理它们的运动,而不会用这些对象弄乱您的 GameScene。现在,当我创建此示例时,我将对象直观地放置在整个场景编辑器的 GameScene.sks 文件中,因此如果您在代码中创建这些对象,我的初始化可能与您的不同。

    在游戏场景中...

    var topSlide: DragObject!
    var rightSlide: DragObject!
    var bottomSlide: DragObject!
    var leftSlide: DragObject!
    
    override func didMove(to view: SKView) {
    
        self.view!.isMultipleTouchEnabled = true
    
        if let topSlide = self.childNode(withName: "topSlide") as? DragObject {
            self.topSlide = topSlide
            topSlide.type = .top
        }
    
        //if you were to create this in code you could do
        //topSlide = DragObject(color: .red, size: CGSize(width: 100, height: 100)
        //topSlide.type = .top
        //topSlide.position = CGPoint(x: 500, y: 500)
        //topSlide.zPosition = 1
        //addChild(topSlide)
    
        if let rightSlide = self.childNode(withName: "rightSlide") as? DragObject {
            self.rightSlide = rightSlide
            rightSlide.type = .right
        }
    
        if let bottomSlide = self.childNode(withName: "bottomSlide") as? DragObject {
            self.bottomSlide = bottomSlide
            bottomSlide.type = .bottom
        }
    
        if let leftSlide = self.childNode(withName: "leftSlide") as? DragObject {
            self.leftSlide = leftSlide
            leftSlide.type = .left
        }
    }
    

    在 DragObject 类中...

    class DragObject: SKSpriteNode {
    
        enum SlideType {
            case left
            case right
            case bottom
            case top
            case nothing
        }
    
        var type: SlideType = .nothing
    
        init(color: SKColor, size: CGSize) {
    
            super.init(texture: nil, color: color, size: size)
    
            setup()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            setup()
        }
    
        func setup() {
            self.isUserInteractionEnabled = true
        }
    
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        }
    
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    
            for touch in touches {
                let location = touch.location(in: self.parent!)
    
                self.color = .white
                if type == .bottom || type == .top {
                    position.x = location.x
                }
                else if type == .left || type == .right {
                    position.y = location.y
                }
            }
        }
    
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            self.color = .red
        }
    }
    

    【讨论】:

    • 非常感谢,请教几个问题。你为什么要把 touchesBegan 留空? self.view!.isMultipleTouchEnabled = true 和 isUserInteractionEnabled = true 可以在 viewController 中吗?
    • 我让 touchesBegan 开始为空,以表明不需要在其中放置任何内容,并忽略 self.view!.isMultipleTouchEnabled = true 这是我的第一个想法,但没有必要
    • 您的拖动对象类存在缺陷,如果您拖动手指的速度足够快以在下一次读取之前将手指从对象上移开,那么在您释放时touchesEnded 将不会被调用跨度>
    • @Knight0fDragon 值得注意的是 OP。我把它写得又快又脏,是为了让 OP 走上一条他们希望可以修改和适应他们需要的道路。我并不清楚 OP 的目标是什么,他们 touchesEnded (通过使对象 .clear 之后)所以我更专注于让 2 个对象同时移动。我还注意到大量新发帖者提出问题,但从不回复评论或批准答案,因此现在很难证明花更多时间在答案上只是为他们做工作是合理的。
    • 哦,我同意 100%,这就是我的活动下降的原因。我并没有试图把你挑出来或任何事情,只是做个笔记以防你不知道会发生的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多