これを試して、
.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;
}
}
}
touchesEndedでは、
if(!movingBall)
{
return;
}
movingBall = nil;