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

标签 ios swift sprite-kit

我浏览了该站点上的建议答案并搜索了我自己的答案。我已经尝试将代码中的标识符附加到我认为可能合适的项目(尽管我在一定程度上是 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/

相关文章:

swift - Spritekit - 移动物理世界问题

ios - 无法完成操作。 (Starscream.WSError 错误 1.) RBSManager

ios - NSMutableArray,检查哪个值最丰富?

ios - 在 UITabbarController 中为所有 View Controller 添加一个公共(public) View

ios - Metal 数据类型对应的 Swift 数据类型?

swift - 即使调用 Swift SpriteKit 中的 removeFromParent() 按钮仍然有效

ios - 在没有 Storyboard 的情况下在 UIView 中添加 UICollectionView

swift - 解决 iPhone 处理器过载时的队列问题

ios - 如何向 PhoneNumberTextField 中的文本和占位符添加插图

使用 Swift 的 SpriteKit 中的 iOS 应用程序在 6 分钟后滞后,知道为什么吗?