从另一个类访问int

时间:2015-03-31 05:40:58

标签: swift sprite-kit

我有另一个视图处理上次会话和排行榜的得分。如何从GameOver类的GameScene类中访问我的score int?

看起来很多,我试图通过这样做找到自己的方式:

class GameOver: SKScene {

let score = GameScene().score

init(size: CGSize, won:Bool) {

    super.init(size: size)

    backgroundColor = SKColor.whiteColor()

    var message = "\(score)"

    println(score)

    let label = SKLabelNode(fontNamed: "AvenirNextCondensed-Heavy")
    label.text = message
    label.fontSize = 40
    label.fontColor = SKColor.blackColor()
    label.position = CGPoint(x: size.width/2, y: size.height/2)
    addChild(label)
}

我认为如果我在GameScene中为我的分数创建了一个变量,那么它会起作用,但是它会使用等于0的分数变量。那么如何将分数从玩家死亡时转移到场景中以上?

class GameScene: SKScene, SKPhysicsContactDelegate {

struct PhysicsCategory {
    static let None: UInt32 = 0
    static let All: UInt32 = UInt32.max
    static let Enemy: UInt32 = 0b1
    static let Player: UInt32 = 0b10
}

let player = SKSpriteNode(imageNamed: "Player")
var playerAlive: Bool = true

var touched:Bool = false
var location = CGPointMake(0, 0)

var difficulty = 1.0

var timer: NSTimer!
var scoreTimer: NSTimer!
var lastAddedTime : CFTimeInterval = 0

var score: Int = 0
var scoreLabel: SKLabelNode!


override func didMoveToView(view: SKView) {
    backgroundColor = SKColor.whiteColor()

    player.position = CGPoint(x: size.width * 0.5, y: size.height * 0.25)
    player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width/2)
    player.physicsBody?.dynamic = true
    player.physicsBody?.categoryBitMask = PhysicsCategory.Player
    player.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
    player.physicsBody?.collisionBitMask = PhysicsCategory.None
    player.physicsBody?.usesPreciseCollisionDetection = true
    addChild(player)

    physicsWorld.gravity = CGVectorMake(0, 0)
    physicsWorld.contactDelegate = self

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

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

    scoreLabel = SKLabelNode(fontNamed: "AvenirNextCondensed-Heavy")
    scoreLabel.text = "\(score)"
    scoreLabel.fontColor = SKColor.blackColor()
    scoreLabel.fontSize = 40
    scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + self.frame.size.height * 0.4)
    addChild(scoreLabel)

}

func addScore() {
    score += 1
    println(score)
}

2 个答案:

答案 0 :(得分:0)

我认为你需要将得分变量声明为类级别变量,但在Swift中不直接支持它,所以在结构中包装score var,如下所示:

private struct ScoreStruct { 
  static var score: Int = 0 
}

class var staticScore: Int {
   get { return ScoreStruct.score }
   set { ScoreStruct.score = newValue }
}

您可以像GameScene.staticScore

一样使用它

答案 1 :(得分:0)

GameScene是一个班级。它在哪里被实例化? 在您的项目中搜索术语“presentScene:”应该有一些语言,如

let myGameScene = GameScene()
skView.presentScene(myGameScene)

此外,项目中必须有一个包含如下代码的地方。添加带注释的行

let myGameOver = GameOverScene()
if(conditionIsMet)
{
    myGameOver.passScore(myGameScene.score) //add this line
    skView.presentScene(myGameOver)
}