我这里有一个结构,用于将函数传递给GameScene,我可以随时调用它。
问题是行GameScene.addChild(explosionFirst)
生成以下错误:
'SKSpriteNode'无法转换为'GameScene'
我如何将其他类的节点添加到GameScene?
import Foundation
import SpriteKit
struct Explosion {
var explosions = [SKTexture]()
mutating func spawnExplosion(spawnPosition: CGPoint) {
for i in 0...79 {
let explosion = SKTexture(imageNamed: String(i))
explosions.append(explosion)
}
let explosionFirst = SKSpriteNode(texture: explosions[0])
explosionFirst.position = spawnPosition
explosionFirst.zPosition = 1
explosionFirst.setScale(5)
GameScene.addChild(explosionFirst)
let animation = SKAction.animate(with: explosions, timePerFrame: 0.1)
let scaleIn = SKAction.scale(to: 10, duration: 2.3)
let fadeOut = SKAction.fadeOut(withDuration: 1.0)
let delete = SKAction.removeFromParent()
let explosionSequence = SKAction.sequence([scaleIn, fadeOut, delete])
explosionFirst.run(animation)
explosionFirst.run(explosionSequence)
}
}
答案 0 :(得分:2)
编译器看到self.GameScene.addChild
并且你没有引用你的GameScene对象所以创建一个
var gameScene : GameScene!
现在使用:
gameScene.addChild(explosionFirst)
注意:默认情况下,您的初始化工具现在为gameScene: GameScene
,因此您必须在Gamescene
init
依赖关系