【问题标题】:Ending a Game When Two Nodes Collide in Swift当两个节点在 Swift 中碰撞时结束游戏
【发布时间】:2015-03-10 01:30:15
【问题描述】:

我正在尝试使用 SpriteKit 在 Swift 中制作一个飞扬的小鸟类型游戏。在我的游戏中,我希望游戏在小鸟离开屏幕时结束,而不是在小鸟与管道碰撞时结束游戏。游戏结束后,我希望它进入新屏幕。这是我到目前为止的代码。 导入 SpriteKit

class GameScene: SKScene {

    var bird = SKSpriteNode()
    var pipeUpTexture = SKTexture()
    var pipeDownTexture = SKTexture()
    var PipeMoveAndRemove = SKAction()
    let pipeGap = 150.0

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        //Physics
        self.physicsWorld.gravity = CGVectorMake(0.0, -5.0);

        //Bird
        var BirdTexture = SKTexture(imageNamed:"birdapp")
        BirdTexture.filteringMode = SKTextureFilteringMode.Nearest

        bird = SKSpriteNode(texture: BirdTexture)
        bird.setScale(0.5)
        bird.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)

        bird.physicsBody = SKPhysicsBody(circleOfRadius:bird.size.height/2.0);
        bird.physicsBody?.dynamic = true
        bird.physicsBody?.allowsRotation = false

        self.addChild(bird)

        //Ground
        var groundTexture = SKTexture(imageNamed:"groundapp")
        var sprite = SKSpriteNode(texture: groundTexture)
        sprite.setScale(2.0)
        sprite.position = CGPointMake(self.size.width/2.0, sprite.size.height/2.0)

        self.addChild(sprite)

        var ground = SKNode()

        ground.position = CGPointMake(0, groundTexture.size().height)
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))

        ground.physicsBody?.dynamic = false
        self.addChild(ground)

        //Pipes

        //create the pipes
        pipeUpTexture = SKTexture(imageNamed:"pipeupapp")
        pipeDownTexture = SKTexture(imageNamed:"pipedownapp")

        //move pipes
        let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
        let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
        let removePipes = SKAction.removeFromParent()

        PipeMoveAndRemove = SKAction.sequence([movePipes, removePipes])

        //spwan pipes
        let spawn = SKAction.runBlock({() in self.spawnPipes()})
        let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
        let spawnThenDelay = SKAction.sequence([spawn, delay])
        let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
        self.runAction(spawnThenDelayForever)
    }

    func spawnPipes(){
        let pipePair = SKNode()
        pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
        pipePair.zPosition = -10

        let height = UInt32(self.frame.size.height / 4)
        let y = arc4random() % height + height

        let pipeDown = SKSpriteNode(texture:pipeDownTexture)
        pipeDown.setScale(2.0)
        pipeDown.position = CGPointMake(0.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap))

        pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize:pipeDown.size)
        pipeDown.physicsBody?.dynamic = false
        pipePair.addChild(pipeDown)

        let pipeUp = SKSpriteNode(texture:pipeUpTexture)
        pipeUp.setScale(2.0)
        pipeUp.position = CGPointMake(0.0, CGFloat(y))

        pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size)
        pipeUp.physicsBody?.dynamic = false
        pipePair.addChild(pipeUp)

        pipePair.runAction(PipeMoveAndRemove)
        self.addChild(pipePair)

    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {

            let location = touch.locationInNode(self)

            bird.physicsBody?.velocity = CGVectorMake(0, 0)
            bird.physicsBody?.applyImpulse(CGVectorMake(0, 20))
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

【问题讨论】:

  • 在更新循环中,您可以检查您的鸟是否仍然可见。如果不是,请运行您的游戏结束功能。你试过什么?你有什么问题?
  • 在屏幕上看不到游戏时无法链接

标签: swift sprite-kit


【解决方案1】:

这就是你想要做的。这不是代码,您可以将其直接复制粘贴到您的游戏中。但它很容易理解,你应该能够修改它以满足你的需要

class GameScene: SKScene {

    let bird: SKSpriteNode = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 10, height: 10))
    var gameOver = false

    override init(size: CGSize) {
        super.init(size: size)

        self.bird.position = CGPoint(x: 0, y: 0)
        self.addChild(self.bird)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func endGame(){
        // restart the game
        let gameScene = GameScene(size: self.size)
        self.view!.presentScene(gameScene)
    }

    override func update(currentTime: NSTimeInterval) {


        // our bird doesnt intersect the frame, 
        // we use gameOver bool so we dont call endGame more than once
        if !self.frame.intersects(self.bird.frame) && !self.gameOver{
            self.gameOver = true
            self.endGame()
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多