问题
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)
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.
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)?
回答1:
You can clone an SKSpriteNode
using this code
let sprite0 = SKSpriteNode()
let sprite1 = sprite0.copy() as! SKSpriteNode
If you have an array of sprites
let sprites: [SKSpriteNode] = [SKSpriteNode(), SKSpriteNode(), SKSpriteNode()]
you can access a value using the square brackets
let aSprite = sprites[0]
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.
来源:https://stackoverflow.com/questions/38928009/in-swift-is-it-possible-to-create-an-skspritenode-by-directly-copying-it-from-a