我用以下方法创建了播放器类:
class Player : SKSpriteNode, GameSprite {
var initialSize = CGSize(width:50, height:50 )
var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "Dino")
func onTap()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
然后我尝试使用以下命令在GameScene.swift中添加玩家类的实例:
class GameScene: SKScene {
override func didMove(to view: SKView) {
self.anchorPoint = .zero
self.backgroundColor = UIColor(red: 0.95, green: 0.1, blue: 0.25, alpha: 1.0)
let player = Player()
player.size = CGSize(width: 50, height: 50)
player.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
self.addChild(player)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches) {
let location = touch.location(in: self)
let nodeTouched = atPoint(location)
if let gameSprite = nodeTouched as? GameSprite {
gameSprite.onTap()
}
}
}
}
但是发生了一个错误,说:
调用中缺少参数'coder'的参数
在读取let player = Player()
谢谢!