所以我是Swift的初学者,并且一直试图制作一个涉及两个角色的游戏。两个角色的主体由头部和身体SKSpriteNode组成,与常规SKNode绑定在一起。当我触摸屏幕的左侧时,我希望播放器向左移动,依此类推。我不确定我是否只是遗漏了一些东西,但我似乎无法使用applyImpulse方法让角色移动到任何地方。
var playerNode = SKNode()
var playerBody = SKSpriteNode()
var playerHead = SKSpriteNode()
var opponentNode = SKNode()
var opponentBody = SKSpriteNode()
var opponentHead = SKSpriteNode()
var ground = SKSpriteNode()
override public func didMove(to view: SKView)
{
playerNode = self.childNode(withName: "playerNode")!
playerBody = playerNode.childNode(withName: "playerBody") as! SKSpriteNode
playerHead = playerNode.childNode(withName: "playerHead") as! SKSpriteNode
opponentNode = self.childNode(withName: "opponentNode")!
opponentBody = opponentNode.childNode(withName: "opponentBody") as! SKSpriteNode
opponentHead = opponentNode.childNode(withName: "opponentHead") as! SKSpriteNode
}
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches
{
let location = touch.location(in: self)
if (location.x < 0)
{
playerNode.physicsBody?.applyImpulse(CGVector(dx: -10, dy: 0))
}
else if (location.x > 0)
{
playerNode.physicsBody?.applyImpulse(CGVector(dx: 10, dy: 0))
}
}
}
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
playerNode.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
}
任何帮助都将不胜感激。