In swift, is it possible to create an SKSpriteNode by directly copying it from an array?

℡╲_俬逩灬. 提交于 2019-12-13 15:22:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!