2016-07-22 59 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吗? –

相关问题