【问题标题】:Trying to resize sknode produces error "Cannot assign to the result of this expression"?尝试调整 sknode 的大小会产生错误“无法分配给此表达式的结果”?
【发布时间】:2014-08-28 02:05:28
【问题描述】:

我创建了一个名为 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

【问题讨论】:

    标签: swift sprite-kit sknode


    【解决方案1】:

    frame 属性是只读的,您可以在 SKNode.h 中看到

    @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.
    

    所以frame是由SKNode子类根据不同的属性计算出来的

    【讨论】:

    • self.frame 属性实际上是只读的,这满足了您的问题,因此不应该将这个答案标记为这样吗?
    猜你喜欢
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多