我正在使用3种不同的SKScenes(scene1,scene2,scene3)开发游戏。在GameViewController中,我将所有这些初始化为:
class GameViewController: UIViewController {
var hero = Hero()
var skView: SKView!
var scene1: SKScene!
var scene2: SKScene!
var scene3: SKScene!
override func viewDidLoad() {
super.viewDidLoad()
// init scenes
scene1 = SKScene(size: view.bounds.size, hero: hero)
scene2 = SKScene(size: view.bounds.size, hero: hero)
scene3 = SKScene(size: view.bounds.size, hero: hero)
scene1.scaleMode = .AspectFill
scene2.scaleMode = .AspectFill
scene3.scaleMode = .AspectFill
// set view
skView = self.view as SKView
// present the first scene
skView.presentScene(scene1)
}
我的想法是首先呈现第一个场景,然后呈现(swith to)另一个场景(即英雄更强时)。在每个场景中,英雄精灵都添加如下:
func addHero() {
let heroSprite = SKSpriteNode(imageNamed: "hero")
hero.sprite = heroSprite
heroSprite.position = CGPoint(x: size.width/4, y: size.height/2)
addChild(heroSprite)
}
在更新方法中,通过触摸来更新英雄位置。
func update() {
if touching {
hero.position++
}
}
Hero课程看起来像这样:
class Hero {
var sprite: SKSpriteNode?
}
问题是: 当仅初始化第一个场景(scene1)时,英雄可通过触摸移动。这意味着,使用上面的代码,英雄不再可以移动。
任何人都可以给我一些建议我做错了什么?提前谢谢!
PS:完整的代码可以在Github。
中找到答案 0 :(得分:0)
问题由另一个相关问题解决:Sprites must be cleaned before switch scenes with SpriteKit and Swift?
通过切换场景,请确保不要通过检查初始状态来添加两次精灵。例如,在" didMoveToView"," init"或"设置"方法:
if isInit {
do nothing
} else {
add sprites
isInit = true
}