spritekit中的场景转换没有转换

时间:2014-09-07 17:07:50

标签: ios swift sprite-kit

我在sprite kit中快速制作游戏。 (我是swift的新手)我想在按下节点时转换场景。我查了一下,转换的正确方法是

    let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)     
    let scene = GameScene(size: self.scene.size)
    scene.scaleMode = SKSceneScaleMode.AspectFill
    self.scene.view.presentScene(scene, transition: transition)

但是当我编程时,它并不像self.scene.size那样在场景上添加问号。这是我的代码

    let playView = PlayScene(size: self.scene?.size) 

而且我不知道如何解决这个问题。有什么建议? (或者说,我可能只是一个简单的错误,因为我是swift的新手。

这是我的更多代码 在GameScene.swift文件中

类GameScene:SKScene {

var playButton = SKSpriteNode(imageNamed: "Play")
var levelsButton = SKSpriteNode(imageNamed: "Levels")
var optionsButton = SKSpriteNode(imageNamed: "Options")

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let myLabel = SKLabelNode(fontNamed:"Helvetica-Light")
    myLabel.text = "Mini Golf";
    myLabel.fontSize = 30;
    myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:525);
    println(size.height)
    println(size.width)
    let backgroundNode = SKSpriteNode(imageNamed: "Background")

    backgroundNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame));
    playButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: 360)
    levelsButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: 295)
    optionsButton.position = CGPoint(x: CGRectGetMidX(self.frame), y: 230)

    self.addChild(backgroundNode)
    self.addChild(playButton)
    self.addChild(levelsButton)
    self.addChild(optionsButton)
    self.addChild(myLabel)
}

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

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        //let touchedNode = nodeAtPoint(Location)
        //touchedNode.postiion =location
        let transitionToPlayScene = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
        if CGRectContainsPoint(playButton.frame, location) {
            let playView = PlayScene(size: self.scene?.size)




        }
    }
}

这是PlayScene.swift文件中的代码

import Foundation
import UIKit
import SpriteKit

private let WALL_CATEGORY: UInt32 = 0x1 << 0
private let HOLE_CATEGORY: UInt32 = 0x1 << 1
private let GOLF_CLUB_CATEGORY: UInt32 = 0x1 << 2
private let BALL_CATEGORY: UInt32 = 0x1 << 4
private let DECORATION_BORDER_CATEGORY: UInt32 = 0x1 << 5


class PlayScene: SKScene, SKPhysicsContactDelegate  {

var ballNode = SKSpriteNode(imageNamed: "Ball")
var flag = SKSpriteNode(imageNamed: "Flag")
var club = SKSpriteNode(imageNamed: "Golf Club")
var tee = SKSpriteNode(imageNamed: "Tee")
var hole = SKSpriteNode(imageNamed: "Hole")
var decoBorder1 = SKSpriteNode(imageNamed: "Border 1")
var decoBorder2 = SKSpriteNode(imageNamed: "Border 2")

override func didMoveToView(view: SKView) {
    //Setting up the view's physics
    let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame);
    self.physicsBody = borderBody
    self.physicsBody?.friction = 0.0
    self.physicsBody?.categoryBitMask  = WALL_CATEGORY
    self.physicsWorld.gravity = CGVectorMake(0.0 ,0.0);
    self.physicsWorld.contactDelegate = self

    //Setting the ball's properties
    ballNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: 100);
    ballNode.physicsBody = SKPhysicsBody(rectangleOfSize: ballNode.frame.size);
    ballNode.physicsBody?.categoryBitMask = BALL_CATEGORY
    ballNode.physicsBody?.mass = 50.0
    ballNode.physicsBody?.dynamic = true
    ballNode.physicsBody?.allowsRotation = true
    self.addChild(ballNode)

    //Setting the club's properties
    club.position = CGPoint(x: CGRectGetMidX(self.frame)-20, y: 75);
    club.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
    club.physicsBody?.categoryBitMask = GOLF_CLUB_CATEGORY
    club.physicsBody?.mass = 150.0
    club.physicsBody?.dynamic = true
    club.physicsBody?.allowsRotation = false
    self.addChild(club)

    //Setting the Tee's properties
    tee.position = CGPoint(x: CGRectGetMidX(self.frame), y: 70);
    //self.addChild(tee)

    //Setting the decoration border nodes
    decoBorder1.position = CGPoint(x: CGRectGetMinX(self.frame), y: 500);
    decoBorder2.position = CGPoint(x: CGRectGetMaxX(self.frame), y:500);
    decoBorder1.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
    decoBorder2.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
    decoBorder1.physicsBody?.categoryBitMask = DECORATION_BORDER_CATEGORY
    decoBorder1.physicsBody?.dynamic = false;
    decoBorder1.physicsBody?.allowsRotation = false
    decoBorder2.physicsBody?.categoryBitMask = DECORATION_BORDER_CATEGORY
    decoBorder2.physicsBody?.dynamic = false;
    decoBorder2.physicsBody?.allowsRotation = false
    //self.addChild(decoBorder1)
    //self.addChild(decoBorder2))

    //Setting the Hole properties
    hole.position = CGPoint(x: CGRectGetMidX(self.frame), y: 550);
    hole.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size);
    hole.physicsBody?.categoryBitMask = HOLE_CATEGORY
    hole.physicsBody?.dynamic = false
    hole.physicsBody?.allowsRotation = false
    //self.addChild(hole)
}

2 个答案:

答案 0 :(得分:0)

问号来自optional chaining,因为您的scene属性必须已声明为此SKScene?。这不是一个问题,它只是意味着如果你的场景为零,它就不会获得尺寸属性。

真正的问题可能是这一行:

self.scene.view.presentScene(scene, transition: transition)

原因是,scene在提出之前可能没有view

你可能想要这样的东西:

(self.view as SKView).presentScene(scene, transition: transition)

你会注意到我说了很多。我只是在猜测这个问题,因为我没有看到足够的代码,但我很感激努力尝试缩小你的问题。你应该发布更多,因为它可能在其他地方。

修改

尝试替换它:

    let transitionToPlayScene = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
    if CGRectContainsPoint(playButton.frame, location) {
        let playView = PlayScene(size: self.scene?.size)




    }

有了这个

    if CGRectContainsPoint(playButton.frame, location) {
        let transitionToPlayScene = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
        let nextScene = PlayScene(size: self.size)
        self.view!.presentScene(nextScene, transition: transitionToPlayScene)
    }

答案 1 :(得分:0)

最终为我工作的是:

 let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
 let scene = GameScene(size: (self.scene as SKScene!).size)
 scene.scaleMode = SKSceneScaleMode.AspectFill
 (self.view as SKView!).presentScene(scene, transition: transition)