SpriteKit - 除底部和顶部外的所有边框

时间:2017-07-02 00:27:41

标签: swift sprite-kit 2d-games

我有一个主要的玩家节点和随机下来的敌人,但是它们的一部分在框架之外,我想做它所以他们(和玩家)不能离开框架,左右边界。我怎样才能做到这一点?

感谢您的时间和精力:)

这是我的敌人,不知道这有什么用,但在这里我是不会因为太宽而被报道

 func launchEnemy() {

    let randomX = arc4random_uniform( UInt32(screenWidth))

    EnemyMissile.position = CGPoint(  x: CGFloat(randomX)  - (screenWidth / 2), y: screenHeight + 50)


    let action = SKAction.move(by: CGVector(dx: 0, dy: -400 + speedScore), duration: 5.0)


   EnemyMissile.run(SKAction.repeatForever(action))        

     increaseSpeed()


    self.run(action, withKey:"LaunchEnemyAction")
}  

这是 EnemyClass

     import Foundation
     import SpriteKit

class EnemyClass: SKNode {

var EnemyNode:SKSpriteNode = SKSpriteNode()

var hitsToKill:Int = 2
var hitCount:Int = 0
var damagePoints: Int = 2

//var missileAnimation:SKAction?

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) had not been implemented")

}

override init () {

    super.init()

}



    func createEnemy ( _ theImage:String) {


EnemyNode = SKSpriteNode(imageNamed: theImage)
self.addChild(EnemyNode)

let body:SKPhysicsBody = SKPhysicsBody(circleOfRadius: EnemyNode.size.width / 2.25, center:CGPoint(x: 0,y: 0))
body.isDynamic = true
body.affectedByGravity = false
body.allowsRotation = false


  body.categoryBitMask = BodyType.enemy.rawValue
  body.contactTestBitMask =  BodyType.bullet.rawValue |              BodyType.player.rawValue


self.physicsBody = body

self.name = "EnemyClass"

}

func destroy() {
   self.removeFromParent()
   self.name = "removeNode"
}
func hit() -> Bool {

    hitCount += 1

    if ( hitCount == hitsToKill) {

        destroy()
        return true

    } else {
        damagePoints = 4

        EnemyNode.removeAction(forKey: "animation")
        return false
    }

}

}

1 个答案:

答案 0 :(得分:0)

基本上你有2个问题:

  1. 你的产卵点搞砸了
  2. 你没有边界
  3. 您可以使用几个选项来设置边界,一个是physicsBody,另一个是keepInBounds()方法等。下面我应该简单的保持边界方法:

    class GameScene: SKScene {
    
      func spawnEnemy() {
        let offset = CGFloat(5) // pixels, so enemy will have some spacing between borders.
    
        let enemy = SKSpriteNode(color: .blue, size: CGSize(width: 50, height: 100))
        enemy.name = "enemy"
        addChild(enemy)
    
        var randomX = CGFloat(arc4random_uniform(UInt32(self.size.width))) // This is not a perfect rando algo.
    
        // Because our random x was from 0 through screenwidth, but .position works
        // on -halfWidth through +halfWidth
        randomX -= (self.frame.size.width / 2)
    
        // Because spawning on the border will mean that the enemy is HALF out of view:
        /*
    
         right border:       |
         enemy @ frame.maxX: x
         - offset:          x|
         - size.w/2        x |
         */
        if randomX > self.frame.maxX - offset - (enemy.size.width / 2) {
           randomX = self.frame.maxX - offset - (enemy.size.width / 2)
        }
        else if randomX < self.frame.minX + offset + (enemy.size.width / 2) {
                randomX = self.frame.minX + offset + (enemy.size.width / 2)
        }
    
        enemy.position.x = randomX
      }
    
      func keepEnemyInBounds() {
        // A better way to do this would be to have a dictionary of enemies:
        for node in self.children {
          guard node.name == "enemy" else { continue }
          let enemy = node as! SKSpriteNode
    
          if enemy.position.x > frame.maxX - (enemy.size.width / 2) {
            enemy.position.x = frame.maxX - (enemy.size.width / 2)
          }
          else if enemy.position.x < frame.minX + (enemy.size.width / 2) {
            enemy.position.x = frame.minX + (enemy.size.width / 2)
          }
        }
      }
    
    
      override func update(_ currentTime: TimeInterval) {
        spawnEnemy()
      }
      override func didFinishUpdate() {
        keepEnemyInBounds()
      }
    }