切换场景时我做错了什么?

时间:2015-06-06 00:55:04

标签: swift sprite-kit

我正在开发一个关于精灵套件的游戏,当玩家失败时,他们会被带到EndScene并再次选择玩游戏。我遇到的问题是,当玩家按下按钮再次播放时,他们会被带到GameScene,而GameScene则显示为全蓝色。谁能告诉我我做错了什么?我在下面发布了EndScene,如果你需要我发布GameScene课程,请问。

class EndScene: SKScene {
    var RestartBtn : UIButton!

    override func didMoveToView(view: SKView) {
        scene?.backgroundColor = UIColor.whiteColor()

        RestartBtn = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 30))
        RestartBtn.setTitle("Restart", forState: UIControlState.Normal)
        RestartBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        RestartBtn.addTarget(self, action: Selector("Restart"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view?.addSubview(RestartBtn)

    }

    func Restart() {
        // close EndScene and start the game again

        self.view?.presentScene(GameScene())

        RestartBtn.removeFromSuperview()
    }
}

GameScene

import SpriteKit


struct PhysicsCatagory {
static let Enemy : UInt32 = 1 //000000000000000000000000000001
static let Bullet : UInt32 = 2 //00000000000000000000000000010
static let Player : UInt32 = 3 //00000000000000000000000000100
}


class GameScene: SKScene, SKPhysicsContactDelegate {

var Score = Int()

var Player = SKSpriteNode(imageNamed: "PlayerGalaga.png")

var ScoreLbl = UILabel()

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

    physicsWorld.contactDelegate = self

    self.scene?.backgroundColor = UIColor.blackColor()

    Player.position = CGPointMake(self.size.width / 2, self.size.height / 5)
    Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
    Player.physicsBody?.affectedByGravity = false
    Player.physicsBody?.categoryBitMask = PhysicsCatagory.Player
    Player.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
    Player.physicsBody?.dynamic = false



    var Timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self,                
    selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)

    var EmenyTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target:                                 
    self, selector: ("SpawnEnemies"), userInfo: nil, repeats: true)


    self.addChild(Player)

    ScoreLbl.text = "\(Score)"
    ScoreLbl = UILabel(frame: CGRect(x: 0,y: 0, width: 100, height: 20))
    ScoreLbl.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, 
    alpha:0.3 )
    ScoreLbl.textColor = UIColor.whiteColor()
    self.view?.addSubview(ScoreLbl)


    }

    func didBeginContact(contact: SKPhysicsContact) {
    var firstBody : SKPhysicsBody = contact.bodyA
    var secondBody : SKPhysicsBody = contact.bodyB

    if ((firstBody.categoryBitMask == PhysicsCatagory.Enemy) && 
    (secondBody.categoryBitMask == PhysicsCatagory.Bullet) ||
        (firstBody.categoryBitMask == PhysicsCatagory.Bullet) &&    
    (secondBody.categoryBitMask == PhysicsCatagory.Enemy)){
        CollisionWithBullet(firstBody.node as! SKSpriteNode, Bullet: 
    secondBody.node as! SKSpriteNode)

    }
    else if ((firstBody.categoryBitMask == PhysicsCatagory.Enemy) && 
    (secondBody.categoryBitMask == PhysicsCatagory.Player) ||
        (firstBody.categoryBitMask == PhysicsCatagory.Player) && 
    (secondBody.categoryBitMask == PhysicsCatagory.Enemy)){

            CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: 
     secondBody.node as! SKSpriteNode)
       }

    }


    func CollisionWithBullet(Enemy: SKSpriteNode, Bullet:SKSpriteNode){
        Enemy.removeFromParent()
        Bullet.removeFromParent()
        Score++
        ScoreLbl.text = "\(Score)"

    }
    func CollisionWithPerson(Enemy:SKSpriteNode, Person:SKSpriteNode){
    Enemy.removeFromParent()
    Player.removeFromParent()
    self.view?.presentScene(EndScene())
    ScoreLbl.removeFromSuperview()

     }



    func SpawnBullets(){
    var Bullet = SKSpriteNode(imageNamed: "BulletGalaga.png")
    Bullet.zPosition = -5

    Bullet.position = CGPointMake(Player.position.x, Player.position.y)

    let action = SKAction.moveToY(self.size.height + 30, duration: 0.5)
    let actionDone = SKAction.removeFromParent()
    Bullet.runAction(SKAction.sequence([action, actionDone]))


    Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
    Bullet.physicsBody?.categoryBitMask = PhysicsCatagory.Bullet
    Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
    Bullet.physicsBody?.affectedByGravity = false
    Bullet.physicsBody?.dynamic = false
    self.addChild(Bullet)


}

func SpawnEnemies(){
    var Enemy = SKSpriteNode(imageNamed: "EnemyGalaga.png")
    var MinValue = self.size.width / 8
    var MaxValue = self.size.width - 20
    var SpawnPoint = UInt32(MaxValue - MinValue)
    Enemy.position = CGPointMake(CGFloat(arc4random_uniform(SpawnPoint)),          
    self.size.height)
    Enemy.physicsBody = SKPhysicsBody(rectangleOfSize: Enemy.size)
    Enemy.physicsBody?.categoryBitMask = PhysicsCatagory.Enemy
    Enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
    Enemy.physicsBody?.affectedByGravity = false
    Enemy.physicsBody?.dynamic = true

    let action = SKAction.moveToY(-70, duration: 2.0)
    let actionDone = SKAction.removeFromParent()
    Enemy.runAction(SKAction.sequence([action, actionDone]))
    self.addChild(Enemy)

     }


     override func touchesBegan(touches: Set<NSObject>, withEvent event:     
     UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        Player.position.x = location.x


        }

       }
    override func touchesMoved(touches: Set<NSObject>, withEvent event:    
    UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        Player.position.x = location.x
        }

     }

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

1 个答案:

答案 0 :(得分:0)

从我所看到的,当与人发生碰撞时,你正在从视图中删除节点,所以当你从EndScene的restart()函数中调用GameScene时它们仍然会消失。

你可以做的是在GameScene中创建一个func initialScene(),你可以在该函数中创建所有内容并在didMoveToView函数中调用它(而不是在didMoveToView函数中完成所有操作)。这样,无论何时从另一个类呈现GameScene,它都会调用该函数并重新创建您需要的所有内容,就像您实际重新启动一样。希望这是一个有用的开始,如果你还需要其他任何评论。

相关问题