创建 (Use) SKView as (in a) factory (static class) [英] Create (Use) SKView as (in a) factory (static class)

查看:15
本文介绍了创建 (Use) SKView as (in a) factory (static class)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个 SKView,我可以将其用作工厂来制作 SKShapeNode 并将它们渲染"为纹理.

I want to make an SKView I can use as a factory to make SKShapeNodes and "render" them to textures.

但我找不到如何初始化这样的东西,而且我一点运气都没有.

But I can't find how I would initialise such a thing, and am having no luck, at all.

如何为此目的制作独立的 SKView?

How do I make a standalone SKView for this purpose?

或者有没有更好的方法来避免使用游戏场景?

Or is there a better way to do this that avoids using the gamescene?

这是我在制造工厂时所做的徒劳,这抱怨纹理(来自:)不明确.我不知道那是什么意思.

Here's my FUTILE Effort at making a factory, this complains that texture(from: ) is ambiguous. I have no idea what that means.

import SpriteKit

class Make: SKView{

static func circle() -> SKSpriteNode {
    let myShapeNode = SKShapeNode(circleOfRadius: 100)
    myShapeNode.fillColor = SKColor.lightGray
    myShapeNode.strokeColor = SKColor.gray
    let tex = texture(from: myShapeNode)
    return SKSpriteNode(texture: tex)
    }

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }   
}

更新

在谷歌上浪费了更多时间后,我尝试搜索 UIView 的初始化,并找到并添加了这段代码,它初始化为一个看似虚构的框架......但它有效!不知道为什么……但是我不能把它作为工厂方法,只能作为实例方法,这样:

Update

After more futile time on google, I tried searching for initialisation of a UIView, and found and added this piece of code, that initialises to a frame that seems imaginary... but it works! I don't know why... but I can't use it as a factory method, only as an instance method, this way:

import Foundation
import SpriteKit

class Make: SKView{

    // added randomly found UIView initialisation "code"...
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    func circle() -> SKSpriteNode {
        let myShapeNode = SKShapeNode(circleOfRadius: 100)
        myShapeNode.fillColor = SKColor.lightGray
        myShapeNode.strokeColor = SKColor.gray
        let tex = texture(from: myShapeNode)
        return SKSpriteNode(texture: tex)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

推荐答案

不太清楚你所说的我将如何初始化这样的东西"是什么意思.如何取决于技术和工厂的整体工作.一些工厂不需要外部输入进行初始化,而另一些工厂则需要.这是几个非常粗略的例子.这些可能过于简单,根本没有经过测试.

It's not quite clear what you mean by "how I would initialise such a thing". How is dependent upon technique and what the factory does overall. Some factories do not need external input for initialization while others do. Here is are a couple of really crude examples. These are perhaps overly simplistic and have not been tested at all.

一个使用静态类的概念,而另一个是单例.这些都不是这比那更好".也不是打开潘多拉的盒子哪个更好,等等.还有其他线程会讨论这些细节.相反,它们只是让您前进的几种方法.

One uses the concept of a static class whilst the other is a singleton. None of this is to serve as "this is better than that". Nor is it to open Pandora's Box on which is better, etc. There are other threads which go over those details. Rather they are just a couple of approaches to get you going.

这是一个静态类版本

// static class version
class SpriteFactory1 {
    private static let view:SKView = SKView()

    static func spriteFromShape(shape:SKShapeNode) -> SKSpriteNode? {
        guard let tex = view.texture(from:shape) else {
            return nil
        }

        return SKSpriteNode(texture:tex)
    }
}

这是一个单例版本.从上面添加您的圈子代码...

Here is a singleton version. Adding your circle code from above ...

// singleton version
class SpriteFactory2 {
    static let sharedInstance = SpriteFactory2()

    private let view:SKView = SKView()

    func spriteFromShape(shape:SKShapeNode) -> SKSpriteNode? {
        guard let tex = view.texture(from:shape) else {
            return nil
        }

        return SKSpriteNode(texture:tex)
    }

    func circle() -> SKSpriteNode {
        let myShapeNode = SKShapeNode(circleOfRadius: 100)
        myShapeNode.fillColor = SKColor.lightGray
        myShapeNode.strokeColor = SKColor.gray
        let tex = texture(from: myShapeNode)
        return SKSpriteNode(texture: tex)
    }
}

您还会注意到代码或多或少是相同的.但是在静态的情况下,类变量也必须是静态的.

You'll also note the code is more or less the same. However in the case of the static, the class variables must be static as well.

用法如下所示:

// Test shape
let shape = SKShapeNode()
// Pretend shape is constructed as desired

let sprite1 = SpriteFactory1.spriteFromShape(shape: shape)    
let sprite2 = SpriteFactory2.sharedInstance.spriteFromShape(shape: shape)
let circle = SpriteFactory2.sharedInstance.circle()

这篇关于创建 (Use) SKView as (in a) factory (static class)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆