除底部和顶部以外的所有边框

除底部和顶部以外的所有边框

我有一个主要的玩家节点和随机掉落的敌人,但它们的一部分在框架外,我想这样做,以使他们(和玩家)无法脱离框架,左右边界。我该如何实现?

谢谢您的时间和精力:)

这是我的敌人,不确定是否可以帮到您,但在这里是这样,因此我不会因为过于宽泛而被报道

 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")
}


这是我的敌人班

     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
    }

}

}

最佳答案

基本上,您有2个问题:


你的产卵点弄乱了
您没有界限


您可以使用几种设置边界的方法,一种是PhysicalBody,另一种是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()
  }
}

关于swift - SpriteKit-除底部和顶部以外的所有边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44866490/

10-13 04:03