Swift - 添加physicsBody从场景中删除Sprite Node

时间:2015-02-15 17:28:09

标签: xcode swift

我正在创建我的第一个iOS应用。我有不同y值出现在屏幕两侧的鸟,并移动到屏幕的另一侧。我决定给鸟类添加一个物理体,这样当它们被球击中时就会发现碰撞。如果没有物理机构,可以看到鸟类,并按照它们的方式移动。当我添加一个物理体时,我在运行应用程序时无法再看到它们。有人可以帮忙吗?谢谢。

我在didMoveToView下将重力设置为零。

代码:

func createBird(){

    let randomSpawn = Int(arc4random_uniform(2))

    if randomSpawn == 1 {
    let newX = Int(-20)
    let newY = Int(arc4random_uniform(500) + 300)
    var p = CGPoint(x: newX, y: newY)
    var destination = CGPoint(x:Int(self.frame.width+21), y:newY)

    var moverSpeed = Int(arc4random_uniform(5) + 3)

    let birdRight = SKSpriteNode (imageNamed: "bird-right1")
    birdRight.xScale = 0.6
    birdRight.yScale = 0.6
    birdRight.position = p
    birdRight.zPosition = 2

    //birdRight.physicsBody = SKPhysicsBody(rectangleOfSize: birdRight.size)
    birdRight.physicsBody?.dynamic = true
    birdRight.physicsBody?.categoryBitMask = birdCategory
    birdRight.physicsBody?.contactTestBitMask = ballCategory
    birdRight.physicsBody?.collisionBitMask = 0

    let birdMoveRight = SKAction.animateWithTextures([
    SKTexture(imageNamed: "bird-right1"),
    SKTexture(imageNamed: "bird-right2"),
    SKTexture(imageNamed: "bird-right3"),
    SKTexture(imageNamed: "bird-right4"),
    SKTexture(imageNamed: "bird-right5"),
    SKTexture(imageNamed: "bird-right6"),
    SKTexture(imageNamed: "bird-right7"),
    SKTexture(imageNamed: "bird-right8"),
    SKTexture(imageNamed: "bird-right9"),
    SKTexture(imageNamed: "bird-right10")
    ], timePerFrame: 0.05)

    let foreverAction = SKAction.repeatActionForever(birdMoveRight)

    birdRight.runAction(foreverAction)

    let duration = NSTimeInterval(moverSpeed)

    let action = SKAction.moveTo(destination, duration: duration)

    birdRight.runAction(action)

    addChild(birdRight)


}

    else {

    let newX = Int(self.frame.width+20)
    let newY = Int(arc4random_uniform(500) + 300)
    var p = CGPoint(x: newX, y: newY)
    var destination = CGPoint(x:-21, y:newY)

    var moverSpeed = Int(arc4random_uniform(5) + 3)

    let birdLeft = SKSpriteNode(imageNamed: "bird-left1")
    birdLeft.xScale = 0.6
    birdLeft.yScale = 0.6
    birdLeft.position = p
    birdLeft.zPosition = 2

    let birdMoveLeft = SKAction.animateWithTextures([
    SKTexture(imageNamed: "bird-left1"),
    SKTexture(imageNamed: "bird-left2"),
    SKTexture(imageNamed: "bird-left3"),
    SKTexture(imageNamed: "bird-left4"),
    SKTexture(imageNamed: "bird-left5"),
    SKTexture(imageNamed: "bird-left6"),
    SKTexture(imageNamed: "bird-left7"),
    SKTexture(imageNamed: "bird-left8"),
    SKTexture(imageNamed: "bird-left9"),
    SKTexture(imageNamed: "bird-left10")
    ], timePerFrame: 0.05)

    birdLeft.physicsBody = SKPhysicsBody(rectangleOfSize: birdLeft.size)
    birdLeft.physicsBody?.dynamic = true
    birdLeft.physicsBody?.categoryBitMask = birdCategory
    birdLeft.physicsBody?.contactTestBitMask = ballCategory
    birdLeft.physicsBody?.collisionBitMask = 0
    birdLeft.physicsBody?.applyForce(CGVectorMake(-400, 0))
    let foreverAction = SKAction.repeatActionForever(birdMoveLeft)

    birdLeft.runAction(foreverAction)

    let duration = NSTimeInterval(moverSpeed)

   // let action = SKAction.moveTo(destination, duration: duration)

   // birdLeft.runAction(action)
    addChild(birdLeft)

    }
}

0 个答案:

没有答案