【问题标题】:SpriteKit, Swift 2.0 - Making an object spawn on the game mapSpriteKit, Swift 2.0 - 在游戏地图上生成对象
【发布时间】:2016-01-14 05:33:34
【问题描述】:

我正在使用 SpriteKit 制作游戏,在这个游戏中我有生成的平台,我想制作它以便宝石或硬币等对象在生成时有机会在平台上生成

我需要它是随机的,并且能够选择它产生的东西(宝石或硬币)

有什么想法吗?

【问题讨论】:

    标签: ios swift object sprite-kit spawning


    【解决方案1】:

    要正确使用堆栈溢出,您应该始终发布一些代码。如果您只是希望他们为您完成工作,人们将不会提供帮助。

    最好的方法是对您的平台进行子类化,这是其背后的基本理念。

     enum PlatformObject: Int {
         case Gem = 0
         case Coin
     }
    
     class Platform: SKSpriteNode {
    
         init (size: CGSize, color: SKColor, objectType: PlatformObject, spawnObjectRandomly: Bool) { // create your own init for your needs
             super.init (texture: nil, color: color, size: size)
    
             // set up platform properties
    
             // Than spawn object
             if spawnObjectRandomly {
                   spawnRandomObject()
             } else if objectType == .Coin {
                   spawnCoin()
             } else if objectType == .Gem {
                   spawnGem()
             }
        }
    
        required init?(coder aDecoder: NSCoder) {
             fatalError("init(coder:) has not been implemented")
        }
    
        func spawnRandomObject() {
           let randomNumber = Int(arc4random() % 2)  // 0 and 1
           if randomNumber == 0 {
              // spawn gem
              spawnGem()
           } else {
              // spawn coin
              spawnCoin()
           }
       }
    
       func spawnCoin() {
           let coin = SKSpriteNode(...
       }
    
       func spawnGem() {
           let gem = SKSpriteNode(...
       }
    }
    

    比在你的场景中你生成的平台是这样的

      class GameScene: SKScene {
          let size = // set your size
          let color = // set color
    
          let platform1 = Platform(size: size, color: color, objectType: .Gem, spawnObjectRandomly: false) 
          ...
          // if false will spawn selected objectType ("Gem" in this example)
    
    
          let platform2 = Platform(size: size, color: color, objectType: .Gem, spawnObjectRandomly: true) 
         // if true will spawn random object regardless of objectType settings
          ...
     }
    

    希望对你有帮助

    【讨论】:

    • 感谢您的标记,我只是稍微调整了答案。你们相处得怎么样?
    • 我相处得很好,我只需要解决我已经解决的这个滚动视图问题,我应该没问题。你给了我很大的帮助,我非常感谢。
    • 那么你对scrollview问题有什么看法?
    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多