Swift 切换场景时我做错了什么?
我正在开发一个关于精灵套件的游戏,当玩家输了,他们会被带到最后一幕,并给出再次玩的选项。我遇到的问题是,当玩家按下按钮再次玩游戏时,他们会被带到游戏场景,但游戏场景却显示为全蓝色。谁能告诉我我做错了什么?我在下面发布了尾景,如果你需要我发布游戏场景类,请提问Swift 切换场景时我做错了什么?,swift,sprite-kit,Swift,Sprite Kit,我正在开发一个关于精灵套件的游戏,当玩家输了,他们会被带到最后一幕,并给出再次玩的选项。我遇到的问题是,当玩家按下按钮再次玩游戏时,他们会被带到游戏场景,但游戏场景却显示为全蓝色。谁能告诉我我做错了什么?我在下面发布了尾景,如果你需要我发布游戏场景类,请提问 class EndScene: SKScene { var RestartBtn : UIButton! override func didMoveToView(view: SKView) { scene?
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()
}
}
游戏场景
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 */
}
}
导入SpriteKit
结构物理分类{
静态让敌人:UInt32=1//0000000000000000000000001
静态let项目符号:UInt32=2//00000000000000000010
静态let播放器:UInt32=3//00000000000000000000 100
}
类游戏场景:SKScene,SKPhysicContactDelegate{
var得分=Int()
var Player=SKSpriteNode(图像名为:“PlayerGalaga.png”)
var ScoreLbl=UILabel()
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
physicsWorld.contactDelegate=self
self.scene?.backgroundColor=UIColor.blackColor()
Player.position=CGPointMake(self.size.width/2,self.size.height/5)
Player.physicsBody=SKPhysicsBody(矩形大小:Player.size)
Player.physicsBody?.affectedByGravity=false
Player.physicsBody?.categoryBitMask=physicCatagory.Player
Player.physicsBody?.contactTestBitMask=physicCatagory.敌军
Player.physicsBody?.dynamic=false
var Timer=NSTimer.scheduledTimerWithTimeInterval(0.2,目标:self,
选择器:选择器(“生成子弹”),userInfo:nil,repeats:true)
var EmenyTimer=Ntimer.scheduledTimerWithTimeInterval(1.0,目标:
self,选择器:(“繁殖敌人”),userInfo:nil,repeats:true)
self.addChild(播放器)
ScoreLbl.text=“\(分数)”
ScoreLbl=UILabel(帧:CGRect(x:0,y:0,宽度:100,高度:20))
ScoreLbl.backgroundColor=UIColor(红色:0.1,绿色:0.1,蓝色:0.1,
α:0.3)
ScoreLbl.textColor=UIColor.whiteColor()
self.view?.addSubview(ScoreLbl)
}
func didBeginContact(联系人:SKPhysicsContact){
var firstBody:SKPhysicsBody=contact.bodyA
var secondBody:SKPhysicsBody=contact.bodyB
如果((firstBody.categoryBitMask==physiccatagory.敌军)和
(secondBody.categoryBitMask==PhysicCatagory.Bullet)||
(firstBody.categoryBitMask==physiccatagory.Bullet)和
(secondBody.categoryBitMask==physicCatagory.敌军)){
与Bullet冲突(第一个body.node为!SKSpriteNode,Bullet:
secondBody.node作为!SKSpriteNode)
}
如果((firstBody.categoryBitMask==physiccatagory.敌军)和
(secondBody.categoryBitMask==PhysicCatagory.Player)||
(firstBody.categoryBitMask==physiccatagory.Player)和
(secondBody.categoryBitMask==physicCatagory.敌军)){
与Person(firstBody.node作为!SKSpriteNode,Person:
secondBody.node作为!SKSpriteNode)
}
}
func与子弹碰撞(敌人:斯普瑞泰诺德,子弹:斯普瑞泰诺德){
敌人。从父对象中移除()
Bullet.removeFromParent()
得分++
ScoreLbl.text=“\(分数)”
}
func与人发生冲突(敌人:斯普林泰诺德,人:斯普林泰诺德){
敌人。从父对象中移除()
Player.removeFromParent()
self.view?.presentScene(EndScene())
ScoreLbl.removeFromSuperview()的
}
func{
var Bullet=SKSpriteNode(图像名为:“BulletGalaga.png”)
Bullet.zPosition=-5
Bullet.position=CGPointMake(Player.position.x,Player.position.y)
let action=SKAction.moveToY(self.size.height+30,持续时间:0.5)
让actionDone=SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action,actionDone]))
Bullet.physicsBody=SKPhysicsBody(矩形尺寸:Bullet.size)
Bullet.physicsBody?.categoryBitMask=PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask=physicCatagory.敌军
Bullet.physicsBody?重力影响=假
Bullet.physicsBody?.dynamic=false
self.addChild(项目符号)
}
func(){
var敌方=SKSpriteNode(图像名为:“EnemyGalaga.png”)
var MinValue=self.size.width/8
var MaxValue=self.size.width-20
变量SpawnPoint=UInt32(最大值-最小值)
敌方位置=CGPointMake(CGFloat(arc4random_统一(繁殖点)),
自身尺寸、身高)
敌方.physicsBody=SKPhysicsBody(矩形尺寸:敌方.size)
敌方.physicsBody?.categoryBitMask=物理分类.敌方
敌方.physicsBody?.contactTestBitMask=PhysicsCatagory.Bullet
敌人。物理身体?。受重力影响=错误
敌人.物理体?.dynamic=真
let action=SKAction.moveToY(-70,持续时间:2.0)
让actionDone=SKAction.removeFromParent()
敌方.runAction(SKAction.sequence([action,actionDone]))
自我补充儿童(敌人)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:
UIEvent){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
Player.position.x=location.x
}
}
覆盖功能触摸移动(触摸:设置,带事件:
UIEvent){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
Player.position.x=location.x
}
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}
据我所见,当与person发生冲突时,您将从视图中删除节点,因此当您从EndScene中的restart()函数中调用GameSecene时,节点仍将消失
您可以在GameSecene中创建func initialScene(),在该函数中创建所有内容,并在didMoveToView函数中调用它(而不是在didMoveToView函数中执行所有操作)。这样,每当你展示另一个类的游戏场景时,它都会调用这个函数并重新创建所有的东西