1

So it's a very simple question: I have an SKSpriteNode fullPiece and when it hits a point I want to copy it and transfer the copy to a different parent. So what i'm doing is this

var tempPiece = fullPiece 
fullPiece.removeFromParent()
collectionOfParts.addChild(tempPiece)

Im copying the object, then removing the old one, then adding the copy. However, later on I remove the fullPiece again and the copy, or whatever is there disappears. How do I make this work properly?

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
Sam Hoffmann
  • 310
  • 2
  • 15
  • I know this is a year later, but I think the line `var tempPiece = fullPiece` does not copy `fullPiece` (as classes are reference types). Instead you are just giving `fullPiece` another name to be called by: `tempPiece`. To copy an `SKSpriteNode` you have to use the `copy()` method. [You can find an example here](http://stackoverflow.com/questions/38928009/in-swift-is-it-possible-to-create-an-skspritenode-by-directly-copying-it-from-a). You must be careful though, as there is a bug with copying `SKSpriteNode`s. The `physicsBody?` property doesn't seem to be copied. – mogelbuster Mar 28 '17 at 04:28

1 Answers1

0

This could be enough:

var tempPiece: SKSpriteNode! = SKSpriteNode()
tempPiece = fullPiece
tempPiece.name = "tempPiece"
collectionOfParts.addChild(tempPiece)
fullPiece.removeFromParent()
...
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • the problem with your code is that it says i can't add fullPiece to two parents. So i switch the second to last with the last but it is the same object. I need it to be a different object that looks exactly the same. – Sam Hoffmann May 23 '16 at 17:34
  • you have everything that's a problem. the other code isn't related to the problem. There has to be some way to copy and make them different. – Sam Hoffmann May 23 '16 at 17:35
  • I've wrote thousand of SKSpriteNode around my games without these problems, even using shared istance.. I think you must recheck your code, the copy function of two SKSpriteNode is the equal sign, there isnt nothing to add. – Alessandro Ornano May 23 '16 at 17:38
  • so i think i know how to fix it. ill try to write the important lines of the size, position, children... and just say one is equal to the other and not the whole object. – Sam Hoffmann May 23 '16 at 17:39
  • In my answer I've added "name". This parameter help you to search on another class his childs to find your SKSpriteNode recently added. If your init methods are correct you shouldn't have any problem. – Alessandro Ornano May 23 '16 at 17:41
  • init methods? what does that mean. i just make skspritenode objects – Sam Hoffmann May 23 '16 at 17:42
  • I meants init class method, if you have some code than cleans object in the inits methods this could be a problem: i dont know your project, how you have customize inits.. – Alessandro Ornano May 23 '16 at 17:44
  • i don't have any custom inits. I don't init anything -- to complicated. – Sam Hoffmann May 23 '16 at 17:46
  • okay, so it kinda worked. It made a new object and it looks the same and it removes it but after i get the problem where an object has a parent already so ill fix that one up. Thanks for the help! – Sam Hoffmann May 23 '16 at 17:51