0

シーンに 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;
}
4

1 に答える 1

0

(私はこのプログラミング言語を知らないので、これは純粋にアルゴリズムの観点です)

まず、ユーザーがボールを移動しようとしている開始位置と終了位置を計算します。移動が埋められたスペースからのものでない場合は、拒否します。空きスペースに移動しない場合は、拒否します。

次に、ボールの動きを計算します-xDelta = xEnd-xStart、yDelta=yEnd-yStart。動きが有効であるためには、2つのうちの1つが真でなければなりません:

  • abs(xDelta)またはabs(yDelta)== 2のいずれか、および他の==0。
  • abs(xDelta)== 2およびabs(yDelta)==2。

どちらも当てはまらない場合は、拒否します。

最後に、中点セルを計算します。xMid = xStart + xDelta / 2、yMid = yStart + yDelta/2。これがボールで満たされている場合は、xStart、yStartを空にし、xMid、yMidを空にして、xEnd、yEndを埋めて移動を実行します。ボールが入っていない場合は拒否します。

于 2013-02-06T04:54:29.143 に答える