Thread1信号SIGABRT表示NSInvalid Argument Exception

时间:2017-05-19 06:35:04

标签: ios swift xcode exception sigabrt

我遇到了这个错误,我无法弄清楚我的代码有什么问题。这是控制台窗口所说的内容,但之前的代码很好,我添加的新内容与它所说的代码无关。有谁能理解什么是错的? 这是Xcode告诉我的:

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:'试图添加一个SKNode   已经有一个父:name:'(null)'   纹理:['气球'(100 x 91)]位置:{ - 144,600}   比例:{1.00,1.00}大小:{100,91}锚:{0.5,0.5}旋转:0.00'

另外,我的代码:

 private var label : SKLabelNode?
    private var spinnyNode : SKShapeNode?
    var flyingPig:SKSpriteNode!
    var water = SKSpriteNode()
    var scoreLabel :SKLabelNode!

    var totalTime = 0

    {
        didSet{
            scoreLabel.text = "Score: \(totalTime) km"
        }
    }

    struct PhysicsCategories{

    static let pig : UInt32 = 0b1
    static let aFish : UInt32 = 0b100
}

var timer : Timer!
var timing : Timer!
var fish = ["bluedy","lantern","balloon","Knife"]



var lives:[SKSpriteNode]!

override func didMove(to view: SKView) {

    self.physicsWorld.contactDelegate = self
    water = SKSpriteNode(imageNamed: "water")

    water.position = CGPoint(x: 0,y:0)

    water.physicsBody = SKPhysicsBody(rectangleOf: water.size)

    water.physicsBody?.affectedByGravity = false
    water.physicsBody?.isDynamic = false

    self.addChild(water)

    flyingPig = SKSpriteNode(imageNamed:"FlyingPig")
    flyingPig.position = CGPoint(x:0,y:0)
    flyingPig.physicsBody = SKPhysicsBody(rectangleOf: flyingPig.size)
    flyingPig.physicsBody!.affectedByGravity=false
    flyingPig.physicsBody!.categoryBitMask = PhysicsCategories.pig
    flyingPig.physicsBody!.contactTestBitMask = PhysicsCategories.aFish
    self.addChild(flyingPig)

    self.physicsWorld.gravity = CGVector(dx:0,dy:0)
    self.physicsWorld.contactDelegate = self

    scoreLabel = SKLabelNode(text:"Score: 0")
    scoreLabel.position = CGPoint(x:-290,y:600)
    scoreLabel.fontName = "PartyLetPlain"
    scoreLabel.fontSize = 44
    self.addChild(scoreLabel)

    timer = Timer.scheduledTimer(timeInterval:1,target:self,selector:#selector(addFish),userInfo:nil,repeats:true)
    timing = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)

    addLives()


}


func addLives()
{
    lives = [SKSpriteNode()]
    for live in 1...3{
        let liveNode = SKSpriteNode(imageNamed:"FlyingPig2")
        liveNode.position = CGPoint(x:350-CGFloat(4-live)*liveNode.size.width,y:610)
        self.addChild(liveNode)
        lives.append(liveNode)
    }
}

func addFish()
{
    fish = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: fish) as![String]

    let aFish = SKSpriteNode(imageNamed: fish[0])

    let fishPosition = GKRandomDistribution(lowestValue:-300, highestValue:300)
    let position = CGFloat(fishPosition.nextInt())
    aFish.position=CGPoint(x:position,y:600)

    aFish.physicsBody = SKPhysicsBody(rectangleOf: aFish.size)
    aFish.physicsBody!.affectedByGravity = false
    aFish.physicsBody?.isDynamic=true
    aFish.physicsBody!.categoryBitMask = PhysicsCategories.aFish

    aFish.physicsBody!.contactTestBitMask = PhysicsCategories.pig
    self.addChild(aFish)

    let nemo = SKSpriteNode(imageNamed: "nemo")

    let nemoPosition = GKRandomDistribution(lowestValue:-300, highestValue:300)
    let nposition = CGFloat(nemoPosition.nextInt())
    nemo.position=CGPoint(x:nposition,y:600)

    nemo.physicsBody = SKPhysicsBody(rectangleOf: aFish.size)
    nemo.physicsBody!.affectedByGravity = false
    nemo.physicsBody?.isDynamic=true



    self.addChild(aFish)
    self.addChild(nemo)

    let animationDuration:TimeInterval = 4
    var actionArray = [SKAction]()
    actionArray.append(SKAction.move(to:CGPoint(x:position,y:-800),duration:animationDuration))
    aFish.run(SKAction.sequence(actionArray))
        }





func didBegin(_ contact: SKPhysicsContact) {
    var body1 = SKPhysicsBody()
    var body2 = SKPhysicsBody()

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
        body1 = contact.bodyA
        body2 = contact.bodyB
    }
    else{
        body1 = contact.bodyB
        body2 = contact.bodyA
    }

    if body1.categoryBitMask == PhysicsCategories.pig && body2.categoryBitMask==PhysicsCategories.aFish
    {
        body2.node?.removeFromParent()
    }
}

1 个答案:

答案 0 :(得分:0)

您要添加一个孩子两次

self.addChild(aFish)

....


self.addChild(aFish)
self.addChild(nemo)

只需删除一个电话就可以了。