gpt4 book ai didi

swift/SpriteKit : Attempting to reach a function/method in a class from another class not working

转载 作者:行者123 更新时间:2023-11-30 11:46:02 25 4
gpt4 key购买 nike

我的 ScoreSystem 类中有一个名为 addScore 的函数。该函数为游戏添加 1 分,将 SKLabelNode 更新为当前分数,然后每 25 分调用函数 startNewLevel。

func addScore(scene: SKScene) {
gameScore += 1
scoreLabel.text = "\(gameScore)"

if CGFloat(gameScore).truncatingRemainder(dividingBy: 25) == 0 {
NotificationCenter.default.post(name: Notification.Name.init("start_new_level"), object: nil)
GameScreen().displayLevel(scene: scene)
}
}

每次发射的鱼雷击中敌人时都会调用该函数。我现在想添加一个必须避免 meteor (SKSpriteNode)的新关卡。我有几个连续的 SKActions 来完成这个任务。本质上,SKSpriteNode 从屏幕顶部移动,到达屏幕下方并被删除。如果 meteor 到达屏幕底部则表示玩家已避开它。

我尝试调用函数 addScore 但它没有更新。

这是函数:

let scoreSystem = ScoreSystem()

func spawnMeteor() {

let randomXStart = CGFloat.random(min: gameArea.minX, max: gameArea.maxX)

let startPoint = CGPoint(x: randomXStart, y: scene.size.height * 1.2)
let endPoint = CGPoint(x: randomXStart, y: -scene.size.height * 0.2)


let meteor = SKSpriteNode(imageNamed: "meteor")
meteor.name = "Meteor"
meteor.zPosition = 2
meteor.position = startPoint

let moveMeteor = SKAction.move(to: endPoint, duration: 3)
let deleteEnemy = SKAction.removeFromParent()

let score = SKAction.run(addToScore)

let meteorSequence = SKAction.sequence([
moveMeteor,
score,
deleteEnemy])

scene.addChild(meteor)
meteor.run(meteorSequence)

}

我尝试过像这样的函数 addToScore :

func addToScore() {
scoreSystem.addScore(scene: scene!)
}

也尝试过这个

func addToScore() {
NotificationCenter.default.post(name: Notification.Name.init("add_to_score"), object: nil)
}

当尝试第二种选择时,我将以下内容添加到 GameScene

override func sceneDidLoad() {
super.sceneDidLoad()
NotificationCenter.default.addObserver(forName: Notification.Name.init("add_to_score"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
self?.scoreSystem.addScore(scene: self!)
}

}

我从spawnMeteor()函数中删除了几行,以免不必要的代码行堵塞空间。我还没有弄清楚如何使用 SKAction.run() 调用该函数。有人能指出我正确的方向吗?

最佳答案

您向函数传递了大量信息,可能太多了。

我建议您实现协议(protocol)或通知来处理您的信息,我个人更喜欢协议(protocol),因此我的示例将是协议(protocol)。

我对您的代码做了一些假设,因为您的问题中并未呈现所有内容......

protocol ScoreSystemDelegate: class {
func displayLevel()
}

class ScoreSystem: SKNode {

weak var scoreSystemDelegate: ScoreSystemDelegate!

//your init func and any other funcs in this class (unknown)

func addScore(scene: SKScene) {
gameScore += 1
scoreLabel.text = "\(gameScore)"

if CGFloat(gameScore).truncatingRemainder(dividingBy: 25) == 0 {
NotificationCenter.default.post(name: Notification.Name.init("start_new_level"), object: nil)
//GameScreen().displayLevel(scene: scene)
self.scoreSystemDelegate.displayLevel()
}
}
}

在创建得分系统的类中(假设 GameScene)...

class GameScene: SKScene, ScoreSystemDelegate {

let scoreSystem = ScoreSystem()
//assign GameScene as the delegate of ScoreSystem that way the 2 can communicate
scoreSystem.scoreSystemDelegate = self

func displayLevel() {
//do whatever you do when you display the level
}
}

现在您的生成 meteor 函数应该按照您编码的方式工作,因为 addScore 不再采用场景属性,这种方法的好处是您可以使任何对象成为 ScoreSystem 的委托(delegate),它不必是场景。

关于 swift/SpriteKit : Attempting to reach a function/method in a class from another class not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48826762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com