快速地,是否可以通过直接从数组中复制来创建SKSpriteNode? [英] In swift, is it possible to create an SKSpriteNode by directly copying it from an array?
问题描述
目前,我在Swift中的一个项目遇到了一些麻烦。我想知道是否可以创建一个像这样的全新sprite节点:
I'm having a little bit of trouble with one of my projects in Swift at the moment. I want to know if you can create a brand new sprite node like this:
var spriteNode : SKSpriteNode = nodes(3)
其中节点是SKSpriteNodes的数组,并在索引号3处有一个对象。以前,我曾在Java,而且我知道,如果要执行类似的操作,它只会传递一个sprite节点变量的引用,而不是创建一个新的sprite节点,但是我不确定这是否会很快发生。
where nodes is an array of SKSpriteNodes and has an object at index number 3. Previously, I have worked in Java, and I know that if you were to do something like this, it would simply pass a reference the sprite node variable rather than creating a whole new sprite node, but I am not sure if this is what happens in swift.
如果您不能通过这种方式创建新的Sprite节点,那么最好的制作方法是(在Java中,您只需创建一个新的Sprite节点,然后逐字复制所有
If you cannot create a new sprite node this way, what is the best way to make it (In Java you would simply create a new sprite node and literally copy all of its values)?
推荐答案
您可以使用以下代码克隆 SKSpriteNode
You can clone an SKSpriteNode
using this code
let sprite0 = SKSpriteNode()
let sprite1 = sprite0.copy() as! SKSpriteNode
如果您有一个精灵数组
let sprites: [SKSpriteNode] = [SKSpriteNode(), SKSpriteNode(), SKSpriteNode()]
您可以使用方括号
let aSprite = sprites[0]
无论如何,在使用其他环境的模式/技术之前,我真的建议您阅读 Sprite Kit编程指南。如果我们按预期的方式使用工具,事情通常会变得更容易。
Anyway before using patterns/techniques of other environments I really suggest you to read the Sprite Kit Programming Guide. Often things get easier if we use a tool the way it is intended to be.
这篇关于快速地,是否可以通过直接从数组中复制来创建SKSpriteNode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!