2016-07-22 66 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]) –