0

我正在尝试构建一个棋盘游戏,其中棋盘由精灵构建,您可以使用 SKCamera 节点以鸟瞰方式查看它们。我已经进行了平移、缩放和向下旋转,但它们感觉不自然,因为无论我将手指放在屏幕上的哪个位置,它都会围绕相机节点的中心旋转。

为了解决这个问题,我将创建一个 SKNode 并将其放置在手指之间作为相机旋转和缩放的锚点。

但是我无法让手指中心点的位置与游戏场景中的正确坐标对齐。

我创建了一些红色框来调试并找到我的问题,看起来像这样: 游戏场景截图

每当我将手指放在整个游戏场景中以旋转它时,它认为它应该在的中心总是在那个红色斑点中。

SKView 作为 SpriteKit 视图放置在情节提要上,并通过顶部和底部填充绑定到屏幕的左右边缘:

视图控制器内部:

    @IBOutlet weak var Gameview: SKView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = GameScene(size:Gameview.frame.size)
        Gameview.presentScene(scene)
    }

GameScene.swift 内部let a = sender.location(in: self.view)似乎是问题所在。

class GameScene: SKScene, UIGestureRecognizerDelegate {

    var newCamera: SKCameraNode!

    let rotateRec = UIRotationGestureRecognizer()
    let zoomRec = UIPinchGestureRecognizer()
    //let panRec = UIPanGestureRecognizer()


    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
                           shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)
        -> Bool {return true}



    override func didMove(to view: SKView) {

        rotateRec.addTarget(self, action: #selector(GameScene.rotatedView (_:) ))
        rotateRec.delegate = self
        self.view!.addGestureRecognizer(rotateRec)

        zoomRec.addTarget(self, action: #selector(GameScene.zoomedView (_:) ))
        zoomRec.delegate = self
        self.view!.addGestureRecognizer(zoomRec)

        //panRec.addTarget(self, action: #selector(GameScene.pannedView (_:) ))
        //self.view!.addGestureRecognizer(panRec)


        newCamera = SKCameraNode()
        newCamera.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
        self.addChild(newCamera)
        self.camera = newCamera

        //drawBoard()
        //I removed this function as it had nothing to do with the question

    }

    @objc func rotatedView(_ sender:UIRotationGestureRecognizer) {
        print(sender.location(in: self.view))
        let square = SKSpriteNode(color: #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1), size: CGSize(width: 10, height: 10))
        let a = sender.location(in: self.view)
        let b = -a.x
        let c = -a.y
        let d = CGPoint(x: b, y: c)


        square.position = d
        self.addChild(square)


        newCamera.zRotation += sender.rotation
        sender.rotation = 0
        if sender.state == .ended {
            let rotateAmount = abs(Double(newCamera.zRotation) * Double(180) / Double.pi)
            let remainder = abs(rotateAmount.truncatingRemainder(dividingBy: 90))
            //print(rotateAmount)
            //print(remainder)
            if remainder < 10 {
                newCamera.zRotation = CGFloat((rotateAmount-remainder)*Double.pi/180)
            } else if remainder > 80 {
                newCamera.zRotation = CGFloat((rotateAmount-remainder+90)*Double.pi/180)
            }

        }

    }



    @objc func zoomedView(_ sender:UIPinchGestureRecognizer) {
        let pinch = SKAction.scale(by: 1/sender.scale, duration: 0.0)
        newCamera.run(pinch)
        sender.scale = 1.0
    }

    //@objc func pannedView(_ sender:UIPanGestureRecognizer) {
    //    sender.translation(in: )
    //}


    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            let previousLocation = touch.previousLocation(in: self)
            let deltaY = location.y - previousLocation.y
            let deltax = location.x - previousLocation.x
            newCamera!.position.y -= deltaY
            newCamera!.position.x -= deltax
        }
    }
}

我确实删除了函数 drawboard() 因为它在问题的上下文中是多余的。

4

1 回答 1

0

您需要使用页面中显示的功能将坐标从 sender.location 转换为 skview 中的坐标:

https://developer.apple.com/documentation/spritekit/skscene

--> https://developer.apple.com/documentation/spritekit/skscene/1520395-convertpoint

在 rotateview() 函数中,放置代码:

let bluesquare = SKSpriteNode(color: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), size: CGSize(width: 10, height: 10))

let a = sender.location(in: self.view)
bluesquare.position = self.convertPoint(fromView: a)

self.addChild(bluesquare)

...将在您的两个手指的中心位置添加蓝色精灵。

于 2017-09-21T15:41:06.593 回答