Cocos2d - 根据球的移动移动图层/视图

时间:2013-11-06 08:07:02

标签: cocos2d-iphone

我的游戏世界空间是4个屏幕尺寸的矩形(2行x 2列) 我根据球的运动移动相机。球位置根据物理引擎设定。 我注意到,当相机移动跟随球时,球有时会略微改变方向。

最初我认为这是一种基于相机移动的幻觉,但是当我画一条路时,线条有时是弯曲的。 我使用了CCFollow实现代码背后的一些代码(不是CCFollow本身,但它背后的代码),用于跟踪球 (我有关于CCfollow的问题)

下面是我的Tick()函数: ----------------- - (void)tick:(ccTime)dt {

bool ballMov = false;
CGPoint pos;
CCSprite *ballData;

dt = 1.0 / 60.0f;
world->Step(dt,8, 3 );

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
        CCSprite *tempData = (CCSprite *)b->GetUserData();

        if (tempData == ball){
            ballData = tempData;
            ballMov = true;
            ballData.position = ccp(b->GetPosition().x * PTM_RATIO,    b->GetPosition().y * PTM_RATIO);
        }    
    }
}


//
//  Move the layer so we can keep looking at the travelling ball
//  Code is similar to CCFollow code with some adjustments
pos = ballPosition;    
if(ballMov){ 

    #define CLAMP(x,y,z) MIN(MAX(x,y),z)
    pos = ccp(CLAMP(pos.x,leftBoundary,rightBoundary), CLAMP(pos.y,bottomBoundary,topBoundary));     
    #undef CLAMP

    pos = ccpSub(pos, halfScreenSize );
    pos.x = - pos.x ;
    pos.y = - pos.y ;

    //calculate delta move towards the new position
    if(gunShot){
        CGPoint moveVect;
        CGPoint oldPos = [self position];
        double dist = ccpDistance(pos, oldPos);
        if (dist > 1){
            moveVect = ccpMult(ccpSub(pos,oldPos),0.4); //0.05 is the smooth constant.
            oldPos = ccpAdd(oldPos, moveVect);
            pos = oldPos;        
        }
    }    
}

//move towards the ball 
[self setPosition:(pos)];

lastPosition = pos;
ballPosition = CGPointMake([ball body]->GetPosition().x * PTM_RATIO, [ball body]->GetPosition().y * PTM_RATIO );   

//if ball is out of gameworld space then reset 
if (!CGRectContainsPoint ( outerBoundaryRect, ballPosition)){              
    ball.visible = false ;  
    [self recreateBody];       //Ball is out of Gameworld, recreate it at the default position 
}    

}

0 个答案:

没有答案