0

我正在制作一个贪吃蛇游戏。要移动蛇,玩家必须在屏幕上滚动。我真的需要知道哪个是卷轴的方向。起初我尝试了这个:我得到了玩家触摸的位置(pointOfTouch)以及上一个触摸点(previousPointOfTouch)。然后,如果previousPointOfTouch.X 减去pointOfTouch.X 大于0,则蛇向右移动。左、上和下也是如此(在上和下我得到 Y 坐标)

它应该可以工作,而且确实可以。但是只有一个问题,它太精确了。如果我向上滚动,就像我在网上冲浪时所做的那样,应用程序会发现一些向左或向右的小动作,然后左右移动蛇。我该如何解决这个问题?

这是我使用的代码

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch: AnyObject in touches{
            let pointOfTouch = touch.location(in: self)
            let previousPointOfTouch = touch.previousLocation(in: self)
            let amountDraggedX = pointOfTouch.x - previousPointOfTouch.x
            let amountDraggedY = pointOfTouch.y - previousPointOfTouch.y
            if amountDraggedY > 0 { //UP
                resetMoves() 
                moves[0] = true
            }
            else if amountDraggedY < 0 { //DOWN
                resetMoves()
                moves[1] = true
            }
            if amountDraggedX > 0 { //RIGHT
                resetMoves()
                moves[2] = true
            }
            else if amountDraggedX < 0 { //LEFT
                resetMoves()
                moves[3] = true
            }
        }
    }
  • 我声明了一个名为 move[] 的全局数组,它包含 4 个元素。它控制 Snake 的移动(如果第一个元素为真,则蛇向上移动)
var moves = [false, false, false, false]
  • resetMoves() 是我用来将数组 move[] 的所有元素设置为 false 的函数。然后我将我需要的项目更改为 true
4

1 回答 1

0

得到了问题的解决方案。继续文件“GameViewController.swift”。在 viewDidLoad() 函数中

        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))

        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        leftSwipe.direction = .left

        let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        upSwipe.direction = .up

        let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        downSwipe.direction = .down

        view.addGestureRecognizer(upSwipe)
        view.addGestureRecognizer(downSwipe)
        view.addGestureRecognizer(rightSwipe)
        view.addGestureRecognizer(leftSwipe)

现在,在这个函数之外写下这个:

@objc func handleSwipe(sender: UISwipeGestureRecognizer){
        if sender.state == .ended {
            switch sender.direction {
            case .up:
                //MOVES UP --> DO SOMETHING
            case .down:
                //MOVES DOWN --> DO SOMETHING
            case .right:
                //MOVES RIGHT --> DO SOMETHING
            case .left:
                //MOVES LEFT --> DO SOMETHING
            default:
                break
            }
        }
    }

这是您应该在“GameViewController.swift”文件中获得的代码

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))

        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        leftSwipe.direction = .left

        let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        upSwipe.direction = .up

        let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        downSwipe.direction = .down

        view.addGestureRecognizer(upSwipe)
        view.addGestureRecognizer(downSwipe)
        view.addGestureRecognizer(rightSwipe)
        view.addGestureRecognizer(leftSwipe)


        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            let scene = GameScene(size: CGSize(width:1536, height: 2048))
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill

                // Present the scene
                view.presentScene(scene)

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }
    }

    @objc func handleSwipe(sender: UISwipeGestureRecognizer){
        if sender.state == .ended {
            switch sender.direction {
            case .up:

            case .down:

            case .right:

            case .left:

            default:
                break
            }
        }
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

现在,如果你想与“GameScene.swift”文件通信,你只需要声明一个全局变量。

为此,您只需在库导入上方声明它。例如:

var movesController = [false, false, false, false]
import UIKit
import SpriteKit
import GameplayKit

然后我可以为每种情况分配一个功能:

           case .up:
                movesController[0]=true
            case .down:
                movesController[1]=true
            case .right:
                movesController[2]=true
            case .left:
                movesController[3]=true
            default:
                break

完成此操作后,即使在“GameScene.swift”文件中,您也可以访问数组 moveController。

如果 moveController[0] == true ----> 他正在向上移动

如果 moveController[1] == true ----> 他正在向下移动

如果 moveController[2] == true ----> 他正在向右移动

如果 moveController[3] == true ----> 他向左移动

每次可以声明重置函数时重置数组:

func resetMoves(){
        for i in 0...3 { //I can do 4 moves, so I have to overwrite 4 items (0,1,2,3)
            movesController[i] = false
        }
    }

然后在每种情况下调用它

            case .up:
                resetMoves()
                movesController[0]=true
            case .down:
                resetMoves()
                movesController[1]=true
            case .right:
                resetMoves()
                movesController[2]=true
            case .left:
                resetMoves()
                movesController[3]=true
            default:
                break
于 2020-01-02T14:18:38.520 回答