简体   繁体   English

如何在SpriteKit中同时发射子弹的同时移动精灵?

[英]How can I move a sprite while simultaneously firing a projectile in SpriteKit?

I am trying to write some logic using Swift 4 and SpriteKit to allow the user to move the spaceship around using a drag action while also allowing them to shoot a projectile. 我正在尝试使用Swift 4和SpriteKit编写一些逻辑,以允许用户使用拖动动作来移动飞船,同时还允许他们射击弹丸。 I can drag the spaceship around with my left thumb, but once I begin shooting with my right thumb by tapping, the spaceship stops moving. 我可以用左手的拇指拖动飞船,但是一旦通过点击开始用右手拇指射击,飞船就会停止移动。

I have enabled 'enableMultiTouch' in the viewDidLoad(). 我已经在viewDidLoad()中启用了“ enableMultiTouch”。 I feel as though I am very close to having this working, I am just missing a piece to allow a user to do both at the same time. 我感觉好像我已经很接近完成这项工作了,只是缺少了一块允许用户同时执行两项操作。 Thanks in advance. 提前致谢。

Here is what I have so far: 这是我到目前为止的内容:

  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
    }

  }

When you tap-to-fire that will cause two events: a 'begins' and an 'ends' event. 当您点击触发时,将导致两个事件:“开始”事件和“结束”事件。 However your touchesEnded method does not distinguish which touch has just ended, and sets your playerIsTouched parameter to false, thus further movement ceases. 但是你touchesEnded方法不区分哪些触摸刚刚结束,并设置你的playerIsTouched参数设置为false,从而进一步移动停止。

There are multiple ways of fixing this, depending on the larger and future structure of the app, but one way would be: in touchesEnded , use the location of the touch to determine whether it is the drag touch that has ended and only reset playerIsTouched if that is the case. 有多种解决方法,具体取决于应用程序的更大和将来的结构,但是一种方法是:在touchesEnded中 ,使用触摸的位置来确定是否是拖动触摸已经结束,并且仅在以下情况下重置playerIsTouched:就是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM