【发布时间】:2017-05-22 08:30:16
【问题描述】:
到目前为止,我的应用中间有一个大球,中间还有一个小球。我希望能够点击屏幕上的任何位置,小球会朝那个方向射击。我听人说创建向量,但我似乎无法让这些在 swift 3 中工作。我是初学者,很抱歉提出一个愚蠢的问题!
这是我的代码:
var mainBall = SKSpriteNode(imageNamed: "Ball")
override func didMove(to view: SKView) {
mainBall.size = CGSize(width: 300, height: 300)
mainBall.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
self.addChild(mainBall)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self)
print(position.x)
print(position.y)
}
for touch in (touches) {
touch.location(in: self)
let smallBall = SKSpriteNode(imageNamed: "Ball")
smallBall.position = mainBall.position
smallBall.size = CGSize(width: 100, height: 100)
smallBall.physicsBody = SKPhysicsBody(circleOfRadius: smallBall.size.width / 2)
smallBall.physicsBody?.affectedByGravity = false
self.addChild(smallBall)
}
}
【问题讨论】:
标签: swift vector direction projectile