Alan Guilfoyle
Alan Guilfoyle

Reputation: 381

Connect a UIButton in GameViewController to work in SKScene (Swift)

I was able to add a button on the screen in my GameScene.swift (which holds my SKScene) by just hard coding:

SWIFT CODE:

    rightControlPad = UIButton(frame: CGRectMake( 4, 580, 200, 150))
    rightControlPad.backgroundColor = UIColor.greenColor()
    rightControlPad.setTitle("Jump", forState: UIControlState.Normal)
    rightControlPad.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    rightControlPad.tag = 22;
    self.view?.addSubview( rightControlPad )

However because I don't know how to add constraints to that button like I would in story board this option isn't working for me.

So, I wanted to create a button in storyboard and then link it in my GameViewController and access the button (aka: the simulated effect of clicking the button which would make the sprite jump) in my GameScene. I added the button in the GameViewController but I'm not able to do anything with the sprite in GameScene. I have even tried testing it by passing a function call in the GameViewController that calls the GameScene and it works by printing out a statement but it skips over the code that causes the sprite to move. For Instance: GAMEVIEWCONTROLLER: **... var gameScene = GameScene()

@IBAction func likedThis(sender: UIButton) 
{
    gameScene.sendToButton( sender as UIButton )
}**

GAMESCENE:
**...
func sendToButton( sender: UIButton)
{
   println( "Test")
   self.sprite.physicsBody?.applyImpulse( CGVectorMake( 0, 35 ))
}**

The output will just be: Test and the sprite will not move. However if I use the method above where I hard code a button on the screen and click it and that applyImpulse was in the function it'll do it. It's the strangest thing and something that I would love guidance on.

So my question is: Could someone explain how to hard code constraints to hard coded buttons OR Show me how to access a button in GameViewController that I put there via storyboard and use it in GameScene.

Thanks for all the help!

Upvotes: 2

Views: 1191

Answers (1)

txaidw
txaidw

Reputation: 479

You shouldn’t use UIButtons on your SpriteKit Game, manly because you would have to add it to the SKView and remove when the button goes of screen.

If you want to build a button or a switch for generic use, you should try this control I made, the use its pretty straight forward: You just initialize the type of Button/Switch you want (ColoredSprite, Textured or TextOnly)

let control = TWButton(normalColor: SKColor.blueColor(), highlightedColor: SKColor.redColor(), size: CGSize(width: 160, height: 80))

And after initialize you add a closure to it (like addTargetForSelector on UIButton)

 control.addClosureFor(.TouchUpInside, target: self, closure: { (scene, sender) -> () in
            scene.testProperty = "Changed Property"
        })
    }

That’s it! There’s more info on the readme section on the GitHub page: https://github.com/txaidw/TWControls

But if you want to implement in a specific node here’s how I did:

internal override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchPoint = touch.locationInNode(self.parent)

    if self.containsPoint(touchPoint) {
        self.touchLocationLast = touchPoint
        touchDown()
    }
}

internal override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchPoint = touch.locationInNode(self.parent)
    self.touchLocationLast = touchPoint
}


internal override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchPoint = touch.locationInNode(self.parent)

        if let lastPoint = self.touchLocationLast where self.containsPoint(lastPoint) {
            // Ended inside
            touchUpInside()
        }
        else {
            // Ended outside
            touchUpOutside()
        }
}

Upvotes: 1

Related Questions