使用Swift子类化SKShapeNode [英] Subclassing SKShapeNode with Swift

查看:178
本文介绍了使用Swift子类化SKShapeNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Swift子类化 SKShapeNode 。到目前为止我有这样的:

I'm trying to subclass SKShapeNode with Swift. So far I've got something like this:

import UIKit
import SpriteKit

class STGridNode: SKShapeNode {

    init() {
        super.init()
        self.name = "STGridNode"
        self.fillColor = UIColor(red: 0.11, green: 0.82, blue: 0.69, alpha: 1)
    }

}


b $ b

在我的代码中,我想这样做:

In my code I want so do something along the lines of:

let s = STGridNode(rectOfSize: CGSize(width: 100, height: 100))

所以我的问题是 - 在 STGridNode 的初始化程序中的code> rectOfSize ?我试过:

So my question is - how do I implement rectOfSize in the initialiser for STGridNode? I've tried:

init(rectOfSize: CGPoint) {
    super.init(rectOfSize: rectOfSize);
}

但是给出一个错误:'无法找到init的重载提供的参数

But that gives an error: 'Could not find an overload for init that accepts the supplied arguments'

推荐答案

您尝试的代码有两个问题:

You have two problems with the code you tried:


  1. rectOfSize 中SKShapeNode 接受 CGSize 不是 CGPoint

  2. rectOfSize c $ c> SKShapeNode 是一个便利初始值设置,所以你不能从子类调用它。您必须自己调用 super.init()并实现rect功能

  1. rectOfSize in SKShapeNode takes a CGSize not a CGPoint
  2. rectOfSize in SKShapeNode is a convenience initializer so you won't be able to call it from a subclass. You will have to call super.init() and implement the rect functionality yourself

你可以这样做:

init(rectOfSize: CGSize) {
    super.init()

    var rect = CGRect(origin: CGPointZero, size: rectOfSize)
    self.path = CGPathCreateWithRect(rect, nil)
}

这篇关于使用Swift子类化SKShapeNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆