2015-11-08 86 views
0

我改变了函数中的值,并想访问同一类中另一个函数的值。我的代码:Swift:变量的公共值仍然是本地的?

class person{ 

var fieldsWidth = CGFloat() 
var xPos = CGFloat() 
var yPos = CGFloat() 
var player = SKSpriteNode() 

func addPlayer(gameScene: GameScene, x: CGFloat, y: CGFloat){ 
    fieldsWidth = gameScene.screenWidth/CGFloat(hintergrund().fieldsX) 
    player = SKSpriteNode(imageNamed: "player") 
    player.size = CGSize(width: fieldsWidth, height: fieldsWidth) 
    player.position = CGPoint(x: x, y: y) 
    gameScene.addChild(player) 
    xPos = x 
} 

func move(gameScene: GameScene, point: CGPoint, fWidth: CGFloat){ 
    print(xPos) 
} 

}

和GameScene:

class GameScene: SKScene { 

var screenWidth = CGFloat() 
var screenHeight = CGFloat() 
var touchLocation = CGFloat() 
var fieldsWidth = CGFloat() 
let player = person() 

override func didMoveToView(view: SKView) { 
    screenWidth = self.size.width 
    screenHeight = self.size.height 
    fieldsWidth = screenWidth/CGFloat(hintergrund().fieldsX) 
    person().addPlayer(self, x: fieldsWidth/2, y: fieldsWidth/2) 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    /* Called when a touch begins */ 
    let touch = touches.first 
    var point = CGPoint() 
    point = touch!.locationInNode(self) 
    player.move(self, point: point, fWidth: fieldsWidth) 
} 

}

不知何故XPOS的值是addPlayer只是局部的。我如何正确访问xPos的值?希望你能帮助我。

回答

1

我觉得CGFloat的是值0.0让你的错误的最可能的原因是你调用函数2在函数之前你的代码。错误在别的地方。

编辑自从你发布代码。我会尝试这个(建立一个新的球员,并保留它 - 我不是一个精灵套件专家):

class GameScene: SKScene { 

    var screenWidth = CGFloat() 
    var screenHeight = CGFloat() 
    var touchLocation = CGFloat() 
    var fieldsWidth = CGFloat() 
    var player = person() // SEE HERE - Variable, so that you can reassign it 

    override func didMoveToView(view: SKView) { 
     screenWidth = self.size.width 
     screenHeight = self.size.height 
     fieldsWidth = screenWidth/CGFloat(hintergrund().fieldsX) 
     player = person().addPlayer(self, x: fieldsWidth/2, y: fieldsWidth/2) // SEE HERE - Create new player and retain it into your scene 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 
     let touch = touches.first 
     var point = CGPoint() 
     point = touch!.locationInNode(self) 
     player.move(self, point: point, fWidth: fieldsWidth) 
    } 
} 

PS:以上可能会奏效,但说真的,从我的理解,你需要创建的每个玩家时间didMoveToView被调用。如果是这样的话,那么我将为player类做一个指定的初始化程序,而不是做这个人为的addPlayer函数。类似于(未经测试):

func init(scene: SKScene, x: CGFLoat, y: CGFloat) { 
// ETC... 
} 
+0

我在错误可能的地方添加了更多的代码。 –

+0

怎样才能获得句柄? –

+0

初始化者绝对是要走的路。谢谢。 –

2

运行完全按照预期下面的代码,我在的XCode 7.1版(7B91b)操场

class person{ 

    var yPos = CGFloat() 

    func function1(y: CGFloat){ 
     yPos = y 
     print(yPos) //Value of yPos is 31.25; correct! 
    } 

    func function2(){ 
     print(yPos) //Value of yPos is 0.0; wrong!?? 
    } 
} 

let foo = person() 
foo.function1(31.25) 
foo.function2() //Outputs 31.25 
+0

这是我所期待的。函数中有更多的代码,并且我使用的是SpriteKit,这是否有所不同?我将代码缩减为必要的部分。 –

+0

现在我添加了更多的错误代码。 –