シーンに 32 個のスプライトがあり、スプライトをこのように配置しました
o o o
o o o
o o o o o o o
o o o . o o o
o o o o o o o
o o o
o o o
o ---> ムービングボール。---> 空の穴
1 つのスプライトを空の穴に移動すると、中央のスプライトが削除され、スプライトと穴が次のように入れ替わります。
o o . -----> . . o
可能な方法:
| | | | | | |
o | | . | | o | . | . | .
o | o o . | o | . o o | o | o | o | o
. | | o | | . | o | o | o
| | | | | | |
スプライトを移動する合計 8 つの可能性。
これを行う方法と、コードで何を変更する必要があるかを誰かが助けてくれますか?
私のコーディングはこちら
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if(movingBall)
{
for(int i = 0; i<32; i++)
{
CCSprite *currentSprite = (CCSprite *)[self getChildByTag:i];
if(CGRectContainsPoint([currentSprite boundingBox],location))
{
// get moving sprite touched
if(movingBall.position.x == hole.position.x+(2*75) || movingBall.position.x == hole.position.x-(2*75) || movingBall.position.y == hole.position.y+(2*75) || movingBall.position.y == hole.position.y -(2*75))
{
movingBall = (CCSprite *)currentSprite;
break;
}
}
}
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
if(!movingBall)
{
return;
}
movingBall.position = location;
for(int i = 0; i<32; i++)
{
CCSprite *currentSprite = (CCSprite *)[self getChildByTag:i];
if(CGRectIntersectsRect([movingBall boundingBox],[currentSprite boundingBox]))
{
// current sprite touched
if(currentSprite.tag == hole.tag)
{
movingBall.position = hole.position;
[self removeChild:currentSprite];
break;
}
}
}}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!movingBall)
{
return;
}
movingBall = nil;
}