Swift SpriteKit SKVideoNode只播放一次

时间:2015-05-31 09:27:45

标签: swift sprite-kit skvideonode

您好我正试图在swift中播放SKVideoNode上的视频 这是我的代码可以工作,但它只播放一次,如果我点击VideoSprite它没有播放但是打印#34;我们点击了视频"我已经搜索过了,但感谢你找不到答案

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)
    {
        for 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 the video")
                VideoSprite.play()
            }

        }
    }

}

1 个答案:

答案 0 :(得分:2)

嗨,我想我已经破解了它 我在顶部添加另一个videoSKnode并重命名,以便用户无法点击原始视频然后在延迟后我将其删除 这种延迟需要是视频运行的时间 我使用一个很酷的功能延迟我在互联网上找不到记得从哪里,所以感谢谁写过它  我希望这可能会帮助别人 我在更新功能中尝试了暂停的bool,但没有运气?

import SpriteKit
import AVFoundation
import AVKit
class GameScene: SKScene
{

 var VideoSprite = SKVideoNode()// emptey Videonode

    func delay(delay:Double, closure:()->())// function for 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 to load video
    VideoSprite.pause()// pauses video ready to play
    }

    func LoadVideo(FileToPlay:String)
    {

        VideoSprite =  SKVideoNode (videoFileNamed:FileToPlay)// fill spritenode with video file
        VideoSprite.position = CGPointMake(size.width/2, size.height/2);  // set size
        VideoSprite.name = "VideoSprite" // give it a name
        VideoSprite.zPosition = 1 // set its z position
        VideoSprite.play()// play video
        addChild(VideoSprite)// add video node to the sceene
    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        for 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")// we click on video
            {

                LoadVideo("9.mp4") /// load another video ontop
                VideoSprite.name = "temp" //name it temp so user cant click to add more videonodes
                delay(1.8 )// wait for time
                    {
                     self.VideoSprite.removeFromParent()// then remove
                     }

            }
        }
    }



}