ios - 如何在 SpriteKit 中同时发射弹丸的同时移动 Sprite ?

标签 ios swift sprite-kit

我正在尝试使用 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,从而停止进一步的移动。

有多种方法可以解决这个问题,具体取决于应用程序的更大和 future 的结构,但一种方法是:在 touchesEnded 中,使用触摸的位置来确定它是否是拖动已结束的触摸,如果是这种情况,则仅重置 playerIsTouched

关于ios - 如何在 SpriteKit 中同时发射弹丸的同时移动 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52342243/

相关文章:

ios - 如果找不到结果,对象过滤器会崩溃

ios - UITextField:如何对 "Done"键使用react并验证输入

html - 使用 webview 的 uiview 滚动

ios - 在 iOS 上使用智能卡进行 TLS 身份验证

swift - 方便的可失败初始化器分配 self

ios - 模拟寿司火车 - Swift SpriteKit

ios - 如何使用该节点的自定义类检测在单独场景中创建的 skspritenode 上的触摸

ios - 使用通用图像 Assets (SpriteKit 游戏)时背景不适合屏幕

swift - 哪里可以看到swiftUI的源代码?

swift - 闭包不能隐式捕获 self 参数。 swift