我制作了一个名为Player的SKShapeNode子类:
import UIKit import SpriteKit let MOVE_UP: CGFloat = 3 class Player: SKShapeNode{ var lifePoints = 3 init(circleOfRadius: CGFloat, fillColor: UIColor, strokeColor: UIColor) { super.init() self.fillColor = fillColor self.strokeColor = strokeColor } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } class func movePlayerUp(mPlayer: SKShapeNode){ mPlayer.position.y += MOVE_UP } }在GameScene我试图创建一个类型为Player的精灵并将其显示在屏幕上。 我没有错误,构建不会失败,但精灵不会显示在屏幕上。 这是我创建精灵的方式:
override func didMove(to view: SKView) { let sprite = Player(circleOfRadius: 45, fillColor: myColors.blue, strokeColor: myColors.yellow) sprite.position = CGPoint(x: frame.midX, y: frame.midY) self.addChild(sprite) }I made a SKShapeNode subclass called Player:
import UIKit import SpriteKit let MOVE_UP: CGFloat = 3 class Player: SKShapeNode{ var lifePoints = 3 init(circleOfRadius: CGFloat, fillColor: UIColor, strokeColor: UIColor) { super.init() self.fillColor = fillColor self.strokeColor = strokeColor } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } class func movePlayerUp(mPlayer: SKShapeNode){ mPlayer.position.y += MOVE_UP } }in GameScene I'm trying to create a sprite of type Player and show it on the screen. I have no error and the build does not fail but the sprite won't show up on the screen. this is how I'm creating the sprite:
override func didMove(to view: SKView) { let sprite = Player(circleOfRadius: 45, fillColor: myColors.blue, strokeColor: myColors.yellow) sprite.position = CGPoint(x: frame.midX, y: frame.midY) self.addChild(sprite) }最满意答案
问题是你的super.init() ...它是空白的......你用一个婴儿车circleOfRadius制作了你自己的初始化程序,但这对计算机没有任何意义,因为你什么也没做 。
更糟糕的是, super.init(circleOfRadius: circleOfRadius)将不起作用,因为它不是一个指定的初始化程序 ......它是一个方便的初始化程序,在调用super.init时不能使用它
所以,基本上你必须求助于使用super.init() ,然后*改变你刚刚做出的空白形状的路径
添加这个,你会得到你的圈子:
self.path = CGPath.init(roundedRect: CGRect(x: 0, y: 0, width: circleOfRadius*2, height: circleOfRadius*2), cornerWidth: 45, cornerHeight: 45, transform: nil)
The problem is your super.init()... it's blank... you made your own initializer with a pram circleOfRadius but this means nothing to the computer since you did nothing with it.
Worse, is that super.init(circleOfRadius: circleOfRadius) won't work either because it is not a designated initializer... it is a convenience initializer which you can't use when calling to a super.init
So, basically you must resort to using super.init() but then *change the path of the empty shape you just made`
Add this and you will get your circle:
self.path = CGPath.init(roundedRect: CGRect(x: 0, y: 0, width: circleOfRadius*2, height: circleOfRadius*2), cornerWidth: 45, cornerHeight: 45, transform: nil)更多推荐
发布评论