IMustCode
IMustCode

Reputation: 109

how to determine if a sprite is in a layer

Sorry if this sounds quite trivial. I am just not getting it. How can one determine if a particular sprite is already in a layer? Basically, I need to check this before determining whether to add it to the layer or not.

Upvotes: 3

Views: 723

Answers (2)

Bivis
Bivis

Reputation: 1408

There's so many ways:

1) try to get child

if (![layer getChild:sprite]) {
   // Your code
}

2) try to get child by tag

if (![layer getChildByTag:spriteTag]) {
   // Your code
}

3) Check if sprite is on children array (like @oopology answer)

if ([layer.children indexOfObject:sprite] == NSNotFound) {
   // Your code
}

Upvotes: 1

oopology
oopology

Reputation: 1072

if ( [ myNode.children indexOfObject:sprite ] == NSNotFound ) {

     // you can add the code here

}

Upvotes: 6

Related Questions