试试这个,
在 .h 中声明一个全局精灵变量,
CCSprite *movingBall;
在 .m 中,在 init 中,
movingBall = nil;
在touchesBegan
,
if(!movingBall)
{
for(int i = 0; i<20; i++)
{
CCSprite *currentSprite = (CCSprite *)[self getChildByTag:i+tagOffset];
if(CGRectIntersectsPoint([currentSprite boundingBox],touchPoint))
{
// get moving sprite touched
if(movingBall.position.x == emptySprite.position.x+(2*xDistance) || movingBall.position.x == emptySprite.position.x-(2*xDistance) || movingBall.position.y == emptySprite.position.y+(2*yDistance) || movingBall.position.y == emptySprite.position.y -(2*yDistance))
{
movingBall = (CCSPrite *)currentSprite;
Break;
}
}
}
}
在touchesMoved中,
if(!movingBall)
{
return;
}
movingBall.position = touchedPoint;
for(int i = 0; i<20; i++)
{
CCSprite *currentSprite = (CCSprite *)[self getChildByTag:i+tagOffset];
if(CGRectIntersectsRect([movingBall boundingBox],[currentSprite boundingBox]))
{
// current sprite touched
if(currentSprite.tag == emptySprite.tag)
{
movingBall.position = emptySprite.position;
[self removeChild:currentSprite];
Break;
}
}
}
在接触结束时,
if(!movingBall)
{
return;
}
movingBall = nil;