2016-07-22 69 views
3

我有一些紋理...SpriteKit SKSpriteNode移動

placeForJump = SKSpriteNode(imageNamed: "placeForJump") 
placeForJump.position = CGPoint(x: 512.346, y: 98.88) 
placeForJump.size = CGSize(width: 166.407, height: 197.762) 
placeForJump.zPosition = 2 
self.addChild(placeForJump) 

和性格......

player = SKSPriteNode(imageNamed: "character") 
player.position... 
player.size... 
player.zPozition = 3 
self.addChild(player) 

當我觸摸的質感,我可以跳,我想我的性格跳就這地方,但我不知道該怎麼做。

當我用placeForJump觸摸紋理 - 我的角色跳上它。

請幫忙。

回答

2

您可以通過檢測觸摸位置跳轉來完成此操作,然後當用戶觸摸它時,您可以運行一個操作來移動該字符。

要檢測用戶觸摸跳,你可以添加此代碼到的touchesBegan或touchesEnded功能的地方:

for touch in touches { 
     let location = touch.locationInNode(self) 
     if placeForJump.containsPoint(location) { 
      print("It was touched") 
     } 
} 

然後運行到地方的動作,你將要使用像這樣的SKAction :

player.runAction(SKAction.moveTo(placeForJump.position, duration: speed) 

這將可能的方式玩家節點移動到placeForJump在shortes所以如果你想讓它飛得更高,然後到地方,你可以做這樣的:

let highPoint = CGPoint(x: player.position.x + 50, y: player.position.y + 100) 

let moveUp = SKAction.moveTo(highPoint, duration: speed) 
let moveDown = SKAction.moveTo(placeForJump.position, duration: speed) 
player.runAction(SKAction.sequence([moveUp, moveDown]) 

希望能幫到您

+0

您好!你的回答非常有幫助。非常感謝你。 (x:player.position.x + 50,y:player.position.y + 100) let moveUp = SKAction.moveTo(highPoint,duration:speed ) let moveDown = SKAction.moveTo(placeForJump.position,duration:speed) player.runAction(SKAction.sequence([moveUp,moveDown]) –