Ios 使SKPhysicsBody单向

Ios 使SKPhysicsBody单向,ios,swift,sprite-kit,skspritenode,skphysicsbody,Ios,Swift,Sprite Kit,Skspritenode,Skphysicsbody,我有一个SKSpriteNode作为一个球,它被赋予了所有SKPhysicsBody属性在各个方向上移动。我现在想要的是使它成为单向的(只朝着它以前没有移动过的方向移动,不要回到它曾经移动过的路径上)。目前我对这个问题有以下想法 创建一个字段位掩码,指向它所迭代的路径并排斥 球不回了 从触球开始/触球移动方法对球施加某种力/脉冲,以防止球反弹 可以在update方法中处理的内容 stackflowoverflow的救星,甚至在周末也在编码:) 支持代码片段以便更好地理解 //getting

我有一个
SKSpriteNode
作为一个球,它被赋予了所有
SKPhysicsBody
属性在各个方向上移动。我现在想要的是使它成为单向的(只朝着它以前没有移动过的方向移动,不要回到它曾经移动过的路径上)。目前我对这个问题有以下想法

  • 创建一个
    字段位掩码
    ,指向它所迭代的路径并排斥 球不回了
  • 触球开始/触球移动
    方法对球施加某种
    力/脉冲
    ,以防止球反弹
  • 可以在
    update
    方法中处理的内容
  • stackflowoverflow的救星,甚至在周末也在编码:)
支持代码片段以便更好地理解

//getting current touch position by using UIEvent methods
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {return}
        let location = touch.location(in: self)
        lastTouchPoint = location
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {return}
        let location = touch.location(in: self)
        lastTouchPoint = location
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        lastTouchPoint = nil
    }
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        lastTouchPoint = nil
    }

//ball created
    func createPlayer(){
        player = SKSpriteNode(imageNamed: "player")
        player.position = CGPoint(x: 220, y: 420)
        player.zPosition = 1

    //physics for ball
    player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
    player.physicsBody?.allowsRotation = false
    player.physicsBody?.linearDamping =  0.5


    player.physicsBody?.categoryBitMask = collisionTypes.player.rawValue
    player.physicsBody?.contactTestBitMask = collisionTypes.finish.rawValue
    player.physicsBody?.collisionBitMask = collisionTypes.wall.rawValue

    addChild(player)
}

//unwarp the optional property, calclulate the postion between player touch and current ball position
override func update(_ currentTime: TimeInterval) {
    guard isGameOver == false else { return }
    if let lastTouchPosition = lastTouchPoint {
        //this usually gives a large value (related to screen size of the device) so /100 to normalize it
        let diff = CGPoint(x: lastTouchPosition.x - player.position.x, y: lastTouchPosition.y - player.position.y)
        physicsWorld.gravity = CGVector(dx: diff.x/100, dy: diff.y/100)
    }
}
//使用UIEvent方法获取当前触摸位置
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
guard let touch=touch.first else{return}
让位置=触摸。位置(in:self)
lastTouchPoint=位置
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
guard let touch=touch.first else{return}
让位置=触摸。位置(in:self)
lastTouchPoint=位置
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
lastTouchPoint=nil
}
覆盖功能触摸已取消(touchs:Set,带有事件:UIEvent?){
lastTouchPoint=nil
}
//球创造
func createPlayer(){
player=SKSpriteNode(图像名为“player”)
player.position=CGPoint(x:220,y:420)
player.zPosition=1
//球的物理学
player.physicsBody=SKPhysicsBody(圆圈:player.size.width/2)
player.physicsBody?.allowsRotation=false
玩家.physicsBody?.linearDamping=0.5
player.physicsBody?.categoryBitMask=碰撞类型.player.rawValue
player.physicsBody?.contactTestBitMask=collisionTypes.finish.rawValue
player.physicsBody?.collisionBitMask=collisionTypes.wall.rawValue
addChild(玩家)
}
//取消翻转可选属性,计算球员触球和当前球位之间的位置
覆盖函数更新(uCurrentTime:TimeInterval){
guard isGameOver==false否则{return}
如果让lastTouchPosition=lastTouchPoint{
//这通常会给出一个较大的值(与设备的屏幕大小相关)so/100以使其正常化
设diff=CGPoint(x:lastTouchPosition.x-player.position.x,y:lastTouchPosition.y-player.position.y)
physicsWorld.gravity=CGVector(dx:diff.x/100,dy:diff.y/100)
}
}

这是一个结合了
触摸开始/触摸移动
更新
功能的小技巧

首先,您需要了解发生的触碰,获取其名称(在我的 案例一创建了alpha值为0的节点,但在 在它们上面移动,即阿尔法1)。在
触摸开始,触摸移动
如下

                        guard let touch = touches.first else {return}
                        let location = touch.location(in: self)
                        lastTouchPoint = location

                        let positionInScene = touch.location(in: self)
                        let touchedNode = self.atPoint(positionInScene)

                        if let name = touchedNode.name
                        {
                            if name == "vortex"
                            {
                                touching = false
                                self.view!.isUserInteractionEnabled = false
                                print("Touched on the interacted node")
                            }else{
                                self.view!.isUserInteractionEnabled = true
                                touching = true
                            }
                        }
                    }
func setupTapDetection() {
        let t = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
        view?.addGestureRecognizer(t)
    }

@objc func tapped(_ tap: UITapGestureRecognizer) {
        touching = true
    }
其次,通过使用点击识别器设置,在屏幕上使用
BOOL触摸来跟踪用户交互,如下所示

                        guard let touch = touches.first else {return}
                        let location = touch.location(in: self)
                        lastTouchPoint = location

                        let positionInScene = touch.location(in: self)
                        let touchedNode = self.atPoint(positionInScene)

                        if let name = touchedNode.name
                        {
                            if name == "vortex"
                            {
                                touching = false
                                self.view!.isUserInteractionEnabled = false
                                print("Touched on the interacted node")
                            }else{
                                self.view!.isUserInteractionEnabled = true
                                touching = true
                            }
                        }
                    }
func setupTapDetection() {
        let t = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
        view?.addGestureRecognizer(t)
    }

@objc func tapped(_ tap: UITapGestureRecognizer) {
        touching = true
    }
最后在
update
中进行如下检查:

        guard isGameOver == false else { return }
        self.view!.isUserInteractionEnabled = true

        if(touching ?? true){
            if let lastTouchPosition = lastTouchPoint {
                //this usually gives a large value (related to screen size of the device) so /100 to normalize it
                let diff = CGPoint(x: lastTouchPosition.x - player.position.x, y: lastTouchPosition.y - player.position.y)
                physicsWorld.gravity = CGVector(dx: diff.x/100, dy: diff.y/100)
            }
        }
    }

我将于明天提交此文件,感谢您的帮助;(