Vorkit
Vorkit

Reputation: 23

Search in CCSprite array element Objective C (cocos2d)

I'm using cocos2d and I want to see if a specific string is in the array's element. Here is the element, which is a CCSprite object:

<theSwift = 08A6EA70 | Rect = (0.00,0.00,27.00,75.00) | tag = 2 | atlasIndex = -1>

I am spawning "monsters" and one type of monsters get the tag = 1 and some get the tag = 2. Is it possible to check if the last monster spawned got the tag = 2 in the element above?

Upvotes: 2

Views: 819

Answers (4)

alpha
alpha

Reputation: 106

You can easily use this method

  [layerName getChildByTag:<(NSInteger)>]

for getting the child of any layer.

Upvotes: 0

JonasG
JonasG

Reputation: 9324

If you are talking about NSArray, then do this:

You can use containsObject in an if statement:

if ([array containsObject:@"tag = 2"]) {
    //contains tag = 2
}

It would be better to use NSDictionary though. Use setObject:forKey: in NSMutableDictionary to set the values for their keys, and to check the value do:

[dict objectForKey:@"tag"]

I would recommend using the NSDictionary method.

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

If that object is in an array, you could use an NSPredicate to find the object with a certain tag:

NSArray *myArray;

NSObject childWithTag = [[myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tag == 2"]]] lastObject];

EDIT: Since you are using cocos2d, its as simple as this:

CCSprite *spriteWithTag = (CCSprite *)[myLayer childWithTag:2];

Upvotes: 2

DarkDust
DarkDust

Reputation: 92432

Not sure I understand your question, I'll give it a shot though:

If the line above is simply an NSString and all you want is to check for tag = 2, then you'd do:

NSRange range = [theString rangeOfString:@"tag = 2"];
if (range.location != NSNotFound) {
    // theString contains "tag = 2"
}

Upvotes: 1

Related Questions