ios - Can't remove tapped spriteNode using removeFromParent : Swift -


i have for-loop instantiates 5 sprites such

for enemy in 1...5 {     negativeoncoming = skspritenode(imagenamed: "enemy3")     negativeoncoming.physicsbody = skphysicsbody(circleofradius: negativeoncoming.frame.size.width/2)     negativeoncoming.physicsbody?.dynamic = true     negativeoncoming.physicsbody?.categorybitmask = physicscategory.negativeoncoming     negativeoncoming.physicsbody?.fieldbitmask = physicscategory.negativeoncoming     negativeoncoming.physicsbody?.contacttestbitmask = physicscategory.maincenternode | physicscategory.positiveoncoming     negativeoncoming.physicsbody?.node?.name = "negativeoncoming" self.addchild(negativeoncoming) 

} declared variable globally

var negativeoncoming : skspritenode! 

now want able remove sprite thats been tapped on once tapped. attempt did

 func removenegativeoncoming(negativeoncomingr:skspritenode){     println("tapped")     childnodewithname("negativeoncoming")?.removefromparent()        }   override func touchesbegan(touches: nsset, withevent event: uievent){     var touch = touches.anyobject() uitouch     var touchposition = touch.locationinnode(self)     var enemynodeposition = negativeoncoming.childnodewithname("negativeoncoming")?.position     var enemynodeframe = negativeoncoming.frame    func removeenemynode (uitouch) -> skspritenode {         if (enemynodeframe.contains(touchposition) ) {             removenegativeoncoming(negativeoncoming)         }         return negativeoncoming     }     mainspritemovement(touch)  } 

now function in fact remove sprite , not remove sprite tapped on 1 of 5 sprites @ random. question how can have sprite tapped on me removed , not , random 1 of 5. question asked , understandable. thank all

you creating 5 enemies, setting negativeoncoming each time, variable end holding reference last node created.

this isn't cause of problem, however. using childnodewithname find node remove - of enemy nodes have same name, 1 @ random.

you can use nodeatpoint determine node tapped -

override func touchesbegan(touches: nsset, withevent event: uievent){     var touch = touches.anyobject() uitouch     var touchposition = touch.locationinnode(self)     var touchednode = self.nodeatpoint(touchposition)     if (touchednode.name == "negativeoncoming") {         touchednode.removefromparent()     }     mainspritemovement(touch)  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -