当前位置: 动力学知识库 > 问答 > 编程问答 >

swift - Removing an SKSpriteNode by name

问题描述:

I have an object called "color" and I give each spawned "color" a name such as:

"red"

"blue"

"green"

I want to know how can I remove the object "color" by its .name extension using removeFromParent() in an if statement?

Basically the spawning code produces "red", "blue" or "green" then I want to have an if statement that runs when the user uses the keyboard.

if str?.lowercaseString == color.name {

"remove the color with that name extension".removeFromParent()

basically I want to remove the color with the name extension (red, blue or green) that matched the "subject"variable in the if statement :)

网友答案:

since you already have a color object that you are comparing and you already have something to compare it to (subject), you have all the pieces you need. You just don't include the name property when removing from parent.

if subject == color.name {
    color.removeFromParent()
}
网友答案:

You can remove a SKNode, such as SKSpriteNode with the .removeFromParent() method. Make sure you're not trying to remove something else, since it won't work.

if str?.lowercaseString == color.name {
      color.removeFromParent() 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: