gpt4 book ai didi

xcode - 预期声明 Swift(Sprite Kit)

转载 作者:行者123 更新时间:2023-11-30 10:08:38 24 4
gpt4 key购买 nike

我正在使用 Xcode 7 和 swift,当我输入时

class Block {
var Block = SKShapeNode(circleOfRadius: 15)
Block.fillColor = SKColor.redColor() //Error Here
Block.physicsBody = SKPhysicsBody(circleOfRadius: 15)
Block.physicsBody?.affectedByGravity = true
Block.physicsBody?.restitution = 0
Block.physicsbody?.LinearDamping = 0
self.addChild(Block)

它给了我一个错误,显示“预期声明”(在带有注释的行上),我不知道为什么

最佳答案

有一些错误:

  • 您有一个类,并且正在尝试在方法或初始值设定项范围之外更改类属性。
  • Block.physicalsbody?.LinearDamping = 0应该是Block.physicalsBody?.linearDamping = 0;区分大小写。
  • 您将 SKShapeNode 实例命名为 Block,与您的类的名称相同。根据命名约定,类(类型)名称以大写字母开头,而类属性在名称中使用小写字母。

解决了这三个问题后,我们就可以继续查看您的场景了。

在 Leo Dabus 的帮助下(谢谢!),我们应该有足够的能力为您设置一个 SKScene 的最小工作示例(使用您的代码):

class GameScene: SKScene {

override func didMoveToView(view: SKView) {

let block = SKShapeNode(circleOfRadius: 15)

// you will also need to set your node initial position
// if you would like your red circle to fall from the middle of the top of your scene you need to use the scene frame midX and maxY (not the view frame). the scene it is not necessarily the same size of your view)
block.position = CGPoint(x: scene!.frame.midX, y: scene!.frame.maxY)

block.fillColor = SKColor.redColor()
block.physicsBody = SKPhysicsBody(circleOfRadius: 15)
block.physicsBody?.affectedByGravity = true
block.physicsBody?.restitution = 0
block.physicsBody?.linearDamping = 0
self.addChild(block)
}

// ...
}

关于xcode - 预期声明 Swift(Sprite Kit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34540484/

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