2016-07-22 44 views
2

我正在嘗試創建一個小遊戲,並且在那裏有一個管道,我想將該管道與y軸一起移動,我想爲手指做薄輕掃,我試着用觸摸做到這一點開始,但它並沒有外表光滑,所以我試圖用uipangesture,但在不熟悉的精靈融合,嘗試在y軸上移動/拖動SKSpriteNode

有人可以幫助我實現這一目標

class GameScene: SKScene,SKPhysicsContactDelegate { 


var pipeTextureUp:SKTexture! 
var pipeTextureDown:SKTexture! 

var pipeUp:SKSpriteNode! 
var pipeDown:SKSpriteNode! 

var circleTouch: UITouch? 

var verticalPipeGap:Double = 60.0 


var moveStatus: Bool! 

override func didMoveToView(view: SKView) { 
    /* Setup your scene here */ 

    backgroundColor = SKColor.whiteColor() 

    //set the gravity 
    self.physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0) 
    self.physicsWorld.contactDelegate = self 

    //first create the pipe in a random location 
    pipeTextureUp = SKTexture(imageNamed: "PipeUp") 
    pipeTextureUp.filteringMode = .Nearest 

    pipeTextureDown = SKTexture(imageNamed: "PipeDown") 
    pipeTextureDown.filteringMode = .Nearest 

    pipeUp = SKSpriteNode(texture: pipeTextureUp) 
    pipeUp.setScale(2.0) 
    pipeUp.name = "pipeUp" 
    pipeUp.position = CGPoint(x: size.width * 0.9, y: 0.0) 

    pipeDown = SKSpriteNode(texture: pipeTextureDown) 
    pipeDown.setScale(2.0) 
    pipeDown.name = "pipeDown" 
    pipeDown.position = CGPoint(x: Double(size.width) * 0.9 , y: Double(pipeDown.size.height) + verticalPipeGap) 

    //addChild(pipeDown) 
    addChild(pipeUp) 


} 




func didBeginContact(contact: SKPhysicsContact) { 


} 


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


    let touch:UITouch = touches.first! 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 

    if let name = touchedNode.name 
    { 
     if (name == "pipeUp" || name == "pipeDown") 
     { 


      print("touches started") 
      moveStatus = true 
      circleTouch = touch 
     } 
    } 

} 

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

    let touch:UITouch = touches.first! 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 



    print(positionInScene) 

    if let name = touchedNode.name 
    { 
     if (name == "pipeUp" || name == "pipeDown") 
     { 
      //let fingerPoint = CGPoint(x: size.width * 0.9, y: 0.3) 

       let actionUpMove:SKAction = SKAction.moveToY(positionInScene.y, duration: 1.0) 
       pipeUp.runAction(actionUpMove) 
       pipeUp.position.y = positionInScene.y 

     } 
    } 

} 


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

    let touch:UITouch = touches.first! 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 

    if let name = touchedNode.name 
    { 
     if (name == "pipeUp" || name == "pipeDown") 
     { 
      //let fingerPoint = CGPoint(x: size.width * 0.9, y: 0.3) 



      circleTouch = nil 
     } 
    } 



} 

}

回答

0

UPDATE:顯然這是我必須做的,

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch:UITouch = touches.first! 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 

    if let name = touchedNode.name 
    { 
     if (name == "pipeUp" || name == "pipeDown") 
     { 
      handleTouch(touches.first!, name: name) 
     } 
    } 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch:UITouch = touches.first! 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 

    if let name = touchedNode.name 
    { 
     if (name == "pipeUp" || name == "pipeDown") 
     { 
      handleTouch(touches.first!, name: name) 
     } 
    } 

} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch:UITouch = touches.first! 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 

    if let name = touchedNode.name 
    { 
     if (name == "pipeUp" || name == "pipeDown" ) 
     { 
      handleTouch(touches.first!, name: name) 
     } 
    } 

} 

func handleTouch(touch: UITouch , name : String) { 

    let location = touch.locationInNode(self) // get the current point 
    let node = self.nodeAtPoint(location) //get the current node based on the touched location 
    let previousLocation = touch.previousLocationInNode(self) //get the previous location in node 

    let diff = location.y - previousLocation.y; //get the different of location 
    let newPosition = CGPointMake(node.position.x, node.position.y + diff); 

    var newPipeDownPosition :CGPoint; 

    if (name == "pipeDown" ) 
    { 
     newPipeDownPosition = CGPointMake(self.pipeUp.position.x, self.pipeUp.position.y + diff); 

     pipeDown.position.y = newPosition.y 
     pipeUp.position.y = newPipeDownPosition.y 

    }else{ 
     newPipeDownPosition = CGPointMake(self.pipeDown.position.x, self.pipeDown.position.y + diff); 

     pipeUp.position.y = newPosition.y 
     pipeDown.position.y = newPipeDownPosition.y 

    } 

} 

現在管道移動根據上下方向

1

如果你想攔截你沿Y座標滑動, d將在didMoveToView這個代碼:

 let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.swipedUp(_:))) 
     swipeUp.direction = .Up 
     swipeUp.cancelsTouchesInView = true 
     swipeUp.delaysTouchesBegan = true 
     view.addGestureRecognizer(swipeUp) 


     let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.swipedDown(_:))) 
     swipeDown.direction = .Down 
     swipeDown.cancelsTouchesInView = true 
     swipeDown.delaysTouchesBegan = true 
     view.addGestureRecognizer(swipeDown) 

然後你的方法是:

func swipedUp(sender:UISwipeGestureRecognizer){ 
     print("swiped up") 
} 
func swipedDown(sender:UISwipeGestureRecognizer){ 
     print("swiped down") 
} 
+0

但我該如何移動我的pipeUp對象在swipedUp函數中,?? –

+0

依靠你的遊戲,如果你使用物理,最好使用addImpulse來移動你的對象,或者更正速度,否則如果你有一個靜態對象,你可以使用moveTo和目標點。 –

+0

我的對象操作系統是靜態的,但moveTo不會給我一個平滑的過渡,我已經嘗試過它,想象它是一個管道,並且我將管道沿y軸水平移動,我可以輕鬆使用addImpulse嗎? –