iphone app - 迷宫解决

时间:2011-07-22 05:22:44

标签: iphone

我正在制作一款带有迷宫的iPhone游戏,并且这个恶棍会在迷宫中追逐你。

我没有使用OpenGL ES也没有使用tile map,所以目前碰撞检测是由xcode的CGRectsIntersectsRect函数执行的,每个wall都是一个IB变量,有点像:

float hunterdiffx = abs(hunter.center.x - hunterDest.x);
float hunterdiffy = abs(hunter.center.y - hunterDest.y);

if((hunterdiffx!=0.0)&&(hunterdiffx>(1.0*diffy))&&(!hunterCollided)){
    //NSLog(@"diffx >");
    hunterVel.y = 0;
    if(hunterDest.x>(hunter.center.x+3)){
        //NSLog(@"x >");
        hunterVel.x = 3;
    }
    else if(hunterDest.x<(hunter.center.x-3)){
        //NSLog(@"x <");
        hunterVel.x= -3;
    }
    //when rabbit reaches the destination
    else if((hunter.center.x>(hunterDest.x-3)) && (hunter.center.x < (hunterDest.x+3))){
        //NSLog(@"x =");
        //NSLog(@"one side reach");
        hunterVel.x = 0;
        hunterVel.y = 0;
    }
}

if(CGRectIntersectsRect(hunter.frame, border1.frame){
    hunterCollided = TRUE;

    if(hunterVel.x==0){

        NSLog(@"vertical collision");
        if(hunterVel.y>0){
            hunter.center = CGPointMake(hunter.center.x, hunter.center.y -10);
        }
        if(hunterVel.y<0){
            hunter.center = CGPointMake(hunter.center.x, hunter.center.y +10);
        }
        hunterVel.y=0;
        if(hunterDest.x > hunter.center.x){
            hunterVel.x=3;
        }
        if(hunterDest.x < hunter.center.x){
            hunterVel.x=-3;
        }
      }
// ...
}

这正如预期的那样导致了猎人非常愚蠢的行为,现在我想知道,因为我没有使用瓷砖地图而且不能只使用数组来表示猎人的尝试,是否有任何算法方法可以让猎人在迷宫中找到正确的道路?

我在很多数学方面都很挣扎,现在我的大脑出现故障了。如果您有任何想法,请帮助我。

1 个答案:

答案 0 :(得分:2)

使用CGRectIntersectsRect进行碰撞检测是一个非常糟糕的想法......因为你必须跟踪移动精灵周围的每个物体并检查矩形交叉点...

你可能正在这样做,因为你已经熟悉了UIKit / CoreGraphics并且不想采用OpenGLES陡峭的学习曲线....

但您可以使用第三方库(例如Cocos2D)开箱即用的碰撞检测(或一般物理)..这需要一些时间来学习,但会为您提供强大的解决方案....

这只是我的意见..

相关问题