将\(使用)SKView创建为\(在)工厂\(静态类) [英] Create \(Use) SKView as \(in a) factory \(static class)

查看:101
本文介绍了将\(使用)SKView创建为\(在)工厂\(静态类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个可以用作工厂的SKView来制作SKShapeNodes并将其渲染"为纹理.

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

更新

在Google上花费了更多时间之后,我尝试搜索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.

一个使用静态类的概念,另一个使用单例.这些都不是比这更好".也不是打开更好的Pandora's Box,等等.还有其他线程讨论这些细节.相反,它们只是使您前进的几种方法.

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

这篇关于将\(使用)SKView创建为\(在)工厂\(静态类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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