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 ---> 移动球。---> 空洞

当我将一个精灵移动到空洞时,中间的精灵将被移除,我的精灵和洞应该互换,就像这样。

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。为了使运动有效,两者之一必须为真:

  • 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 回答