Swift Spritekit Adding Button Programaticly -



Swift Spritekit Adding Button Programaticly -

how programatically add together button run action when clicked? code used?

i new swift , new languages, used adding button in storyboard , running ibaction there.

adding button in spritekit , responding taps on not quite easy in uikit. need create sknode of sort draw button , check see if touches registered in scene within node's bounds.

a simple scene single reddish rectangle in center acting button this:

class buttontestscene: skscene { var button: sknode! = nil override func didmovetoview(view: skview) { // create simple reddish rectangle that's 100x44 button = skspritenode(color: skcolor.redcolor(), size: cgsize(width: 100, height: 44)) // set in center of scene button.position = cgpoint(x:cgrectgetmidx(self.frame), y:cgrectgetmidy(self.frame)); self.addchild(button) } override func touchesended(touches: nsset, withevent event: uievent) { // loop on touches in event touch: anyobject in touches { // location of touch in scene allow location = touch.locationinnode(self) // check if location of touch within button's bounds if button.containspoint(location) { println("tapped!") } } } }

if need button looks , animates ones in uikit, you'll need implement yourself; there's nil built in spritekit.

swift

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -