Mr.G Mr.G - 4 months ago 23
iOS Question

Trying to move / drag SKSpriteNode in y axis

I'm trying to create a small game ,and in there there is a pipe , and i want to move that pipe along with the y axis , i want to do thins for finger swipe , i tried to do this with touches began but its doesn't looks smooth , so i tried to use uipangesture , but in not familiar with the sprite integration ,

can someone help me to achive this

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



}


}

Answer

UPDATE : apparently this is what i had to do ,

 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

    }

}

now the pipe moves according to top and down direction

Comments