简体   繁体   English

如何检测特定NSMutableArray中的对象触摸?

[英]How can I detect a touch of an object in a specific NSMutableArray?

I have an NSMutableArray filled with different sprites. 我有一个充满不同精灵的NSMutableArray。 These sprites are all on the screen. 这些精灵都在屏幕上。 How can I detect if a touch is landing on one of these sprites, and then do something if a touch on the sprite has occurred? 我如何检测触摸是否在这些小精灵之一上着陆,如果在小精灵上发生了触摸,该怎么办?

This is what I have now, 这就是我现在所拥有的

CGPoint touchLocation = [touch locationInNode:_physicsNode];

if(CGRectContainsPoint((starInArray.boundingBox), touchLocation))  {

Instead of (starInArray.boundingBox) , I want to be able to say something like (anyObjectInMyArray.boundingBox) . 而不是(starInArray.boundingBox) ,我希望能够说类似(anyObjectInMyArray.boundingBox)

Any way to go about this? 有什么办法吗?

Thanks! 谢谢!

Something along the lines of this should work. 与此类似的东西应该起作用。

  - (BOOL) ccTouchBegan:(UITouch *)touches withEvent:(UIEvent *)event 
{

CGPoint touchLocation = [self convertTouchToNodeSpace:touches];    
        for (CCSprite *star in starInArray)
            {
                if (CGRectContainsPoint(CGRectMake(star.position.x - star.contentSize.width/2,
                                                   star.position.y - star.contentSize.height/2, star.contentSize.width, star.contentSize.height), touchLocation))
                {
                    //Do Something
                    break;
                }
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM