0

嗨,我正在尝试快速在 SKVideoNode 上播放视频这是我的代码,但如果我点击 VideoSprite 它只播放一次它不播放但剂量打印“我们点击了视频”我已经搜索但可以' t 似乎找到答案感谢寻找

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()
            }

        }
    }

}
4

1 回答 1

2

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

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
                     }

            }
        }
    }



}
于 2015-05-31T18:01:43.060 回答