今天又做了一个小功能,就是触屏的时候,当手指碰到精灵时,精灵能执行我们想要的效果。
下面看代码片段吧
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
//得到触摸屏上的点
CGPoint location = [touch locationInView:[touch view]];
//定义一个点,并把location赋值给point,为了在下面的代码执行中,不改变locaion的值
CGPoint point;
location = [[CCDirector sharedDirector] convertToGL:location];
point = location;
//遍历精灵数组中的精灵
for(CCSprite *ballon in ballonarray)
{
//得到精灵的坐标范围,使手指碰到精灵的任意位置都能响应我们想要的效果
CGRect rect = [ballon textureRect];
rect = CGRectMake(0, 0, ballon.contentSize.width, ballon.contentSize.height);
//用convertTouchToNodeSpaceAR:来转换坐标,转换坐标还有convertTouchToNodeSpace: convertToNodeSpace:
//这两个函数,在网