雪碧没有出现在场景中[关闭](Sprite is not showing up in scene [closed])

编程入门 行业动态 更新时间:2024-10-21 18:40:42
雪碧没有出现在场景中[关闭](Sprite is not showing up in scene [closed])

我制作了一个名为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)

enter image description here

更多推荐

本文发布于:2023-07-22 10:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:雪碧   景中   Sprite   closed   scene

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!