首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使SKPhysicsBody单向

使SKPhysicsBody单向
EN

Stack Overflow用户
提问于 2020-02-01 14:55:04
回答 1查看 54关注 0票数 3

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

back

  • something
  • 创建一个fieldBitMask,到它迭代的路径上,并拒绝球返回
  • ,在touchesBegan/ touchesMoved方法的球上应用某种force/ impulses,以防止其进入update方法

H 116可以在堆栈流溢出中处理的force/ impulses,后者甚至在周末也在编写代码:)H 217F 218

支持代码片段以更好地理解,

代码语言:javascript
复制
//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)
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-06 08:55:43

这是touchesBegan/ touchesMovedupdate func中的一个小黑客组合,

首先,您需要了解发生了什么触摸,获取它的名称(在我的例子中,我创建了α为0的节点,但在它们之上移动时变得可见,即alpha 1)。在touchesBegan, touchesMoved中,如下所示

代码语言:javascript
复制
                        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
                            }
                        }
                    }

第二,通过使用点击识别器设置,在屏幕上使用BOOL touching跟踪用户交互,如下所示

代码语言:javascript
复制
func setupTapDetection() {
        let t = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
        view?.addGestureRecognizer(t)
    }

@objc func tapped(_ tap: UITapGestureRecognizer) {
        touching = true
    }

最后,在update中,如下所示:

代码语言:javascript
复制
        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)
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60018022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档