我改变了函数中的值,并想访问同一类中另一个函数的值。我的代码: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的值?希望你能帮助我。
我在错误可能的地方添加了更多的代码。 –
怎样才能获得句柄? –
初始化者绝对是要走的路。谢谢。 –