Issue with SKVideoNode in SpriteKit, Simulator just shows Grey Screen

℡╲_俬逩灬. 提交于 2021-02-07 20:43:18

问题


Trying to run a 'Cut Scense Video' before entering into my game build. The game works great. I have created a separate scene, which I named "StartScene" to play my cut scene. The build succeeds, but when it gets to the cut scene part on the simulator I get a blank grey screen. I have searched online and get a lot of resizing issues with the video, however Apple Developer says this:

When a video node is created, its size property is initialized to the base size of the video content, but you can change it if you want. The video content is automatically stretched to the new size.

Example code I copied from Apple Developer:

let sample = SKVideoNode(fileNamed: "sample.mov")
sample.position = CGPoint(x: frame.midX,
                      y: frame.midY)
addChild(sample)
sample.play()

Here is my code in my StartScene.swift for playing the cut scene:

import SpriteKit
import GameplayKit

class StartScene: SKScene {
    override func sceneDidLoad() {
        let openingVideo = SKVideoNode(fileNamed: "MyCutScene.mp4")
        openingVideo.position = CGPoint(x: frame.midX, y: frame.midY)
        addChild(openingVideo)
        openingVideo.play()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let gameSceneTemp = GameScene(fileNamed: "GameScene")
        self.scene?.view?.presentScene(gameSceneTemp!, transition: SKTransition.doorsCloseHorizontal(withDuration: 1.0))
    }
}

Maybe its a newbie error in just formatting the scene wrong in general. I thought it would be pretty straight forward. The Video is saved in the App bundle in Assets, and yes I made sure the name matched what I put in the code string. The last bit of code is just my transition to the GameScene with the touchesBegan function.


回答1:


I asked an Apple Developer friend and he figured it out for me. Here are the steps to get the SKVideoNode to play properly: Note my Scene for this build is called StartScene.

  1. Set the class in the StartScene.sks file to StartScene My issue here is I did not change the "Custom Class" to my scene name. Make sure you do, newbie mistake by me for sure.

  2. Add the mp4 to the “Copy Bundle Resources” Build Phase (not sure why this is needed, I don’t think it is in a typical app, but maybe games are different) Make sure the video file is not only in the App files on the left, but that it also gets copied into the Build Phases---> Copy Bundle Resources area.

  3. At this point it will play, but you will only hear the sound of the video. To see it you need to set the size. So add a line to the didMove function in the StartScene class to set the size.

    override func didMove(to view: SKView) {
    let openingVideo = SKVideoNode(fileNamed: "UncleClaryWolfIntro.mp4")
    openingVideo.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    openingVideo.position = CGPoint(x: 0, y: 0)
    openingVideo.size = frame.size
    addChild(openingVideo)
    openingVideo.play()
    }
    

All about making the size right for the screen playing it.

  1. The video should now show, but the aspect ratio will be off. So in the GameViewController change this line

    scene.scaleMode = .aspectFill
    

    To this

    scene.scaleMode = .fill
    

and Voila! The video should play, and mine does play now with this code, and steps completed. Hope this helps you, like it did me. Happy Coding!



来源:https://stackoverflow.com/questions/51107193/issue-with-skvideonode-in-spritekit-simulator-just-shows-grey-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!