在cocos2d游戏开发中,CGRectContainsPoint方法经常用来检测是否触碰到CCSprite。如何以方便的方式获取精灵矩形?
我用代码fllow得到一个精灵的(这在CCNode)rect属性
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CCLOG(@"ccTouchEnded"); CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; CCLOG(@"location.x:%f, y:%f", location.x, location.y); CGRect rect; rect = CGRectMake(self.firstCard.face.position.x-(self.firstCard.face.contentSize.width/2), self.firstCard.face.position.y-(self.firstCard.face.contentSize.height/2), self.firstCard.face.contentSize.width, self.firstCard.face.contentSize.height); if (CGRectContainsPoint(rect, location)) { CCLOG(@"first card touched"); [firstCard open]; } rect = CGRectMake(self.secondCard.face.position.x-(self.secondCard.face.contentSize.width/2), self.secondCard.face.position.y-(self.secondCard.face.contentSize.height/2), self.secondCard.face.contentSize.width, self.secondCard.face.contentSize.height); if (CGRectContainsPoint(rect, location)) { CCLOG(@"second card touched"); [secondCard open]; } }
我想知道如果有一种方便的方式获得CCSprite的矩形简单?
你可以通过精灵值为[sprite_nm boundingBox的]。方法。我认为它会对你有用。它返回整个Rect。 – Marine 2012-01-11 05:51:42