gpt4 book ai didi

ios - Sprite Kit with Swift "use of unresolved identifier ' size'”示例代码

转载 作者:行者123 更新时间:2023-11-28 12:51:03 25 4
gpt4 key购买 nike

我浏览了该站点上的建议答案并搜索了我自己的答案。我已经尝试将代码中的标识符附加到我认为可能合适的项目(尽管我在一定程度上是 Swift 和 Sprite Kit 的新手),但我无法弄清楚这一点。代码如下。

func addMonster() {

// Create Sprite

let monster = SKSpriteNode(imageNamed: "monster")

// Determine where to spawn the monster along the Y axis

let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)

// Position the mosnter slightly off-screen along the right edge and along a random position along the Y axis as calculated above

monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)

// Add the mosnter to the scene

addChild(monster)

// Determine speed of the monster

let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))

// Create the actions

let actionMove = SKAction.moveTo(CGPoint(x: -monster.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKaction.removeFromParent()
monster.runAction(SKAction.sequence([actioinMove, actionMoveDone]))

在代码行中

let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)

我收到“使用未解析的标识符‘size’”的错误

现在,我猜这是因为没有像 min 中那样的“标识符”:参数 (monster.size.height) 与 (size.height)。我不知道的是,该标识符需要是什么。只有当我使用 SKScene.size.height 时错误才会消失,但我不认为教程所有者试图根据场景减去怪物来设置最大轴。

正如我所说,我是新来的。我已经发布到网站论坛,供教程所有者查看。这可能是 Swift 2.2(我仍在学习)修订版中的更改,所以我可能只是缺少一种编写代码的新方法。

这是我关于堆栈溢出的第一个问题。提前,很高兴认识大家!

edit: screenshot of specific line of code and error location

最佳答案

使用类似于 size.height 的东西在 GameScene 里面类,或者你当前的场景,与self.size.height相同.这两个示例之间的唯一区别是隐式 self 与显式 self 的使用。但是效果是一样的。

如果实例没有名为 size 的属性,则可能会发生此错误。两者 SKScene , 和 SKSpriteNode有它。这个错误可能会发生,例如,如果怪物是 SKNode因为SKNode没有称为大小的属性。

所以以上不是问题,真正的问题(正如您已经发现的那样)与拼写错误和错误的代码格式有关。

关于ios - Sprite Kit with Swift "use of unresolved identifier ' size'”示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36517344/

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