2016-12-14 67 views
1

因此,在我的應用程序中,我使用下面的代碼來反彈球,但問題是如果我按下右鍵,球會彈起,如果我按左鍵,則會離開。如果我在中間按下,它會像平常一樣彈起來。這是我使用的代碼:SwiftSpriteKit touchesBegan detect right/left

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() 
     } 
    } 

回答

1

嘗試添加此功能:

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) 

讓我知道這不適合你了!

+0

完美的作品。 -謝謝! –

0

嘗試改變

if xpo > 0 { 
    xpower = xpower * -1 
} 

if xpo > 0 { 
    xpower = xpower * 1.5 
} 
+0

是的,當你按下右鍵時,你把這個邏輯放在你的方法中。同樣,前者會在您按下左按鈕時調用的方法中進行。 –