Am trying to subclass SKShapeNode yet it never works. Specifically the SKShapeNode(rectOf: CGSize).
Trying to use init(), super.init, and sometimes the compiler tells me to use the designated initializer, which I cannot find to save my life. Am using Xcode 11 and Swift 5.
Here is a sample of one of my declaration attempts and my call. The self element in the call is from the background pic. It works if I create an SKShapeNode directly using the same parameters. But would like to subclass this so I can add custom methods and properties.
I've scoured the internet and so many other forums. Not one reply. I'm desperate at this point.
Thank you
p.s. I apologize this is a repost. But I only realized too late that I used the wrong tags.
In this sample code I removed all the part that fill color, and position, set the zPosition, etc. because it all works if I do it straight from my GameScene, without using a custom SubClassed attempt.
Trying to use init(), super.init, and sometimes the compiler tells me to use the designated initializer, which I cannot find to save my life. Am using Xcode 11 and Swift 5.
Here is a sample of one of my declaration attempts and my call. The self element in the call is from the background pic. It works if I create an SKShapeNode directly using the same parameters. But would like to subclass this so I can add custom methods and properties.
I've scoured the internet and so many other forums. Not one reply. I'm desperate at this point.
Thank you
p.s. I apologize this is a repost. But I only realized too late that I used the wrong tags.
In this sample code I removed all the part that fill color, and position, set the zPosition, etc. because it all works if I do it straight from my GameScene, without using a custom SubClassed attempt.
Code Block class PegBase : SKShapeNode { init(rectOfPegBase: CGSize) { super.init() self.fillColor = SKColor.red self.strokeColor = SKColor.red self.Position = 0 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }} let childP = PegBase(rectOfPegBase: CGSize(width: self.size.width * 0.8, height: self.size.height * 0.2)) self.addChild(childP)
Ok, the code below works, as I can tell because the pegBaseBoard is filled with red. So if I consider this step one, how do I start passing extra parameters so I can set the fill, and other properties in the subClass file?
i
i
Code Block mport Foundation import SpriteKit class PegBaseBoard : SKShapeNode { override init() { super.init() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
Code Block import SpriteKit import GameplayKit class GameScene: SKScene, SKPhysicsContactDelegate { var pegBaseBoard = PegBaseBoard() override func didMove(to view: SKView) { let background = SKSpriteNode(imageNamed: "background") background.size = self.size background.position = CGPoint(x: self.size.width/2, y: self.size.height/2) background.zPosition = -1 self.addChild(background) pegBaseBoard = PegBaseBoard.init(rectOf : CGSize(width: self.size.width * 0.8, height: self.size.height * 0.2)) pegBaseBoard.fillColor = SKColor.red self.addChild(pegBaseBoard) } }