Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 检测两个单独的精灵节点上的接触_Swift_Sprite Kit_Multi Touch_Uitouch_Touchesbegan - Fatal编程技术网

Swift 检测两个单独的精灵节点上的接触

Swift 检测两个单独的精灵节点上的接触,swift,sprite-kit,multi-touch,uitouch,touchesbegan,Swift,Sprite Kit,Multi Touch,Uitouch,Touchesbegan,所以我在这里要做的是,能够移动四个sprite节点中的两个,彼此独立。我使用一个枚举来检测滑动的内容,然后使用该信息来移动正确的节点,我知道我可以使用节点的名称,但似乎可以更好地处理我正在尝试的操作。到目前为止,除了一次只能移动一个节点外,其他一切都正常。我希望能够搬家。。。比如说,向左向上滑动,同时向右向下滑动 感谢您的任何建议或意见,欢迎您提供任何反馈 var selectedSlides: [SKSpriteNode] = [] var theSlideTouched: SlideTouc

所以我在这里要做的是,能够移动四个sprite节点中的两个,彼此独立。我使用一个枚举来检测滑动的内容,然后使用该信息来移动正确的节点,我知道我可以使用节点的名称,但似乎可以更好地处理我正在尝试的操作。到目前为止,除了一次只能移动一个节点外,其他一切都正常。我希望能够搬家。。。比如说,向左向上滑动,同时向右向下滑动

感谢您的任何建议或意见,欢迎您提供任何反馈

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()
}
var selectedSlides:[SKSpriteNode]=[]
var theSlideTouched:SlideTouched=.nothing
滑动接触枚举
{
案例左
案例权利
箱底
箱盖
没什么
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?)
{   
接触
{
如果leftSlide.contains((touch.location(in:self)))
{
selectedSlides.append(左幻灯片)
滑梯=左
}
如果rightSlide.contains((touch.location(in:self)))
{
selectedSlides.append(右幻灯片)
滑梯=右
}
如果底部幻灯片包含((触摸位置(in:self)))
{
selectedSlides.append(底部幻灯片)
滑块=底部
}
如果上滑道包含((接触位置(in:self)))
{
所选幻灯片。附加(上滑块)
滑块=顶部
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?)
{
接触
{
让位置=触摸。位置(in:self)
开关滑块
{
案例底部:
如果选择了幻灯片。包含(底部幻灯片)
{
bottomSlide.color=UIColor.white
底部滑块.position.x=位置.x
}
案例顶部:
如果选择了Slides.contains(上滑块)
{
topSlide.color=UIColor.white
上滑块位置x=位置x
}
案例左:
如果选择了幻灯片。包含(左幻灯片)
{
leftSlide.color=UIColor.white
leftSlide.position.y=位置.y
}
案例.右侧:
如果选择了幻灯片。包含(右幻灯片)
{
rightSlide.color=UIColor.white
右滑块.position.y=位置.y
}
箱子,没什么,破了
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?)
{
开关滑块
{
case.bottom:bottomSlide.color=UIColor.clear
case.top:topSlide.color=UIColor.clear
case.left:leftSlide.color=UIColor.clear
case.right:rightsiled.color=UIColor.clear
箱子,没什么,破了
}
滑梯=没有
selectedSlides.removeAll()
}

我会将滑块对象移动到它们自己的类中。这样你就可以独立处理它们的移动,而不会因为这些物体而把游戏场景弄得乱七八糟。现在,当我创建这个示例时,我在整个场景编辑器中将对象可视化地放置在GameSecene.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
    }
}
类DragObject:SKSpriteNode{
枚举滑动类型{
案例左
案例权利
箱底
箱盖
没什么
}
变量类型:SlideType=.nothing
初始化(颜色:SKColor,大小:CGSize){
super.init(纹理:nil,颜色:color,大小:size)
设置()
}
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
设置()
}
函数设置(){
self.isUserInteractionEnabled=true
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
接触{
让位置=touch.location(在:self.parent!)
self.color=.white
如果类型=.bottom | |类型=.top{
位置.x=位置.x
}
如果type=.left | | type=.right,则为else{
position.y=location.y
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
self.color=.red
}
}

非常感谢,有几个问题。你为什么让ToucheSStart空着?并且可以自我查看!。isMultipleTouchEnabled=true和isUserInteractionEnabled=true是否在viewController中?我将touchesStart保留为空,以显示不需要任何内容,并忽略self.view!。isMultipleTouchEnabled=true这是我的第一个想法,但没有必要你的drag object类有缺陷,如果你的手指拖动速度足够快,在下次读取之前将手指从对象上取下,
touchesend
在您release@Knight0fDragon这篇文章值得注意。我写得很快,很脏,让文章走上了一条他们可以根据自己的需要进行修改和调整的道路。我真的不太清楚OP的目标是什么,他们的触控是固定的(通过使对象在之后清除),所以我更专注于让两个对象同时移动。我还注意到大量新的海报,它们提出问题,却从不回来评论或批准答案,所以现在很难证明花额外的时间在答案上为他们做工作是合理的。哦,我100%同意,这就是我的活动减少的原因。我不是想把你挑出来或者做什么,只是做个记录,以防你不知道会发生什么行为。