调用类函数

时间:2015-01-13 02:53:33

标签: ios objective-c xcode class swift

我有我的主GameScene.swift文件和facebookLogin.swift文件。

我正在从facebookLogin.Swift文件中创建一个SKSpriteNode'FBButton',并通过self.addChild(FBButton)将其添加到GameScene中。该类有两个函数touchBegan()和facebookSignIn()。

当用户触摸精灵'FBButton'时,touchBegan()会确认触摸按钮并调用facebookSignIn()。然后,此功能连接到用户Facebook。这里的一切都很好。

然后我想让这个函数在GameScene.swift中调用ShowButtons()函数。 我调用GameScene()。ShowButtons()删除FBLogin SpriteNode并添加2个SpriteNode并将用户的Facebook数据下载到NSDictionary中。

数据从用户Facebook获得,但是按钮不生成FBButton不会从父母中删除。

请帮助我,告诉我哪里出错了。

class facebookLogin: SKNode {

var signedIn = false


override init() {

    fbButton = SKSpriteNode(imageNamed: "login_with_facebook")
    fbButton.xScale = 1.0
    fbButton.yScale = 1.0
    super.init()

    addChild(fbButton)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        let touch = touches.anyObject() as UITouch
        let point = touch.locationInNode(self)

        if fbButton.containsPoint(point) {
            facebookSignin()
        }
}

func facebookSignin()  {

    var permissions = ["user_about_me",
        "user_birthday"];

    PFFacebookUtils.logInWithPermissions(permissions, {
        (user: PFUser!, error: NSError!) -> Void in
        if user == nil {
            NSLog("Uh oh. The user cancelled the Facebook login.")
            self.signedIn = false
        } else if user.isNew {
            NSLog("User signed up and logged in through Facebook!")
            self.signedIn = true
        } else {
            NSLog("User logged in through Facebook!")

            self.signedIn = true

            GameScene().ShowButtons()


        }
    })

}
}

以下是show buttons脚本

class GameScene: SKScene {


let fbButton = facebookLogin()
var btnHostGame: SKSpriteNode = SKSpriteNode(imageNamed: "HostGame")
var btnJoinGame: SKSpriteNode = SKSpriteNode(imageNamed: "JoinGame")    



override func didMoveToView(view: SKView) {

    userInteractionEnabled=true
    self.backgroundColor = SKColor.whiteColor()

    self.fbButton.userInteractionEnabled = true
    self.fbButton.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
    addChild(self.fbButton)

}



func showButtons() {

    self.btnHostGame.name = "btnHostGame"
    self.btnHostGame.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.6)
    addChild(self.btnHostGame)

    self.btnJoinGame.name = "btnJoinGame"
    self.btnJoinGame.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.40)
    addChild(self.btnJoinGame)

    fbButton.RemoveFromParent()

    }
}

除非它回叫GameScene,否则一切都很完美。按钮不会出现,FBButton也不会被删除。

任何人都知道解决方案吗?

干杯 Ryann

1 个答案:

答案 0 :(得分:3)

GameScene().ShowButtons()中,您实际上正在创建GameScene类的新实例。此实例不是添加到SKView的当前实例。这就是为什么它不起作用。

而是在GameScene节点中为facebookLogin声明弱属性

class faceBookLogin : SKNode
{
    weak var gameScene : GameScene? = nil
}

并从didMoveToView功能中添加gameScene。

override func didMoveToView(view: SKView) {

    userInteractionEnabled=true
    self.backgroundColor = SKColor.whiteColor()

    self.fbButton.userInteractionEnabled = true
    self.fbButton.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
    addChild(self.fbButton)
    self.fbButton.gameScene = self
}

通过这种方式,您可以从GameScene

致电您所呈现的func facebookSignin()
func facebookSignin()  {
   // Your code.
   self.gameScene.showButtons()
}