因此,在我的应用程序中,我使用下面的代码弹回球,但是问题是,如果我按向右,则球向左反弹,而当我按向左时,球会向左反弹。如果我按中间的按钮,则弹跳会像往常一样直线上升。这是我正在使用的代码:

var PlayingFlag:Bool = false
let jumpAmount:Double = 310.0
var Ballx1:CGFloat = 0.0
var Ballx2:CGFloat = 0.0
var Ballx3:CGFloat = 0.0
var Ballx4:CGFloat = 0.0
var BallMoveAmount = (10.0, 100.0, 150.0, 200.0)

func CreateBall(){

        let ballTexture = SKTexture(imageNamed: "ball")
        ballTexture.filteringMode = .Nearest
        self.ball = SKSpriteNode(texture: ballTexture)
        self.ball!.physicsBody = SKPhysicsBody(texture: ballTexture, size: ball!.size)
        self.ball!.physicsBody?.dynamic = true
        self.ball!.physicsBody?.allowsRotation = true
        self.ball!.physicsBody?.restitution = 0.6
        self.ball!.physicsBody?.mass = 0.430 // m = 430g
        self.ball!.position = CGPoint(x: self.frame.size.width / 2 , y: self.frame.size.height)
        self.ball!.physicsBody?.collisionBitMask = 0x1 << 1
        self.ball!.zPosition = 10
        self.addChild(self.ball!)

        let Ballx0 = ball!.size.width
        self.Ballx1 = Ballx0 * 0.2
        self.Ballx2 = Ballx0 * 0.4
        self.Ballx3 = Ballx0 * 0.6
        self.Ballx4 = Ballx0 * 0.8
    }

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let ballHeight = self.ball!.size.height
            for touch : UITouch in touches{

                let tpoint = touch.locationInNode(self)
                if tpoint.y < (self.ball!.position.y + (ballHeight / 2.0)) {
                    if tpoint.y > (self.ball!.position.y - (ballHeight / 2.0))
                    {
                        var xpower:Double = 0.0
                        let xpo = abs(tpoint.x - self.ball!.position.x)
                        if(xpo < self.Ballx1){
                            xpower = BallMoveAmount.0
                        }else if (xpo < self.Ballx2) {
                            xpower = BallMoveAmount.1
                        }else if (xpo < self.Ballx3) {
                            xpower = BallMoveAmount.2
                        }else if (xpo < self.Ballx4) {
                            xpower = BallMoveAmount.3
                        }else{
                            return
                        }

                        if xpo > 0 {
                            xpower = xpower * -1
                        }
                        self.ball!.physicsBody?.velocity = CGVector.zero
                        self.ball!.physicsBody?.applyImpulse(CGVector(dx: xpower, dy: self.jumpAmount))
                        self.ball!.runAction(se_ball)

                        if !self.PlayingFlag { // initial touch
                            self.gonode?.removeFromParent()
                            self.PlayingFlag = true
                        }else{
                            //something else
                        }
                    }
                }
            }
        }

func didBeginContact(contact: SKPhysicsContact) {

        if !self.PlayingFlag { return }
        if contact.bodyA.node == self.floorSprite || contact.bodyB.node == self.floorSprite {

            self.PlayingFlag = false

            // Game over...

            self.ball?.removeFromParent()
        }
    }

最佳答案

尝试添加此功能:

func vectorFrom(point pointA: CGPoint, to pointB: CGPoint) -> CGVector {
        let vector = CGVector(dx: pointB.x - pointA.x, dy: 310.0)
        return vector
    }

然后更改此行:
self.ball!.physicsBody?.applyImpulse(CGVector(dx: xpower, dy: self.jumpAmount))

对此:
let dirVector = vectorFrom(point: tpoint, to: ball!.position)
ball!.physicsBody?.applyImpulse(dirVector)

让我知道这是否对您不利!

关于ios - Swift SpriteKit touchs开始检测向右/向左,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41151583/

10-11 19:49