Bipul Chandra Dev Nath
Bipul Chandra Dev Nath

Reputation: 431

Select sprite by Touch better Method

From the begging of my objective c and cocos2d coding i have been using the sequential search method to select an sprite. Each time I detect a touch I search each object from my array list and then identify if the touch is on the sprite area if I gets the object then I select that sprite. This is the method Known by me.

the problem with the method is that if the object list become huge then it may take time and even when we are working with one array then it locks the array we con do nothing with that array.

Do anyone know better method except this one?

Upvotes: 0

Views: 104

Answers (2)

giorashc
giorashc

Reputation: 13713

You can use CCMenuItemImage instead of CCSprite. You can choose a selector once the item is touched and of course can apply an image like a regular sprite with the following method :

itemFromNormalImage:selectedImage:target:selector:

You will also have to create a menu to contain this menu items, otherwise touching them won't work. Just create an instance of CCMenu and add the items to this menu then add the menu to your layer.

About effiency, well you will have to make some benchmarking with both implementations as this one is more convenient but not necessarily faster

Upvotes: 2

Guru
Guru

Reputation: 22042

Locks array? then just use one more pointer for object.

 NSArray *array = [NSArray arrayWithArray:mSpriteArray];

    for(CCSprite *sprite in array)
    {
        if(CGRectContainsPoint([sprite boundingBox], touchPoint))
        {
          //touched...
        }
    }

Upvotes: 0

Related Questions