Ios 尝试在y轴上移动/拖动SKSpriteNode

Ios 尝试在y轴上移动/拖动SKSpriteNode,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在尝试创建一个小游戏,其中有一个管道,我想沿着y轴移动管道,我想做一些手指滑动的动作,我尝试用触控开始,但它看起来并不平滑,所以我尝试使用uipangesture,但不熟悉sprite集成 有人能帮我做到这一点吗 class GameScene: SKScene,SKPhysicsContactDelegate { var pipeTextureUp:SKTexture! var pipeTextureDown:SKTexture! var pipeUp:SKSpriteNode! v

我正在尝试创建一个小游戏,其中有一个管道,我想沿着y轴移动管道,我想做一些手指滑动的动作,我尝试用触控开始,但它看起来并不平滑,所以我尝试使用uipangesture,但不熟悉sprite集成

有人能帮我做到这一点吗

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



}
类游戏场景:SKScene,skphysiccontactdelegate{
var pipeTextureUp:SKTexture!
var pipeTextureDown:SKTexture!
var Pippep:SKSpriteNode!
var pipeDown:SKSpriteNode!
var circleTouch:UITouch?
变量垂直管道间隙:双=60.0
状态:Bool!
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
backgroundColor=SKColor.whiteColor()
//设置重力
self.physicsWorld.gravity=CGVector(dx:0.0,dy:0.0)
self.physicsWorld.contactDelegate=self
//首先在随机位置创建管道
pipeTextureUp=SKTexture(图像名为:“PipeUp”)
pipeTextureUp.filteringMode=
pipeTextureDown=SKTexture(图像名为:“PipeDown”)
pipeTextureDown.filteringMode=.Nearest
pipeUp=SKSpriteNode(纹理:pipeTextureUp)
管道设置刻度(2.0)
pipeUp.name=“pipeUp”
pipeUp.position=CGPoint(x:size.width*0.9,y:0.0)
pipeDown=SKSpriteNode(纹理:pipeTextureDown)
管道下降。设置刻度(2.0)
pipeDown.name=“pipeDown”
pipeDown.position=CGPoint(x:Double(size.width)*0.9,y:Double(pipeDown.size.height)+垂直管道间隙)
//addChild(管道下降)
addChild(pipeUp)
}
func didBeginContact(联系人:SKPhysicsContact){
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
让触摸:UITouch=touch.first!
let positionInScene=触摸位置InNode(自身)
让touchedNode=self.nodeAtPoint(positionInScene)
如果let name=touchedNode.name
{
if(name==“pipeUp”| | name==“pipeDown”)
{
打印(“触摸开始”)
moveStatus=true
circleTouch=触摸
}
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:UITouch=touch.first!
let positionInScene=触摸位置InNode(自身)
让touchedNode=self.nodeAtPoint(positionInScene)
打印(位置InScene)
如果let name=touchedNode.name
{
if(name==“pipeUp”| | name==“pipeDown”)
{
//让fingerPoint=CGPoint(x:size.width*0.9,y:0.3)
让actionUpMove:SKAction=SKAction.moveToY(位置场景y,持续时间:1.0)
运行操作(actionUpMove)
pipeUp.position.y=positionInScene.y
}
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
让触摸:UITouch=touch.first!
let positionInScene=触摸位置InNode(自身)
让touchedNode=self.nodeAtPoint(positionInScene)
如果let name=touchedNode.name
{
if(name==“pipeUp”| | name==“pipeDown”)
{
//让fingerPoint=CGPoint(x:size.width*0.9,y:0.3)
circleTouch=零
}
}
}

}

如果您想拦截沿Y坐标的刷卡,可以将此代码放入
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")
}

更新:显然这是我必须做的

 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

    }

}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
让触摸:UITouch=touch.first!
let positionInScene=触摸位置InNode(自身)
让touchedNode=self.nodeAtPoint(positionInScene)
如果let name=touchedNode.name
{
if(name==“pipeUp”| | name==“pipeDown”)
{
handleTouch(触摸。第一!名称:name)
}
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:UITouch=touch.first!
let positionInScene=触摸位置InNode(自身)
让touchedNode=self.nodeAtPoint(positionInScene)
如果let name=touchedNode.name
{
if(name==“pipeUp”| | name==“pipeDown”)
{
handleTouch(触摸。第一!名称:name)
}
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
让触摸:UITouch=touch.first!
let positionInScene=触摸位置InNode(自身)
让touchedNode=self.nodeAtPoint(positionInScene)
如果let name=touchedNode.name
{
if(name==“pipeUp”| | name==“pipeDown”)
{
handleTouch(触摸。第一!名称:name)
}
}
}
func handleTouch(触摸:UITouch,名称:String){
让location=touch.locationInNode(self)//获取当前点
让node=self.nodeAtPoint(location)//根据触摸的位置获取当前节点
让previousLocation=touch.previousLocationInNode(self)//获取节点中的上一个位置
让diff=location.y-previousLocation.y;//获取不同的位置
设newPosition=CGPointMake(node.position.x,node.position.y+diff);
变量newPipeDownPosition:CGPoint;
如果(名称==“管道向下”)
{
newPipeDownPosition=CGPointMake(self.pipeUp.position.x,self.pipeUp.position.y+diff);
pipeDown.position.y=newPosition.y
pipeUp.position.y=newPipeDownPosition.y
}否则{
newPipeDownPosition=CGPointMake(self.pipeDown.position.x,self.pipeDown.position.y+diff);
pipeUp.position.y=newPosition.y
pipeDown.position.y=newPipeDownPosition.y
}
}

现在管道按照上下方向移动

但是我如何才能在swipedUp功能中移动我的pipeUp对象,取决于你的游戏,如果你使用物理,最好使用附加脉冲或修正速度来移动你的对象,否则如果你有一个静态对象,你可以使用moveTo和一个目标点。我的对象是静态的,但是moveTo并没有给我一个平滑的过渡,我已经试过了,想象它是一个管道,我可以水平移动y轴上的管道吗