[英]How can I move a sprite while simultaneously firing a projectile in SpriteKit?
我正在嘗試使用Swift 4和SpriteKit編寫一些邏輯,以允許用戶使用拖動動作來移動飛船,同時還允許他們射擊彈丸。 我可以用左手的拇指拖動飛船,但是一旦通過點擊開始用右手拇指射擊,飛船就會停止移動。
我已經在viewDidLoad()中啟用了“ enableMultiTouch”。 我感覺好像我已經很接近完成這項工作了,只是缺少了一塊允許用戶同時執行兩項操作。 提前致謝。
這是我到目前為止的內容:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if player.contains(touchLocation) {
playerIsTouched = true
} else {
// 2 - Set up initial location of projectile
let projectile = SKSpriteNode(imageNamed: "projectile")
projectile.position = player.position
projectile.physicsBody = SKPhysicsBody(circleOfRadius: projectile.size.width/2)
projectile.physicsBody?.isDynamic = true
projectile.physicsBody?.categoryBitMask = PhysicsCategory.projectile
projectile.physicsBody?.contactTestBitMask = PhysicsCategory.monster
projectile.physicsBody?.collisionBitMask = PhysicsCategory.none
projectile.physicsBody?.usesPreciseCollisionDetection = true
// 3 - Determine offset of location to projectile
let offset = touchLocation - projectile.position
// 4 - Bail out if you are shooting down or backwards
if offset.x < 0 {
return
} else {
run(SKAction.playSoundFileNamed("laser-shot-basic.wav", waitForCompletion: false))
shotsFired += 1
accuracyLabel.text = "\(trunc((shotsHit/shotsFired*100) * 10)/10)% Accuracy"
}
// 5 - OK to add now - you've double checked position
addChild(projectile)
// 6 - Get the direction of where to shoot
let direction = offset.normalized()
// 7 - Make it shoot far enough to be guaranteed off screen
let shootAmount = direction * 1000
// 8 - Add the shoot amount to the current position
let realDest = shootAmount + projectile.position
// 9 - Create the actions
let actionMove = SKAction.move(to: realDest, duration: 2.0)
let actionMoveDone = SKAction.removeFromParent()
projectile.run(SKAction.sequence([actionMove, actionMoveDone]))
}
if pauseButton.contains(touchLocation) {
run(gameLostSound, completion: {
let scene = LevelSelectScene(size: self.size)
self.view?.presentScene(scene)
})
}
// for touch: AnyObject in touches {
// let pointOfTouch = touch.location(in: self)
// let previousPointOfTouch = touch.previousLocation(in: self)
//
// let amountDragged = pointOfTouch.x - previousPointOfTouch.x
//
// player.position.x += amountDragged
// }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if (playerIsTouched == true) {
player.position = (touches.first?.location(in: self))!
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// 1 - Choose one of the touches to work with
guard let touch = touches.first else {
return
}
let touchLocation = touch.location(in: self)
if playerIsTouched {
playerIsTouched = false
}
}
當您點擊觸發時,將導致兩個事件:“開始”事件和“結束”事件。 但是你touchesEnded方法不區分哪些觸摸剛剛結束,並設置你的playerIsTouched參數設置為false,從而進一步移動停止。
有多種解決方法,具體取決於應用程序的更大和將來的結構,但是一種方法是:在touchesEnded中 ,使用觸摸的位置來確定是否是拖動觸摸已經結束,並且僅在以下情況下重置playerIsTouched:就是這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.