0

我在 ContentView 页面上看不到 Text 对象的变化。但是,当我在带有打印的 .onReceive 中运行相同的代码时,我可以看到变化。这里有什么问题?

我想从不同的地方管理游戏的状态和从不同的地方运行游戏。我的逻辑错了吗?

枚举

enum GameSituation {
    case play
}

游戏配置

class GameConfig: ObservableObject {
    @Published var randomCellValue: Int = 0
    @Published var timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect()
    
    func startGame() {
        determineRandomCell()
    }
    
    func determineRandomCell() {
        randomCellValue = Int.random(in: 0...11)
    }

    func playSound(soundfile: String, ofType: String) {
        
        if let path = Bundle.main.path(forResource: soundfile, ofType: ofType){
            do{
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                audioPlayer.prepareToPlay()
                audioPlayer.play()
            } catch {
                print("Error")
            }
        }
    }
}

游戏情况

class GameSituations: ObservableObject {
    @Published var gameConfig = GameConfig()
    
    func gameSituation(gameSituation: GameSituation) {
        switch gameSituation {
        case .play:
            gameConfig.startGame()
        }
    }
}

内容视图

struct ContentView: View {
    @StateObject var gameSituation = GameSituations()
    var body: some View {
        Text("\(gameSituation.gameConfig.randomCellValue)")
            .padding()
            .onReceive(gameSituation.gameConfig.timer, perform: { _ in
                gameSituation.gameSituation(gameSituation: .play)
                print("random: \(gameSituation.gameConfig.randomCellValue)")
            })
    }
}
4

1 回答 1

0

您有多个问题我都解决了,您的第一个也是最大的问题是,您正在初始化ObservableObject的一个实例,但使用另一个实例!第二个是您忘记在视图中使用 GameConfig 的StateObject,正如您在代码中看到的那样,您确实使用了 GameSituations 的StateObject而不是GameConfig,如果您问为什么,因为它是Publisher

我最好的建议:尝试只使用一个ObservableObject!使用具有绑定的2 个不同的ObservableObject有一个你可以看到的问题!尝试将所有代码组织到一个ObservableObject中。


enum GameSituation {
    case play
}

class GameConfig: ObservableObject {
    
    static let shared: GameConfig = GameConfig()    // <<: Here

    @Published var randomCellValue: Int = 0
    @Published var timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect()
    
    func startGame() {
        determineRandomCell()
    }
    
    func determineRandomCell() {
        randomCellValue = Int.random(in: 0...11)
    }

    func playSound(soundfile: String, ofType: String) {
        
        print(soundfile)


    }
}

class GameSituations: ObservableObject {
    
    static let shared: GameSituations = GameSituations()  // <<: Here

    let gameConfig = GameConfig.shared        // <<: Here
     
    func gameSituation(gameSituation: GameSituation) {
        switch gameSituation {
        case .play:
            gameConfig.startGame()
        }
    }
}

struct ContentView: View {
    
    @StateObject var gameSituation = GameSituations.shared     // <<: Here
    @StateObject var gameConfig = GameConfig.shared            // <<: Here
    
    var body: some View {
        
        Text(gameConfig.randomCellValue.description)          // <<: Here
            .onReceive(gameConfig.timer, perform: { _ in      // <<: Here

                       gameSituation.gameSituation(gameSituation: .play) 
                        print("random: \(gameSituation.gameConfig.randomCellValue)")
        
                    })

    }
}
于 2021-03-19T19:25:36.010 回答