首页 > 解决方案 > 如何从数组中移动单个 SKspritenode

问题描述

我有一个精灵数组,点击它时移动它,问题是它正在移动所有精灵,我希望它移动单个元素,我该怎么做?先感谢您

这是我的代码:

var containerPieces: [ContainerPieces] = []
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            let rsCurrent = self.atPoint(location)


            for nodo in containerPieces {
                let rsBody = nodo.block.physicsBody
                if rsBody == rsCurrent.physicsBody  {
                    if nodo.block.frame.contains(location) {
                        nodo.block.position = location
                        nodo.block.run(blinkAnimation(), withKey:"wiggle")
                        isFingerOnPaddle = true
                    }

                }
        }


        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if isFingerOnPaddle {

            let touch = touches.first
            let touchLocation = touch!.location(in: self)
            let previousLocation = touch!.previousLocation(in: self)


            for nodo in containerPieces{

                var paddleX = nodo.block.position.x + (touchLocation.x - previousLocation.x)

                var paddleY = nodo.block.position.y + (touchLocation.y - previousLocation.y)

                paddleX = max(paddleX, nodo.block.size.width/2)
                paddleX = min(paddleX, size.width - nodo.block.size.width/2)

                paddleY = max(paddleY, nodo.block.size.width/2)
                paddleY = min(paddleY, size.width - nodo.block.size.width/2)

                nodo.block.position = CGPoint(x: paddleX, y: paddleY)
                            }

        }
    }

标签: iosswiftsprite-kit

解决方案


推荐阅读