每10秒添加一个跟踪您位置的敌人

时间:2017-05-22 23:23:44

标签: ios swift sprite-kit

下面我将介绍我正在制作的简单游戏的部分代码。在游戏中,你的手指在屏幕周围,手指下方有一个球。然后每10秒钟加一个球跟随你的球。我有一个SKAaction,它每10秒调用我的添加敌人功能,它会产生敌人。问题是我无法在每一帧都更新敌人的功能,因为SKAction不会让我在每帧更新时调用它,所以我不知道该怎么办才能每10秒钟添加一次球并让球跟踪你的位置。因为目前它只跟踪球被添加时的初始位置。感谢任何帮助,谢谢。

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var me = SKSpriteNode()

    override func didMove(to view: SKView) {

        me = self.childNode(withName: "me") as! SKSpriteNode

        let border = SKPhysicsBody (edgeLoopFrom: self.frame)
        border.friction = 0
        self.physicsBody = border

        run(SKAction.repeatForever(SKAction.sequence([SKAction.run(createEnemy), SKAction.wait(forDuration: 10.0)])))
    }

    func createEnemy () {

        let enemy = SKSpriteNode(imageNamed: "ball 1")
        enemy.name = "enemy"
        enemy.position = CGPoint(x:667, y: -200)

        enemy.run(SKAction.moveTo(x: me.position.x, duration: 2))
        enemy.run(SKAction.moveTo(y: me.position.y, duration: 2))

        enemy.zPosition = +1
        addChild(enemy)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches{

            let location = touch.location(in: self)
            me.run(SKAction.moveTo(x: location.x, duration: 0))
            me.run(SKAction.moveTo(y: location.y, duration: 0))
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches{

            let location = touch.location(in: self)
            me.run(SKAction.moveTo(x: location.x, duration: 0))
            me.run(SKAction.moveTo(y: location.y, duration: 0))
        }
    }

    override func update(_ currentTime: TimeInterval) {


    }
}

1 个答案:

答案 0 :(得分:0)

  1. 通过.name属性在字典中保留所有当前活着的敌人的列表。这本词典中的值是一个元组。这个元组跟踪敌人的实例及其目标实例。
  2. 我是通过createEnemymakeEnemyName

    addEnemyToDict中执行此操作的
    1. update期间为每个敌人提供一个新的行动来跟随其目标。这是通过迭代字典,然后使用每个键的值moveFollowerToTarget来完成的。此信息存储在我们创建的元组类型中。
    2. import SpriteKit
      
      class GameScene22: SKScene {
        // MARK: - My code:
      
        // Tuple to keep track of enemy objects:
        typealias FollowerAndTarget = (follower: SKSpriteNode, target: SKSpriteNode)
      
        // [followerName: (followerSprite, targetSprite):
        var spriteDictionary: [String: FollowerAndTarget] = [:]
      
        // Give each enemy a unique name for the dictionary:
        var enemyCounter = 0
      
        // Assign to each enemy a unique name for the dictionary:
        private func makeEnemyName() -> String {
          enemyCounter += 1
          return "enemy\(enemyCounter)"
        }
      
        private func addEnemyToDict(enemy: SKSpriteNode, target: SKSpriteNode) {
          if let name = enemy.name { spriteDictionary[name] = (enemy, target) }
          else { print("enemy not found") } // error!
        }
      
        private func removeEnemyFromDict(enemy: SKSpriteNode) {
          if let name = enemy.name { spriteDictionary[name] = nil }
          else { print("enemy not removed from dictionary!") } // error!
        }
      
        // Note, you did not set the enemy to have a constant speed, so the enemy will move at random speeds
        // based on how far they are away from the target.
        private func moveFollowerToTarget(_ sprites: FollowerAndTarget) {
          let action = SKAction.move(to: sprites.target.position, duration: 2)
          sprites.follower.run(action)
        }
      
        private func allEnemiesMoveToTarget() {
          for sprites in spriteDictionary.values {
            moveFollowerToTarget(sprites)
          }
        }
      
        // Use this carefully if you are using phsyics later on:
        func killEnemy(_ enemy: SKSpriteNode) {
      
          // Remove this enemy from the update loop:
          removeEnemyFromDict(enemy: enemy)
          enemy.removeAllActions()
          enemy.removeFromParent()
          enemy.physicsBody = nil
        }
      
        // MARK: - Your code (mostly):
      
        var me = SKSpriteNode()
      
        override func didMove(to view: SKView) {
          me = SKSpriteNode(color: .blue, size: CGSize(width: 50, height: 50))
          me.name = "me"
          addChild(me)
      
          self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
          let border = SKPhysicsBody (edgeLoopFrom: self.frame)
          border.friction = 0
          self.physicsBody = border
      
          run(SKAction.repeatForever(SKAction.sequence([SKAction.run(createEnemy), SKAction.wait(forDuration: 10.0)])))
        }
      
        func createEnemy () {
      
          let enemy = SKSpriteNode(color: .purple, size: CGSize(width: 30, height: 30))
          enemy.name = makeEnemyName()
          addEnemyToDict(enemy: enemy, target: me)
      
          moveFollowerToTarget((follower: enemy, target: me))
          enemy.zPosition = +1
          addChild(enemy)
        }
      
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      
          for touch in touches{
      
            let location = touch.location(in: self)
            me.run(SKAction.moveTo(x: location.x, duration: 0))
            me.run(SKAction.moveTo(y: location.y, duration: 0))
          }
        }
      
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
      
          for touch in touches{
      
            let location = touch.location(in: self)
            me.run(SKAction.moveTo(x: location.x, duration: 0))
            me.run(SKAction.moveTo(y: location.y, duration: 0))
            allEnemiesMoveToTarget()
          }
        }
      
        override func update(_ currentTime: TimeInterval) {
          // Will iterate through dictonary and then call moveFollowerToTarget()
          // thus giving each enemy a new movement action to follow.
          allEnemiesMoveToTarget()
        }
      }