本文介绍了SKShapeNode(circleOfRadius:半径)没有指定的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以 SKShapeNode(circleOfRadius: radius) 的形式快速创建 SKShapeNode 的子类,但没有为它指定初始化.

I'm trying to create a subclass of SKShapeNode in swift as SKShapeNode(circleOfRadius: radius) but there is no designated init for it.

任何人有任何解决方法或有关原因的信息吗?我不确定这是一个错误还是故意的.我发现这个视频演示了 SKSpriteNode 的解决方法,但它对我不起作用.https://skillsmatter.com/skillscasts/5695-how-to-subclass-a-skspritenode

Anyone have any workarounds or info about why? I'm not sure if this is a bug or intentional. I found this video demonstrating a workaround for SKSpriteNode but its not working for me. https://skillsmatter.com/skillscasts/5695-how-to-subclass-a-skspritenode

总的来说,我正在尝试为 SKShapeNode 创建一个子类,然后我可以再次对其进行子类化,以获得不同的版本,以便更轻松地管理我的代码.TIA

Overall i am trying to make a subclass for an SKShapeNode that i can then subclass from again to have different versions of to easier manage my code.TIA

谢谢马丁,我早先找到了那个例子.它可以工作,但我怎样才能把它变成一个圆形而不是一个矩形?

Thanks Martin i found that example earlier. It works but how would i make that into a circle instead of a rectangle?

import Foundation
import SpriteKit


    class Player : SKShapeNode {

        override init() {
            super.init()
            self.name = "Player"
            self.fillColor = UIColor.blackColor()

        }

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

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

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }

在主代码中

let playerOne = Player(rectOfSize: CGSize(width: 100, height: 100))

推荐答案

这是怎么回事?

class Player: SKShapeNode {

    init(circleOfRadius: CGFloat){
        super.init()

        let diameter = circleOfRadius * 2
        self.path = CGPathCreateWithEllipseInRect(CGRect(origin: CGPointZero, size: CGSize(width: diameter, height: diameter)), nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

这篇关于SKShapeNode(circleOfRadius:半径)没有指定的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 12:34