我已经创建了一个名为level的sknode子类,我正在尝试更改它的大小,但是我得到了一个错误“无法分配给这个表达式的结果”。
我做错什么了?

class Level: SKNode {

    override init() {
        super.init()
        self.frame.size= CGSizeMake(width: 100, height: 100) // Cannot assign to the result of this expression

最佳答案

如sknode.h中所示,frame属性是只读的。

@property (SK_NONATOMIC_IOSONLY, readonly) CGRect frame;

我建议您多阅读一下SKNode文档:https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/Reference/Reference.html
在文档中,您将找到以下段落:
The frame property provides the bounding rectangle for a node’s visual content, modified
by the scale and rotation properties. The frame is non-empty if the node’s class draws
content. Each node subclass determines the size of this content differently. In some
subclasses, the size of the node’s content is declared explicitly, such as in the
SKSpriteNode class. In other subclasses, the content size is calculated implicitly by the
class using other object properties. For example, a SKLabelNode object determines its
content size using the label’s message text and font characteristics.

因此,根据不同的属性,sknode子类计算frame

10-08 12:29