Swift3中的SKScene和URLQueryItems?

时间:2017-04-09 21:17:07

标签: ios swift swift3 sprite-kit imessage-extension

好的,我是URL查询的新手,也是Swift的整个方面,需要帮助。我有一个包含和SKScene的iMessage应用程序。为了让用户轮流玩游戏,我需要在1个会话中来回发送消息,我在这里学到:https://medium.com/lost-bananas/building-an-interactive-imessage-application-for-ios-10-in-swift-7da4a18bdeed

到目前为止,我的场景全部都在运行,但是我已经倾倒了Apple的冰淇淋演示,他们来回发送连续制作的冰淇淋,我不明白如何查询"我SKScene中的所有内容都可以发送现场。

我不清楚URLQueryItem的工作方式,因为文档与sprite kit场景无关。

Apple查询他们的"冰淇淋"在目前的状态如下:

    init?(queryItems: [URLQueryItem]) {
            var base: Base?
            var scoops: Scoops?
            var topping: Topping?

            for queryItem in queryItems {
                guard let value = queryItem.value else { continue }

                if let decodedPart = Base(rawValue: value), queryItem.name == Base.queryItemKey {
                    base = decodedPart
                }
                if let decodedPart = Scoops(rawValue: value), queryItem.name == Scoops.queryItemKey {
                    scoops = decodedPart
                }
                if let decodedPart = Topping(rawValue: value), queryItem.name == Topping.queryItemKey {
                    topping = decodedPart
                }
            }

            guard let decodedBase = base else { return nil }

            self.base = decodedBase
            self.scoops = scoops
            self.topping = topping
        }
    }

fileprivate func composeMessage(with iceCream: IceCream, caption: String, session: MSSession? = nil) -> MSMessage {
        var components = URLComponents()
        components.queryItems = iceCream.queryItems

        let layout = MSMessageTemplateLayout()
        layout.image = iceCream.renderSticker(opaque: true)
        layout.caption = caption

        let message = MSMessage(session: session ?? MSSession())
        message.url = components.url!
        message.layout = layout

        return message
    }
}

但我无法找到如何"查询" SKScene。我怎样才能发送"来回SKScene?这可能吗?

1 个答案:

答案 0 :(得分:1)

You do not need to send an SKScene back and forth :) What you need to do is send the information relating to your game set up - such as number of turns, or whose turn it is, or whatever, as information that can be accessed by your app at the other end to build the scene.

Without knowing more about how your scene is set up and how it interacts with the information received for the other player's session, I can't tell you a lot in terms of specifics. But, what you need to do, if you are using URLQueryItems to pass the information, simply retrieve the list of query items in your scene and set up the scene based on the received values.

If you have specific questions about how this could be done, if you either share the full project, or post the relevant bits of code as to where you send out a message from one player and how the other player receives the information and sets up the scene, I (or somebody else) should be able to help.

Also, if you look at composeMessage in the code you posted above, you will see how in that particular code example the scene/game information was being sent to the other user. At the other end of the process, the received message's URL parameter would be decomposed to get the values for the various query items and then the scene would be set up based on those values. Look at how that is done in order to figure out how your scene should be set up.