Swift SpriteKit游戏给出错误'尝试添加已经拥有父母的SKNode'

时间:2018-06-16 11:36:50

标签: swift sprite-kit swift4 skaction

我有一个SpriteKit游戏,我正在使用SKAction移动我的流星(流星是敌人)一直到屏幕末端的玩家(玩家位于屏幕的底部,他们只能在x轴上移动来躲避流星而不是y)必须躲避这些流星。我只是试图在屏幕上一次使用SKAction朝着玩家的方向移动一个流星。屏幕上一次只能出现在流星上,但我得到错误'尝试添加已经拥有父母的SKNode'。我的SKAction将流星添加到屏幕上,但在流星移动到目的地之后将其删除,然后到达屏幕底部。那么为什么我会收到此错误以及如何解决?

注意:SKAction使用meteor.removeFromParent()

从屏幕上移除流星

这是我的代码:

import SpriteKit
import GameplayKit
import UIKit



class GameScene: SKScene, SKPhysicsContactDelegate{

    let player = SKSpriteNode(imageNamed: "spaceship")
    let stars = SKSpriteNode(imageNamed: "stars")
    let meteor = SKSpriteNode(imageNamed: "meteor")
    var scoreLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Bold")
    var score:Int = 0
    var playerLost:Bool = false


    override func didMove(to view: SKView) {

        physicsWorld.contactDelegate = self
        print(frame.size.width)
        print(frame.size.height)

        stars.position = CGPoint(x:0, y:0)
        stars.zPosition = 1

        player.position = CGPoint(x:0, y:-320)
        player.zPosition = 4
        player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
        player.physicsBody?.affectedByGravity = false
        player.physicsBody?.isDynamic = false

        player.physicsBody?.categoryBitMask = 2
        player.physicsBody?.collisionBitMask = 2
        player.physicsBody?.contactTestBitMask = 1



        self.addChild(player)
        self.addChild(stars)

        self.addMeteor()
        self.setLabel()

        //spawnEnemySKAction()


    }


    func spawnEnemySKAction() {
        let spawn = SKAction.run(addMeteor)
        let waitToSpawn = SKAction.wait(forDuration: 1)
        let spawnSequence = SKAction.sequence([spawn, waitToSpawn])
        let spawnForever = SKAction.repeatForever(spawnSequence)
        self.run(spawnForever)
    }


    func didBegin(_ contact: SKPhysicsContact) {
        gameOver()

    }

    func addMeteor() {

        meteor.physicsBody = SKPhysicsBody(texture: meteor.texture!, size: meteor.size)
        meteor.physicsBody?.affectedByGravity = false
        meteor.setScale(0.50)
        meteor.position = CGPoint(x:Int(arc4random()%300),y:Int(arc4random()%600))
        //meteor.position = CGPoint(x:0 , y:0)
        meteor.zPosition = 4

        let moveMeteor = SKAction.moveTo(y: player.position.y - 300, duration: 1.5)
        let deleteMeteor = SKAction.removeFromParent()
        let meteorSequence = SKAction.sequence([moveMeteor, deleteMeteor])
        meteor.run(meteorSequence)

        meteor.physicsBody?.categoryBitMask = 1
        meteor.physicsBody?.collisionBitMask = 2
        meteor.physicsBody?.contactTestBitMask = 2

        self.addChild(meteor)
        }

    func fireBullet() {
        let bullet = SKSpriteNode(imageNamed: "bullet")
        bullet.position = player.position
        bullet.setScale(0.5)
        bullet.zPosition = 3
        self.addChild(bullet)
        let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
        let deleteBullet = SKAction.removeFromParent()
        let bulletSequence = SKAction.sequence([moveBullet, deleteBullet])
        bullet.run(bulletSequence)

    }
    func setLabel() {
        scoreLabel.text = "Score: " + String(score)
        scoreLabel.position = CGPoint(x: 290, y: 590)
        scoreLabel.zPosition = 20
    }

    func gameOver() {
        playerLost = true
        var gameOverLabel: SKLabelNode!
        gameOverLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Bold")
        gameOverLabel.text = "Game Over! You lost! Your score was: " + String(score)
        gameOverLabel.zPosition = 10
        gameOverLabel.position = CGPoint(x: 0, y:0)
        self.addChild(gameOverLabel)
        player.removeFromParent()
        meteor.removeFromParent()
        stars.removeFromParent()
        scoreLabel.removeFromParent()

    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if playerLost == false {
        fireBullet()
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch:  AnyObject in touches {
            let pointOfTouch = touch.location(in: self)
            let previousPointOfTouch = touch.previousLocation(in: self)
            let amountDragged = pointOfTouch.x - previousPointOfTouch.x
            player.position.x += amountDragged
        }

    }


    override func update(_ currentTime: TimeInterval) {
        addMeteor()
        if meteor.position.y < player.position.y - 300 && playerLost == false {
            meteor.removeFromParent()
            addMeteor()
            if playerLost == false {
                score += 1
            }
            scoreLabel.removeFromParent()
            scoreLabel.text = "Score: " + String(score)
            self.addChild(scoreLabel)
        }
    }
}

感谢您花时间看看这个问题!

1 个答案:

答案 0 :(得分:-1)

我只需要在更新功能中删除self.addChild(meteor)