Lukas Köhl
Lukas Köhl

Reputation: 1579

Swift: Value Of A Variable Public But Still Local?

I change the value in a function and want to access the value in another function within the same class. My code:

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)
}

}

and the 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)
}

}

Somehow the value of xPos is just local in addPlayer. How can I access the value from xPos correctly? Hopefully you can help me.

Upvotes: 0

Views: 57

Answers (2)

Eppilo
Eppilo

Reputation: 773

I think CGFloat is 0.0 by default so the most likely cause of your error is that you call function 2 before function one in your code. The error is somewhere else.

EDIT since you posted code. I would try this (create a new player and retain it -- I am not a sprite kit expert):

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: The above might work but really, from what I understand you need to create a player each time didMoveToView is called. If that is the case, then instead of doing this contrived addPlayer funciton, I would make a designated initialiser for the player class. Something like (untested):

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

Upvotes: 1

Nirma
Nirma

Reputation: 5790

The following code runs exactly as expected for me in a XCode Version 7.1 (7B91b) playground

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

Upvotes: 2

Related Questions