sprite kit - Swift SpriteKit SKVideoNode only plays once -
hi i'm trying play video on skvideonode in swift code works plays once if click on videosprite doesn't play dose print "we clicked video" i've searched can't seem find answer looking
import spritekit import avfoundation class gamescene: skscene { var videosprite = skvideonode() override func didmovetoview(view: skview) { loadvideo("9.mp4") } func loadvideo(filetoplay:string) { videosprite = skvideonode (videofilenamed:filetoplay) videosprite.position = cgpointmake(size.width/2, size.height/2); videosprite.name = "videosprite" videosprite.zposition = 2 addchild(videosprite) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { touch in (touches as! set<uitouch>) { var touch: uitouch = touches.first as! uitouch var location = touch.locationinnode(self) var node = self.nodeatpoint(location) if (node.name == "videosprite") { println("we clicked video") videosprite.play() } } } }
hi think i've cracked add videosknode on top , rename user can't click on original video after delay remove delay needs time video runs use cool function delay found on internet can't remember ever wrote hope might else tried paused bool in update function no luck?
import spritekit import avfoundation import avkit class gamescene: skscene { var videosprite = skvideonode()// emptey videonode func delay(delay:double, closure:()->())// function delay { dispatch_after( dispatch_time( dispatch_time_now, int64(delay * double(nsec_per_sec)) ), dispatch_get_main_queue(), closure) } override func didmovetoview(view: skview) { loadvideo("9.mp4") // run function load video videosprite.pause()// pauses video ready play } func loadvideo(filetoplay:string) { videosprite = skvideonode (videofilenamed:filetoplay)// fill spritenode video file videosprite.position = cgpointmake(size.width/2, size.height/2); // set size videosprite.name = "videosprite" // give name videosprite.zposition = 1 // set z position videosprite.play()// play video addchild(videosprite)// add video node sceene } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { touch in (touches as! set<uitouch>) { var touch: uitouch = touches.first as! uitouch var location = touch.locationinnode(self) var node = self.nodeatpoint(location) println(node.name) if (node.name == "videosprite")// click on video { loadvideo("9.mp4") /// load video ontop videosprite.name = "temp" //name temp user cant click add more videonodes delay(1.8 )// wait time { self.videosprite.removefromparent()// remove } } } } }
Comments
Post a Comment